excel_tool
Read and write Excel and CSV files, and convert JSON data to spreadsheet formats for data management and analysis tasks.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| action | Yes | Action to perform: read, write, or convert_json_to_xlsx | |
| filePath | Yes | Absolute path to the input file | |
| outputFilePath | No | Absolute path to the output file (required for write and convert actions) | |
| format | Yes | File format: xlsx, csv | |
| data | No | Data to write (required for write action) | |
| options | No | Additional options |
Implementation Reference
- src/tools/excel_tool.ts:203-275 (handler)Main execution logic for the excel_tool, handling read, write, and convert_json_to_xlsx actions for XLSX and CSV files.export default async function (request: any) { try { const { action, filePath, outputFilePath, format, data, options } = request.params.arguments; // Validate file path if (!path.isAbsolute(filePath)) { throw new Error("Input filePath must be absolute"); } // Check if file exists for read action if (action === "read" && !fs.existsSync(filePath)) { throw new Error(`Error: File not found - ${filePath}`); } if ((action === "write" || action === "convert_json_to_xlsx") && !outputFilePath) { throw new Error("outputFilePath is required for write and convert actions"); } if (outputFilePath && !path.isAbsolute(outputFilePath)) { throw new Error("outputFilePath must be absolute"); } if (action === "read") { // Read file logic switch (format) { case "xlsx": return readXLSX({ filePath, format }); case "csv": return await readCSV({ filePath, format }); default: throw new Error(`Unsupported format for read: ${format}`); } } else if (action === "write") { // Write file logic if (!data) { throw new Error("Data is required for write action"); } switch (format) { case "xlsx": return writeXLSX({ filePath: outputFilePath, data, format, options }); case "csv": return await writeCSV({ filePath: outputFilePath, data, format, options }); default: throw new Error(`Unsupported format for write: ${format}`); } } else if (action === "convert_json_to_xlsx") { if (format !== 'xlsx') { throw new Error("convert_json_to_xlsx action only supports xlsx format"); } const fileContent = fs.readFileSync(filePath, 'utf-8'); const jsonData = JSON.parse(fileContent); if (!Array.isArray(jsonData)) { throw new Error("JSON content must be an array of objects."); } return writeXLSX({ filePath: outputFilePath, data: jsonData, format: 'xlsx', options: options }); } else { throw new Error(`Invalid action: ${action}`); } } catch (error: any) { // Error handling console.error(error); return { content: [ { type: "text", text: JSON.stringify(`Error: ${error instanceof Error ? error.message : String(error)}`, null, 2), }, ], isError: true }; } }
- src/tools/excel_tool.ts:7-55 (schema)Input schema for the excel_tool defining parameters such as action, filePath, outputFilePath, format, data, and options.export const schema = { name: "excel_tool", description: "Read and write Excel (xlsx) and CSV files, and convert from JSON.", type: "object", properties: { action: { type: "string", enum: ["read", "write", "convert_json_to_xlsx"], description: "Action to perform: read, write, or convert_json_to_xlsx" }, filePath: { type: "string", description: "Absolute path to the input file" }, outputFilePath: { type: "string", description: "Absolute path to the output file (required for write and convert actions)" }, format: { type: "string", enum: ["xlsx", "csv"], description: "File format: xlsx, csv" }, data: { type: "array", description: "Data to write (required for write action)", items: { type: "object", additionalProperties: true } }, options: { type: "object", description: "Additional options", properties: { sheetName: { type: "string", description: "Sheet name for Excel files" }, headerRow: { type: "boolean", description: "Include header row in output" } }, additionalProperties: true } }, required: ["action", "filePath", "format"] };