getSpreadsheet
Retrieve spreadsheet metadata and sheet listings from Google Sheets using a URL to access document structure and contents.
Instructions
スプレッドシートの基本情報と含まれるシート一覧を取得
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes | スプレッドシートのURL |
Implementation Reference
- src/tools/getSpreadsheet.ts:13-61 (handler)Complete definition of the getSpreadsheet tool, including schema, description, and the handler function that extracts the spreadsheet ID from the URL, fetches the spreadsheet information using helper functions, formats the result, and returns it as structured content or error.export const getSpreadsheetTool = { name: "getSpreadsheet", description: "スプレッドシートの基本情報と含まれるシート一覧を取得", schema: { url: z.string().describe("スプレッドシートのURL") }, handler: async ({ url }: { url: 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 formattedResult = formatSpreadsheetInfo(spreadsheetInfo); 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/server.ts:27-32 (registration)Registers the getSpreadsheet tool with the MCP server using the server.tool method.server.tool( getSpreadsheetTool.name, getSpreadsheetTool.description, getSpreadsheetTool.schema, getSpreadsheetTool.handler );
- src/api/spreadsheet.ts:47-70 (helper)Helper function that retrieves spreadsheet information by calling the Google Apps Script web app API (or mock data in mock mode). Called by the tool handler.export async function getSpreadsheetInfo(spreadsheetId: string): Promise<SpreadsheetInfo> { // モックモードの場合はモックデータを返す if (config.isMockMode) { return getMockSpreadsheetInfo(spreadsheetId); } // API呼び出し用のURLを生成 const apiUrl = new URL(config.gas.webAppUrl); apiUrl.searchParams.append('action', 'getSpreadsheet'); apiUrl.searchParams.append('id', spreadsheetId); apiUrl.searchParams.append('apiKey', config.gas.apiKey); // APIリクエスト const response = await fetch(apiUrl); // ステータスコードのチェック if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.message || `API Error: ${response.status}`); } // レスポンスをJSONとしてパース return await response.json() as SpreadsheetInfo; }
- src/api/spreadsheet.ts:14-40 (helper)Helper function to extract the spreadsheet ID from a Google Sheets URL using regex. Used in the tool handler.export function extractSpreadsheetId(url: string): string | null { try { // URL自体がnullまたはundefinedの場合 if (!url) { return null; } // 実際のGoogle SpreadsheetのURLからIDを抽出 // 例: https://docs.google.com/spreadsheets/d/1a2b3c4d5e6f7g8h9i0j/edit#gid=0 const regex = /\/d\/([a-zA-Z0-9_-]+)/; const match = url.match(regex); if (match && match[1]) { return match[1]; } // モック用に特殊なURLも処理 if (config.isMockMode && (url.includes("budget") || url.includes("project"))) { return url.includes("budget") ? "mock_budget" : "mock_project"; } return null; } catch (error) { console.error("URL解析エラー:", error); return null; } }