Skip to main content
Glama

merge_cells

Combine a range of cells in an Excel spreadsheet to create a single merged cell. Input file path, sheet name, and start and end cells to perform the operation.

Instructions

Merge a range of cells.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
end_cellYes
filepathYes
sheet_nameYes
start_cellYes

Implementation Reference

  • MCP tool handler and registration for 'merge_cells'. The @mcp.tool() decorator registers this function as an MCP tool. It handles input validation via type hints, calls the helper function, and returns success/error messages.
    @mcp.tool()
    def merge_cells(filepath: str, sheet_name: str, start_cell: str, end_cell: str) -> str:
        """Merge a range of cells."""
        try:
            full_path = get_excel_path(filepath)
            result = merge_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 merging cells: {e}")
            raise
  • Supporting utility function that performs the actual cell merging using openpyxl. Parses the cell range, calls worksheet.merge_cells(), saves the workbook, and returns a success message.
    def merge_range(filepath: str, sheet_name: str, start_cell: str, end_cell: str) -> Dict[str, Any]:
        """Merge a range of cells."""
        try:
            wb = load_workbook(filepath)
            if sheet_name not in wb.sheetnames:
                raise SheetError(f"Sheet '{sheet_name}' not found")
                
            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 merging")
    
            range_string = format_range_string(start_row, start_col, end_row, end_col)
            worksheet = wb[sheet_name]
            worksheet.merge_cells(range_string)
            wb.save(filepath)
            return {"message": f"Range '{range_string}' merged in sheet '{sheet_name}'"}
        except SheetError as e:
            logger.error(str(e))
            raise
        except Exception as e:
            logger.error(f"Failed to merge range: {e}")
            raise SheetError(str(e))

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/haris-musa/excel-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server