UNPKG

aspose.cells.mcp

Version:

Excel MCP - AI-powered Excel automation server for Excel AI, Excel Formula MCP, spreadsheets MCP, and Excel automation using Aspose.Cells for Node.js

133 lines (117 loc) 3.71 kB
/** * Create Workbook and worksheet Service * MCP service for creating new Excel workbooks or worksheet */ import { getAspose, getAbsolutePath, ensureDirectoryExists, handleExcelFileError } from '../core/utils.js'; interface CreateWorkbookArgs { filepath: string; sheet_name?: string; } interface CreateWorksheetArgs { filepath: string; sheet_name: string; } interface ServiceResponse { content: Array<{ type: 'text'; text: string; }>; [key: string]: unknown; } export const createWorkbookService = { // Tool definition definition: { name: 'create_workbook', description: 'Create new Excel workbook (.xlsx). Use when user wants to create a new Excel file from scratch.', inputSchema: { type: 'object', properties: { filepath: { type: 'string', description: 'Path for new Excel file (should end with .xlsx)', }, sheet_name: { type: 'string', description: 'Name of initial worksheet (optional, default: "Sheet1")', default: 'Sheet1', }, }, required: ['filepath'], }, }, // Implementation async handler(args: CreateWorkbookArgs): Promise<ServiceResponse> { let fullPath: string = ''; try { const AsposeCells = getAspose(); fullPath = getAbsolutePath(args.filepath); ensureDirectoryExists(fullPath); const workbook = new AsposeCells.Workbook(); // Rename the default sheet const sheet = workbook.getWorksheets().get(0); sheet.setName(args.sheet_name || 'Sheet1'); workbook.save(fullPath); return { content: [ { type: 'text', text: `Created workbook at: ${fullPath}`, }, ], }; } catch (error) { const err = error instanceof Error ? error : new Error(String(error)); handleExcelFileError(err, fullPath || args.filepath, args.filepath, 'creating workbook'); } } }; export const createWorksheetService = { // Tool definition definition: { name: 'create_worksheet', description: 'Create a new worksheet in an existing workbook', inputSchema: { type: 'object', properties: { filepath: { type: 'string', description: 'Path to the Excel file', }, sheet_name: { type: 'string', description: 'Name of the new worksheet', }, }, required: ['filepath', 'sheet_name'], }, }, // Implementation async handler(args: CreateWorksheetArgs): Promise<ServiceResponse> { let fullPath: string = ''; try { const AsposeCells = getAspose(); fullPath = getAbsolutePath(args.filepath); const workbook = new AsposeCells.Workbook(fullPath); const worksheets = workbook.getWorksheets(); // Check if sheet already exists for (let i = 0; i < worksheets.getCount(); i++) { if (worksheets.get(i).getName() === args.sheet_name) { throw new Error(`Sheet "${args.sheet_name}" already exists`); } } worksheets.add(args.sheet_name); workbook.save(fullPath); return { content: [ { type: 'text', text: `Created worksheet "${args.sheet_name}" in ${fullPath}`, }, ], }; } catch (error) { const err = error instanceof Error ? error : new Error(String(error)); handleExcelFileError(err, fullPath || args.filepath, args.filepath, 'creating worksheet'); } } };