Skip to main content
Glama
marekrost

mcp-server-spreadsheet

copy_sheet

Duplicate a sheet within the same spreadsheet workbook, copying all cell values to create a new sheet with an optional custom name and position.

Instructions

Duplicate a sheet within the same workbook.

Copies all cell values. Returns the name of the new sheet. Not supported for CSV files.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fileYesPath to the spreadsheet file
source_nameYesName of the existing sheet to duplicate
new_nameNoName for the copy. Auto-generated if omitted.
positionNo1-based position for the copied sheet. Placed at the end if omitted.

Implementation Reference

  • The MCP tool registration and handler function for `copy_sheet`. It takes the file path, source sheet name, and optional new name/position parameters, performs the copy via the backend, and saves the workbook.
    @mcp.tool()
    def copy_sheet(
        file: Annotated[str, Field(description="Path to the spreadsheet file")],
        source_name: Annotated[str, Field(description="Name of the existing sheet to duplicate")],
        new_name: Annotated[str | None, Field(description="Name for the copy. Auto-generated if omitted.")] = None,
        position: Annotated[int | None, Field(description="1-based position for the copied sheet. Placed at the end if omitted.")] = None,
    ) -> str:
        """Duplicate a sheet within the same workbook.
    
        Copies all cell values. Returns the name of the new sheet.
        Not supported for CSV files.
        """
        wb = load_workbook(file)
        copied = wb.copy_sheet(source_name)
        if new_name:
            copied.title = new_name
        if position is not None:
            current_idx = wb.sheetnames.index(copied.title)
            wb.move_sheet(copied, offset=position - 1 - current_idx)
        wb.save(file)
        return copied.title
    
    
    # ---------------------------------------------------------------------------
  • The backend implementation for copying a sheet in XLSX files using openpyxl's `copy_worksheet` method.
    def copy_sheet(self, source_name: str) -> XlsxSheet:
        if source_name not in self._wb.sheetnames:
            raise ValueError(f"Sheet not found: {source_name!r}")
        copied = self._wb.copy_worksheet(self._wb[source_name])
        return XlsxSheet(copied)

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/marekrost/mcp-server-spreadsheet'

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