File size: 2,672 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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: 'setDropdownValidation',
    description:
      'Adds or removes a dropdown list on a range of cells. Provide values to create a dropdown restricting input to those options. Omit values to remove dropdown validation from the range.',
    parameters: z.strictObject({
      spreadsheetId: z
        .string()
        .describe(
          'The spreadsheet ID — the long string between /d/ and /edit in a Google Sheets URL.'
        ),
      range: z
        .string()
        .describe(
          'A1 notation range to apply the dropdown to (e.g., "Sheet1!B2:B100" or "C2:C50").'
        ),
      values: z
        .array(z.string())
        .optional()
        .describe(
          'The allowed dropdown options (e.g., ["Open", "In Progress", "Done"]). Omit to remove existing dropdown validation from the range.'
        ),
      strict: z
        .boolean()
        .optional()
        .default(true)
        .describe('If true, reject input that does not match one of the dropdown values.'),
      inputMessage: z
        .string()
        .optional()
        .describe('Help text shown when a cell with the dropdown is selected.'),
    }),
    execute: async (args, { log }) => {
      const sheets = await getSheetsClient();
      const isClearing = !args.values || args.values.length === 0;
      log.info(
        isClearing
          ? `Clearing dropdown validation on "${args.range}" in spreadsheet ${args.spreadsheetId}`
          : `Setting dropdown validation on "${args.range}" with ${args.values!.length} options in spreadsheet ${args.spreadsheetId}`
      );

      try {
        await SheetsHelpers.setDropdownValidation(
          sheets,
          args.spreadsheetId,
          args.range,
          args.values,
          args.strict,
          args.inputMessage
        );

        if (isClearing) {
          return `Successfully removed dropdown validation from range "${args.range}".`;
        }
        return `Successfully added dropdown validation to range "${args.range}" with ${args.values!.length} options: ${args.values!.join(', ')}.`;
      } catch (error: any) {
        log.error(`Error setting dropdown validation: ${error.message || error}`);
        if (error instanceof UserError) throw error;
        throw new UserError(
          `Failed to set dropdown validation: ${error.message || 'Unknown error'}`
        );
      }
    },
  });
}