excel_write_worksheet
Write data arrays to Excel worksheets for financial calculations, investment analysis, and automated reporting with precise cell positioning.
Instructions
Write data to a worksheet
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| data | Yes | ||
| startCol | No | ||
| startRow | No | ||
| worksheetName | Yes |
Implementation Reference
- src/tools/excel-tools.ts:136-171 (registration)Tool registration including name, description, input schema, and handler function for excel_write_worksheet{ name: "excel_write_worksheet", description: "Write data to a worksheet", inputSchema: { type: "object", properties: { worksheetName: { type: "string" }, data: { type: "array", items: { type: "array" } }, startRow: { type: "number", default: 1 }, startCol: { type: "number", default: 1 } }, required: ["worksheetName", "data"] }, handler: async (args: any): Promise<ToolResult> => { try { await excelManager.writeWorksheet( args.worksheetName, args.data, args.startRow || 1, args.startCol || 1 ); return { success: true, message: `Data written to ${args.worksheetName}` }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : String(error) }; } } },
- src/excel/excel-manager.ts:129-164 (handler)Core handler logic in ExcelManager that writes data to worksheet cells using ExcelJS, supports formulas, styles, creates worksheet if neededasync writeWorksheet(worksheetName: string, data: any[][], startRow: number = 1, startCol: number = 1): Promise<void> { if (!this.workbook) { throw new Error('No workbook is currently open'); } let worksheet = this.workbook.getWorksheet(worksheetName); if (!worksheet) { worksheet = this.workbook.addWorksheet(worksheetName); } data.forEach((row, rowIndex) => { row.forEach((cellData, colIndex) => { const cell = worksheet!.getCell(startRow + rowIndex, startCol + colIndex); if (typeof cellData === 'object' && cellData !== null && !(cellData instanceof Date)) { if (cellData.formula) { // When a formula is provided, set it and let Excel calculate the value cell.value = { formula: cellData.formula }; // Note: ExcelJS will automatically calculate the value when the file is opened } else if (cellData.value !== undefined) { cell.value = cellData.value; } if (cellData.style) { Object.assign(cell, { style: cellData.style }); } } else { cell.value = cellData; } }); }); // Force calculation of all formulas this.workbook.calcProperties = { fullCalcOnLoad: true // Ensure formulas are calculated when file is opened }; }
- src/index.ts:32-44 (registration)MCP server registers all tools including excelTools containing excel_write_worksheet via spread into allTools array used for listTools and callTool handlersconst allTools = [ ...excelTools, ...financialTools, ...rentalTools, ...expenseTools, ...reportingTools, ...cashFlowTools, ...taxTools, ...analyticsTools, ...chartTools, ...complianceTools, ...propertyTools, ];