getSheetData
Extract data from specific sheets in Google Spreadsheets by providing the URL and sheet name to access structured information.
Instructions
スプレッドシートの特定シートのデータを取得
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | スプレッドシートのURL | |
| sheetName | Yes | 取得するシート名 |
Implementation Reference
- src/tools/getSheetData.ts:20-79 (handler)The main execution logic for the 'getSheetData' tool handler. It processes the input URL and sheetName, validates the spreadsheet and sheet, fetches the data using helper functions, formats the result, and returns it in the expected MCP format.handler: async ({ url, sheetName }: { url: string; sheetName: string }) => { try { const spreadsheetId = extractSpreadsheetId(url); if (!spreadsheetId) { return { content: [ { type: "text" as const, text: "有効なスプレッドシートURLではありません。Google SpreadsheetのURLを入力してください。" } ], isError: true }; } // スプレッドシート情報を取得して、シート名が有効かチェック const spreadsheetInfo = await getSpreadsheetInfo(spreadsheetId); const isValidSheet = spreadsheetInfo.sheets.some(sheet => sheet.name === sheetName); if (!isValidSheet) { const availableSheets = spreadsheetInfo.sheets.map(sheet => sheet.name).join(", "); return { content: [ { type: "text" as const, text: `シート「${sheetName}」は存在しません。利用可能なシート: ${availableSheets}` } ], isError: true }; } // シートデータを取得 const sheetData = await getSheetData(spreadsheetId, sheetName); // データをフォーマットして返す const formattedResult = formatSheetData(sheetData, sheetName, spreadsheetInfo.name); return { content: [ { type: "text" as const, text: formattedResult } ] }; } catch (error) { console.error("シートデータ取得エラー:", error); return { content: [ { type: "text" as const, text: `シートデータの取得中にエラーが発生しました: ${error instanceof Error ? error.message : String(error)}` } ], isError: true }; } }
- src/tools/getSheetData.ts:16-19 (schema)Zod schema defining the input parameters for the getSheetData tool: spreadsheet URL and sheet name.schema: { url: z.string().describe("スプレッドシートのURL"), sheetName: z.string().describe("取得するシート名") },
- src/server.ts:34-40 (registration)Registration of the getSheetData tool with the MCP server using server.tool().// シートデータ取得ツール server.tool( getSheetDataTool.name, getSheetDataTool.description, getSheetDataTool.schema, getSheetDataTool.handler );
- src/api/spreadsheet.ts:78-111 (helper)Helper function that makes the actual API call to the Google Apps Script web app to retrieve sheet data, handling both real API and mock mode.export async function getSheetData( spreadsheetId: string, sheetName: string ): Promise<SheetData> { // モックモードの場合はモックデータを返す if (config.isMockMode) { return getMockSheetData(spreadsheetId, sheetName); } // API呼び出し用のURLを生成 const apiUrl = new URL(config.gas.webAppUrl); apiUrl.searchParams.append('action', 'getSheetData'); apiUrl.searchParams.append('id', spreadsheetId); apiUrl.searchParams.append('sheetName', sheetName); apiUrl.searchParams.append('apiKey', config.gas.apiKey); // 任意のパラメータ(必要に応じて) // apiUrl.searchParams.append('maxRows', '1000'); // APIリクエスト const response = await fetch(apiUrl); // ステータスコードのチェック if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || `API Error: ${response.status}`); } // レスポンスをJSONとしてパース const result = await response.json(); // APIのレスポンス形式を SheetData 形式(二次元配列)に変換 return result.data as SheetData; }