Skip to main content
Glama

copy_worksheet

Duplicate a worksheet within an Excel workbook by specifying source and target sheet names, enabling efficient data organization and management.

Instructions

Copy worksheet within workbook.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filepathYes
source_sheetYes
target_sheetYes

Implementation Reference

  • MCP tool handler for 'copy_worksheet'. Registers the tool via @mcp.tool() decorator and delegates to copy_sheet helper function from sheet.py. Defines input parameters: filepath, source_sheet, target_sheet.
    @mcp.tool()
    def copy_worksheet(
        filepath: str,
        source_sheet: str,
        target_sheet: str
    ) -> str:
        """Copy worksheet within workbook."""
        try:
            full_path = get_excel_path(filepath)
            result = copy_sheet(full_path, source_sheet, target_sheet)
            return result["message"]
        except (ValidationError, SheetError) as e:
            return f"Error: {str(e)}"
        except Exception as e:
            logger.error(f"Error copying worksheet: {e}")
            raise
  • Core helper function implementing the worksheet copy logic using openpyxl's Workbook.copy_worksheet() method. Performs validation and saves the workbook.
    def copy_sheet(filepath: str, source_sheet: str, target_sheet: str) -> Dict[str, Any]:
        """Copy a worksheet within the same workbook."""
        try:
            wb = load_workbook(filepath)
            if source_sheet not in wb.sheetnames:
                raise SheetError(f"Source sheet '{source_sheet}' not found")
                
            if target_sheet in wb.sheetnames:
                raise SheetError(f"Target sheet '{target_sheet}' already exists")
                
            source = wb[source_sheet]
            target = wb.copy_worksheet(source)
            target.title = target_sheet
            
            wb.save(filepath)
            return {"message": f"Sheet '{source_sheet}' copied to '{target_sheet}'"}
        except SheetError as e:
            logger.error(str(e))
            raise
        except Exception as e:
            logger.error(f"Failed to copy sheet: {e}")
            raise SheetError(str(e))
  • The @mcp.tool() decorator registers the copy_worksheet function as an MCP tool.
    @mcp.tool()

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