add_worksheet
Add a new worksheet to an existing Excel workbook by specifying the file path and sheet name using the Excel MCP Server tool.
Instructions
既存のワークブックにワークシートを追加します
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | 対象のExcelファイルの絶対パス。既存のファイルである必要があります | |
| sheetName | Yes | 作成するワークシート名。英数字、日本語、アンダースコア、ハイフンが使用可能です |
Implementation Reference
- src/index.ts:25-28 (schema)Zod schema defining the input parameters for the add_worksheet tool: filePath and sheetName.const AddWorksheetSchema = z.object({ filePath: z.string().describe("対象のExcelファイルの絶対パス。既存のファイルである必要があります"), sheetName: z.string().describe("作成するワークシート名。英数字、日本語、アンダースコア、ハイフンが使用可能です"), });
- src/index.ts:207-228 (handler)Core handler function that loads the Excel workbook, checks for existing sheet, adds new worksheet using ExcelJS, saves the file, and returns confirmation.async function addWorksheet(filePath: string, sheetName: string): Promise<string> { try { if (!sheetName || sheetName.trim() === '') { throw new Error("ワークシート名が空です"); } const workbook = await loadWorkbook(filePath); // 既存のシート名と重複チェック const existingSheet = workbook.getWorksheet(sheetName); if (existingSheet) { throw new Error(`ワークシート '${sheetName}' は既に存在します`); } workbook.addWorksheet(sheetName); await workbook.xlsx.writeFile(filePath); return `ワークシート '${sheetName}' を追加しました。`; } catch (error) { throw new McpError(ErrorCode.InternalError, `ワークシート追加エラー: ${error}`); } }
- src/index.ts:476-480 (registration)Tool registration in the listTools handler, defining name, description, and input schema.{ name: "add_worksheet", description: "既存のワークブックにワークシートを追加します", inputSchema: zodToJsonSchema(AddWorksheetSchema) },
- src/index.ts:535-538 (registration)Tool implementation wrapper in the dispatch map, which parses args with schema and delegates to the handler function.add_worksheet: async (args: any) => { const { filePath, sheetName } = AddWorksheetSchema.parse(args); return await addWorksheet(filePath, sheetName); },
- src/index.ts:194-204 (helper)Helper function to load and validate an Excel workbook, used by the addWorksheet handler.async function loadWorkbook(filePath: string): Promise<ExcelJS.Workbook> { try { validateFilePath(filePath); const workbook = new ExcelJS.Workbook(); await workbook.xlsx.readFile(filePath); return workbook; } catch (error) { throw new McpError(ErrorCode.InternalError, `ワークブック読み込みエラー: ${error}`); } }