Spaces:
Sleeping
Sleeping
File size: 4,580 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | import type { FastMCP } from 'fastmcp';
import { UserError } from 'fastmcp';
import { z } from 'zod';
import { getSheetsClient } from '../../clients.js';
import * as SheetsHelpers from '../../googleSheetsApiHelpers.js';
function colIndexToLetters(index: number): string {
let s = '';
let i = index;
do {
s = String.fromCharCode(65 + (i % 26)) + s;
i = Math.floor(i / 26) - 1;
} while (i >= 0);
return s;
}
function rgbToHex(rgb: { red?: number; green?: number; blue?: number } | null | undefined): string {
if (!rgb) return '#000000';
const r = Math.round((rgb.red ?? 0) * 255);
const g = Math.round((rgb.green ?? 0) * 255);
const b = Math.round((rgb.blue ?? 0) * 255);
return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}`.toUpperCase();
}
export function register(server: FastMCP) {
server.addTool({
name: 'getConditionalFormatting',
description:
'Lists all conditional formatting rules for a sheet as JSON. Each rule includes its index (needed for deleteConditionalFormatting), kind (BOOLEAN or GRADIENT), ranges, condition type/values, and applied formats (colors, bold, italic).',
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.'),
}),
execute: async (args, { log }) => {
const sheets = await getSheetsClient();
log.info(`Getting conditional formatting rules for spreadsheet ${args.spreadsheetId}`);
try {
const sheetId = await SheetsHelpers.resolveSheetId(
sheets,
args.spreadsheetId,
args.sheetName
);
const response = await sheets.spreadsheets.get({
spreadsheetId: args.spreadsheetId,
fields: 'sheets(properties(sheetId,title),conditionalFormats)',
});
const sheet = response.data.sheets?.find((s) => s.properties?.sheetId === sheetId);
const rules = sheet?.conditionalFormats ?? [];
const sheetTitle = sheet?.properties?.title ?? null;
const ruleSummaries = rules.map((rule, idx) => {
const condition = rule.booleanRule?.condition;
const gradient = rule.gradientRule;
const fmt = rule.booleanRule?.format ?? {};
const ranges = (rule.ranges ?? []).map((r) => {
const startCol =
r.startColumnIndex != null ? colIndexToLetters(r.startColumnIndex) : '';
const endCol = r.endColumnIndex != null ? colIndexToLetters(r.endColumnIndex - 1) : '';
const startRow = r.startRowIndex != null ? r.startRowIndex + 1 : '';
const endRow = r.endRowIndex != null ? r.endRowIndex : '';
return `${startCol}${startRow}:${endCol}${endRow}`;
});
const kind = gradient ? 'GRADIENT' : 'BOOLEAN';
const conditionType = condition?.type ?? (gradient ? 'GRADIENT' : null);
const conditionValues = (condition?.values ?? [])
.map((v) => v.userEnteredValue)
.filter((v): v is string => typeof v === 'string');
const bg = fmt.backgroundColor;
const backgroundColor = bg
? rgbToHex({ red: bg.red ?? 0, green: bg.green ?? 0, blue: bg.blue ?? 0 })
: null;
const fg = fmt.textFormat?.foregroundColor;
const textColor = fg
? rgbToHex({ red: fg.red ?? 0, green: fg.green ?? 0, blue: fg.blue ?? 0 })
: null;
return {
index: idx,
kind,
ranges,
conditionType,
conditionValues,
backgroundColor,
textColor,
bold: fmt.textFormat?.bold ?? false,
italic: fmt.textFormat?.italic ?? false,
};
});
return JSON.stringify(
{
spreadsheetId: args.spreadsheetId,
sheetName: sheetTitle,
count: ruleSummaries.length,
rules: ruleSummaries,
},
null,
2
);
} catch (error: any) {
log.error(`Error getting conditional formatting: ${error.message || error}`);
if (error instanceof UserError) throw error;
throw new UserError(
`Failed to get conditional formatting: ${error.message || 'Unknown error'}`
);
}
},
});
}
|