File size: 1,720 Bytes
7dc28be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import type { FastMCP } from 'fastmcp';
import { UserError } from 'fastmcp';
import { z } from 'zod';
import { getSheetsClient } from '../../clients.js';

export function register(server: FastMCP) {
  server.addTool({
    name: 'copySheetTo',
    description:
      'Copies a sheet (tab) from one spreadsheet to another spreadsheet. Use getSpreadsheetInfo to find the numeric sheet ID. The copied sheet will be appended to the destination spreadsheet.',
    parameters: z.strictObject({
      sourceSpreadsheetId: z.string().describe('The spreadsheet ID of the source file.'),
      sheetId: z
        .number()
        .int()
        .describe('The numeric sheet ID to copy. Use getSpreadsheetInfo to find sheet IDs.'),
      destinationSpreadsheetId: z.string().describe('The spreadsheet ID of the destination file.'),
    }),
    execute: async (args, { log }) => {
      const sheets = await getSheetsClient();
      log.info(
        `Copying sheet ${args.sheetId} from ${args.sourceSpreadsheetId} to ${args.destinationSpreadsheetId}`
      );

      try {
        const response = await sheets.spreadsheets.sheets.copyTo({
          spreadsheetId: args.sourceSpreadsheetId,
          sheetId: args.sheetId,
          requestBody: { destinationSpreadsheetId: args.destinationSpreadsheetId },
        });

        const props = response.data;
        return `Successfully copied sheet to destination as "${props.title}" (Sheet ID: ${props.sheetId}).`;
      } catch (error: any) {
        log.error(`Error copying sheet: ${error.message || error}`);
        if (error instanceof UserError) throw error;
        throw new UserError(`Failed to copy sheet: ${error.message || 'Unknown error'}`);
      }
    },
  });
}