mirror of
https://github.com/haris-musa/excel-mcp-server.git
synced 2025-12-08 08:42:30 +08:00
Add tool to get merged cells (#59)
This commit is contained in:
13
TOOLS.md
13
TOOLS.md
@ -145,6 +145,19 @@ unmerge_cells(filepath: str, sheet_name: str, start_cell: str, end_cell: str) ->
|
|||||||
- `end_cell`: Ending cell of range
|
- `end_cell`: Ending cell of range
|
||||||
- Returns: Success message
|
- Returns: Success message
|
||||||
|
|
||||||
|
### get_merged_cells
|
||||||
|
|
||||||
|
Get merged cells in a worksheet.
|
||||||
|
|
||||||
|
```python
|
||||||
|
get_merged_cells(filepath: str, sheet_name: str) -> str
|
||||||
|
```
|
||||||
|
|
||||||
|
- `filepath`: Path to Excel file
|
||||||
|
- `sheet_name`: Target worksheet name
|
||||||
|
- Returns: String representation of merged cells
|
||||||
|
|
||||||
|
|
||||||
## Formula Operations
|
## Formula Operations
|
||||||
|
|
||||||
### apply_formula
|
### apply_formula
|
||||||
|
|||||||
@ -32,6 +32,7 @@ from excel_mcp.sheet import (
|
|||||||
rename_sheet,
|
rename_sheet,
|
||||||
merge_range,
|
merge_range,
|
||||||
unmerge_range,
|
unmerge_range,
|
||||||
|
get_merged_ranges,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Get project root directory path for log file path.
|
# Get project root directory path for log file path.
|
||||||
@ -469,6 +470,18 @@ def unmerge_cells(filepath: str, sheet_name: str, start_cell: str, end_cell: str
|
|||||||
logger.error(f"Error unmerging cells: {e}")
|
logger.error(f"Error unmerging cells: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
@mcp.tool()
|
||||||
|
def get_merged_cells(filepath: str, sheet_name: str) -> str:
|
||||||
|
"""Get merged cells in a worksheet."""
|
||||||
|
try:
|
||||||
|
full_path = get_excel_path(filepath)
|
||||||
|
return str(get_merged_ranges(full_path, sheet_name))
|
||||||
|
except (ValidationError, SheetError) as e:
|
||||||
|
return f"Error: {str(e)}"
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error getting merged cells: {e}")
|
||||||
|
raise
|
||||||
|
|
||||||
@mcp.tool()
|
@mcp.tool()
|
||||||
def copy_range(
|
def copy_range(
|
||||||
filepath: str,
|
filepath: str,
|
||||||
|
|||||||
@ -243,6 +243,21 @@ def unmerge_range(filepath: str, sheet_name: str, start_cell: str, end_cell: str
|
|||||||
logger.error(f"Failed to unmerge range: {e}")
|
logger.error(f"Failed to unmerge range: {e}")
|
||||||
raise SheetError(str(e))
|
raise SheetError(str(e))
|
||||||
|
|
||||||
|
def get_merged_ranges(filepath: str, sheet_name: str) -> list[str]:
|
||||||
|
"""Get merged cells in a worksheet."""
|
||||||
|
try:
|
||||||
|
wb = load_workbook(filepath)
|
||||||
|
if sheet_name not in wb.sheetnames:
|
||||||
|
raise SheetError(f"Sheet '{sheet_name}' not found")
|
||||||
|
worksheet = wb[sheet_name]
|
||||||
|
return [str(merged_range) for merged_range in worksheet.merged_cells.ranges]
|
||||||
|
except SheetError as e:
|
||||||
|
logger.error(str(e))
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to get merged cells: {e}")
|
||||||
|
raise SheetError(str(e))
|
||||||
|
|
||||||
def copy_range_operation(
|
def copy_range_operation(
|
||||||
filepath: str,
|
filepath: str,
|
||||||
sheet_name: str,
|
sheet_name: str,
|
||||||
|
|||||||
Reference in New Issue
Block a user