unmerge_cells
Unmerge cells in an Excel sheet to split merged data back into individual cells. Specify file path, sheet name, and cell range to process.
Instructions
Unmerge a range of cells.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| end_cell | Yes | ||
| filepath | Yes | ||
| sheet_name | Yes | ||
| start_cell | Yes |
Implementation Reference
- src/excel_mcp/server.py:457-468 (handler)MCP tool handler for unmerge_cells, decorated with @mcp.tool() for registration, which delegates to the unmerge_range helper.@mcp.tool() def unmerge_cells(filepath: str, sheet_name: str, start_cell: str, end_cell: str) -> str: """Unmerge a range of cells.""" try: full_path = get_excel_path(filepath) result = unmerge_range(full_path, sheet_name, start_cell, end_cell) return result["message"] except (ValidationError, SheetError) as e: return f"Error: {str(e)}" except Exception as e: logger.error(f"Error unmerging cells: {e}") raise
- src/excel_mcp/sheet.py:213-244 (helper)Core helper function implementing the unmerge logic using openpyxl. Validates the range, checks if merged, calls worksheet.unmerge_cells, and saves the workbook.def unmerge_range(filepath: str, sheet_name: str, start_cell: str, end_cell: str) -> Dict[str, Any]: """Unmerge a range of cells.""" try: wb = load_workbook(filepath) if sheet_name not in wb.sheetnames: raise SheetError(f"Sheet '{sheet_name}' not found") worksheet = wb[sheet_name] start_row, start_col, end_row, end_col = parse_cell_range(start_cell, end_cell) if end_row is None or end_col is None: raise SheetError("Both start and end cells must be specified for unmerging") range_string = format_range_string(start_row, start_col, end_row, end_col) # Check if range is actually merged merged_ranges = worksheet.merged_cells.ranges target_range = range_string.upper() if not any(str(merged_range).upper() == target_range for merged_range in merged_ranges): raise SheetError(f"Range '{range_string}' is not merged") worksheet.unmerge_cells(range_string) wb.save(filepath) return {"message": f"Range '{range_string}' unmerged successfully"} except SheetError as e: logger.error(str(e)) raise except Exception as e: logger.error(f"Failed to unmerge range: {e}") raise SheetError(str(e))