|
| 1 | +import type { FastMCP } from 'fastmcp'; |
| 2 | +import { UserError } from 'fastmcp'; |
| 3 | +import { z } from 'zod'; |
| 4 | +import { getSheetsClient } from '../../clients.js'; |
| 5 | +import { rowColToA1 } from '../../googleSheetsApiHelpers.js'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Converts a Google Sheets RGBA color object (0-1 range) to a hex string. |
| 9 | + * Returns null if the color is undefined or has no meaningful channels. |
| 10 | + */ |
| 11 | +function rgbaToHex( |
| 12 | + color: { red?: number | null; green?: number | null; blue?: number | null } | null | undefined |
| 13 | +): string | null { |
| 14 | + if (!color) return null; |
| 15 | + const r = Math.round((color.red ?? 0) * 255); |
| 16 | + const g = Math.round((color.green ?? 0) * 255); |
| 17 | + const b = Math.round((color.blue ?? 0) * 255); |
| 18 | + return `#${r.toString(16).padStart(2, '0').toUpperCase()}${g.toString(16).padStart(2, '0').toUpperCase()}${b.toString(16).padStart(2, '0').toUpperCase()}`; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Extracts a simplified formatting summary from a Google Sheets CellFormat object. |
| 23 | + * Only includes properties that are explicitly set (non-default). |
| 24 | + */ |
| 25 | +function simplifyFormat(fmt: any): Record<string, any> | null { |
| 26 | + if (!fmt) return null; |
| 27 | + const result: Record<string, any> = {}; |
| 28 | + |
| 29 | + // Text formatting |
| 30 | + if (fmt.textFormat) { |
| 31 | + const tf: Record<string, any> = {}; |
| 32 | + if (fmt.textFormat.bold) tf.bold = true; |
| 33 | + if (fmt.textFormat.italic) tf.italic = true; |
| 34 | + if (fmt.textFormat.strikethrough) tf.strikethrough = true; |
| 35 | + if (fmt.textFormat.underline) tf.underline = true; |
| 36 | + if (fmt.textFormat.fontSize != null) tf.fontSize = fmt.textFormat.fontSize; |
| 37 | + if (fmt.textFormat.fontFamily) tf.fontFamily = fmt.textFormat.fontFamily; |
| 38 | + if (fmt.textFormat.foregroundColorStyle?.rgbColor) { |
| 39 | + tf.foregroundColor = rgbaToHex(fmt.textFormat.foregroundColorStyle.rgbColor); |
| 40 | + } else if (fmt.textFormat.foregroundColor) { |
| 41 | + tf.foregroundColor = rgbaToHex(fmt.textFormat.foregroundColor); |
| 42 | + } |
| 43 | + if (Object.keys(tf).length > 0) result.textFormat = tf; |
| 44 | + } |
| 45 | + |
| 46 | + // Background color |
| 47 | + if (fmt.backgroundColorStyle?.rgbColor) { |
| 48 | + result.backgroundColor = rgbaToHex(fmt.backgroundColorStyle.rgbColor); |
| 49 | + } else if (fmt.backgroundColor) { |
| 50 | + result.backgroundColor = rgbaToHex(fmt.backgroundColor); |
| 51 | + } |
| 52 | + |
| 53 | + // Alignment |
| 54 | + if (fmt.horizontalAlignment) result.horizontalAlignment = fmt.horizontalAlignment; |
| 55 | + if (fmt.verticalAlignment) result.verticalAlignment = fmt.verticalAlignment; |
| 56 | + |
| 57 | + // Number format |
| 58 | + if (fmt.numberFormat) { |
| 59 | + result.numberFormat = { |
| 60 | + type: fmt.numberFormat.type, |
| 61 | + pattern: fmt.numberFormat.pattern, |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + // Borders |
| 66 | + if (fmt.borders) { |
| 67 | + const borders: Record<string, any> = {}; |
| 68 | + for (const side of ['top', 'bottom', 'left', 'right'] as const) { |
| 69 | + if (fmt.borders[side]) { |
| 70 | + borders[side] = { |
| 71 | + style: fmt.borders[side].style, |
| 72 | + ...(fmt.borders[side].colorStyle?.rgbColor |
| 73 | + ? { color: rgbaToHex(fmt.borders[side].colorStyle.rgbColor) } |
| 74 | + : fmt.borders[side].color |
| 75 | + ? { color: rgbaToHex(fmt.borders[side].color) } |
| 76 | + : {}), |
| 77 | + }; |
| 78 | + } |
| 79 | + } |
| 80 | + if (Object.keys(borders).length > 0) result.borders = borders; |
| 81 | + } |
| 82 | + |
| 83 | + // Wrap strategy |
| 84 | + if (fmt.wrapStrategy) result.wrapStrategy = fmt.wrapStrategy; |
| 85 | + |
| 86 | + return Object.keys(result).length > 0 ? result : null; |
| 87 | +} |
| 88 | + |
| 89 | +export function register(server: FastMCP) { |
| 90 | + server.addTool({ |
| 91 | + name: 'readCellFormat', |
| 92 | + description: |
| 93 | + 'Reads the formatting/style of cells in a given range. Returns formatting details like bold, italic, fontSize, fontFamily, colors, alignment, borders, and number format per cell.', |
| 94 | + parameters: z.object({ |
| 95 | + spreadsheetId: z |
| 96 | + .string() |
| 97 | + .describe( |
| 98 | + 'The spreadsheet ID — the long string between /d/ and /edit in a Google Sheets URL.' |
| 99 | + ), |
| 100 | + range: z |
| 101 | + .string() |
| 102 | + .describe('A1 notation range to read formatting from (e.g., "Sheet1!A1:D5" or "A1:B2").'), |
| 103 | + }), |
| 104 | + execute: async (args, { log }) => { |
| 105 | + const sheets = await getSheetsClient(); |
| 106 | + log.info( |
| 107 | + `Reading cell format for range "${args.range}" in spreadsheet ${args.spreadsheetId}` |
| 108 | + ); |
| 109 | + |
| 110 | + try { |
| 111 | + const response = await sheets.spreadsheets.get({ |
| 112 | + spreadsheetId: args.spreadsheetId, |
| 113 | + ranges: [args.range], |
| 114 | + includeGridData: true, |
| 115 | + fields: |
| 116 | + 'sheets.data.rowData.values.userEnteredFormat,sheets.data.startRow,sheets.data.startColumn', |
| 117 | + }); |
| 118 | + |
| 119 | + const sheetData = response.data.sheets?.[0]?.data?.[0]; |
| 120 | + if (!sheetData?.rowData) { |
| 121 | + return JSON.stringify({ range: args.range, cells: [] }, null, 2); |
| 122 | + } |
| 123 | + |
| 124 | + const startRow = sheetData.startRow ?? 0; |
| 125 | + const startCol = sheetData.startColumn ?? 0; |
| 126 | + |
| 127 | + const cells: Array<{ cell: string; format: Record<string, any> }> = []; |
| 128 | + |
| 129 | + for (let rowIdx = 0; rowIdx < sheetData.rowData.length; rowIdx++) { |
| 130 | + const row = sheetData.rowData[rowIdx]; |
| 131 | + if (!row.values) continue; |
| 132 | + |
| 133 | + for (let colIdx = 0; colIdx < row.values.length; colIdx++) { |
| 134 | + const cellData = row.values[colIdx]; |
| 135 | + const fmt = simplifyFormat(cellData?.userEnteredFormat); |
| 136 | + if (fmt) { |
| 137 | + const cellRef = rowColToA1(startRow + rowIdx, startCol + colIdx); |
| 138 | + cells.push({ cell: cellRef, format: fmt }); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + return JSON.stringify({ range: args.range, cells }, null, 2); |
| 144 | + } catch (error: any) { |
| 145 | + log.error( |
| 146 | + `Error reading cell format for spreadsheet ${args.spreadsheetId}: ${error.message || error}` |
| 147 | + ); |
| 148 | + if (error instanceof UserError) throw error; |
| 149 | + throw new UserError(`Failed to read cell format: ${error.message || 'Unknown error'}`); |
| 150 | + } |
| 151 | + }, |
| 152 | + }); |
| 153 | +} |
0 commit comments