sheets_duplicate_sheet
Duplicate a sheet in Google Sheets by specifying the spreadsheet ID, sheet ID, insertion index, and new name. Streamline worksheet replication for organized data management.
Instructions
Duplicate a sheet within a Google Sheets spreadsheet
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| insertSheetIndex | No | The index where the new sheet should be inserted (0-based) | |
| newSheetName | No | The name for the duplicated sheet | |
| sheetId | Yes | The ID of the sheet to duplicate (use sheets_get_metadata to find sheet IDs) | |
| spreadsheetId | Yes | The ID of the spreadsheet (found in the URL after /d/) |
Implementation Reference
- src/tools/duplicate-sheet.ts:34-65 (handler)The core handler function that executes the sheet duplication logic using Google Sheets API's batchUpdate with duplicateSheet request. It validates input, calls the API, extracts the new sheet properties, and returns a formatted response.export async function handleDuplicateSheet(input: any) { try { const validatedInput = validateDuplicateSheetInput(input); const sheets = await getAuthenticatedClient(); const response = await sheets.spreadsheets.batchUpdate({ spreadsheetId: validatedInput.spreadsheetId, requestBody: { requests: [ { duplicateSheet: { sourceSheetId: validatedInput.sheetId, insertSheetIndex: validatedInput.insertSheetIndex, newSheetName: validatedInput.newSheetName, }, }, ], }, }); const duplicatedSheet = response.data.replies?.[0]?.duplicateSheet ? response.data.replies[0].duplicateSheet.properties : undefined; return formatSheetOperationResponse('Sheet duplicated', { newSheetId: duplicatedSheet ? duplicatedSheet.sheetId : undefined, title: duplicatedSheet ? duplicatedSheet.title : undefined, index: duplicatedSheet ? duplicatedSheet.index : undefined, }); } catch (error) { return handleError(error); } }
- src/tools/duplicate-sheet.ts:7-32 (schema)The Tool schema definition specifying the name, description, and inputSchema (including properties for spreadsheetId, sheetId, insertSheetIndex, newSheetName; required: spreadsheetId, sheetId).export const duplicateSheetTool: Tool = { name: 'sheets_duplicate_sheet', description: 'Duplicate a sheet within a Google Sheets spreadsheet', inputSchema: { type: 'object', properties: { spreadsheetId: { type: 'string', description: 'The ID of the spreadsheet (found in the URL after /d/)', }, sheetId: { type: 'number', description: 'The ID of the sheet to duplicate (use sheets_get_metadata to find sheet IDs)', }, insertSheetIndex: { type: 'number', description: 'The index where the new sheet should be inserted (0-based)', }, newSheetName: { type: 'string', description: 'The name for the duplicated sheet', }, }, required: ['spreadsheetId', 'sheetId'], }, };
- src/index.ts:44-44 (registration)Registration of the 'sheets_duplicate_sheet' tool with its handler (tools.handleDuplicateSheet) in the central toolHandlers Map used for tool execution.['sheets_duplicate_sheet', tools.handleDuplicateSheet],
- src/index.ts:79-79 (registration)Inclusion of the duplicateSheetTool in the allTools array used for listing available tools via ListToolsRequest.tools.duplicateSheetTool,
- src/tools/index.ts:12-12 (registration)Re-export of the duplicateSheetTool and handleDuplicateSheet from duplicate-sheet.ts, making them available as tools.* in src/index.ts.export * from './duplicate-sheet.js';