File size: 2,060 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import type { FastMCP } from 'fastmcp';
import { UserError } from 'fastmcp';
import { z } from 'zod';
import { getSheetsClient } from '../../clients.js';
import * as SheetsHelpers from '../../googleSheetsApiHelpers.js';

export function register(server: FastMCP) {
  server.addTool({
    name: 'setColumnWidths',
    description:
      'Sets the width (in pixels) of one or more columns in a spreadsheet. Accepts multiple column specs in a single call, each targeting a single column or a contiguous range (e.g., "A", "B:D").',
    parameters: z.strictObject({
      spreadsheetId: z
        .string()
        .describe(
          'The spreadsheet ID — the long string between /d/ and /edit in a Google Sheets URL.'
        ),
      sheetName: z
        .string()
        .optional()
        .describe('Name of the sheet/tab. Defaults to the first sheet if not provided.'),
      columnWidths: z
        .array(
          z.strictObject({
            column: z
              .string()
              .describe('Column or column range in A1 notation (e.g., "A", "B:D").'),
            width: z.number().int().min(0).describe('Width in pixels. Use 0 to hide the column.'),
          })
        )
        .min(1)
        .describe('List of column width specifications to apply.'),
    }),
    execute: async (args, { log }) => {
      const sheets = await getSheetsClient();
      log.info(`Setting column widths in spreadsheet ${args.spreadsheetId}`);

      try {
        await SheetsHelpers.setColumnWidths(
          sheets,
          args.spreadsheetId,
          args.sheetName,
          args.columnWidths
        );

        const summary = args.columnWidths.map((cw) => `${cw.column}=${cw.width}px`).join(', ');
        return `Successfully set column widths: ${summary}.`;
      } catch (error: any) {
        log.error(`Error setting column widths: ${error.message || error}`);
        if (error instanceof UserError) throw error;
        throw new UserError(`Failed to set column widths: ${error.message || 'Unknown error'}`);
      }
    },
  });
}