Project Files
dist / tools / sheets.js
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSheetTools = getSheetTools;
const sdk_1 = require("@lmstudio/sdk");
const zod_1 = require("zod");
const exceljs_1 = __importDefault(require("exceljs"));
const workbook_1 = require("../utils/workbook");
const config_1 = require("../utils/config");
function getSheetTools() {
return [
// ── 13. excel_list_sheets ────────────────────────────────────────────────
(0, sdk_1.tool)({
name: "excel_list_sheets",
description: "List all sheets in a workbook with row counts, column counts, and cell ranges.",
parameters: { filePath: zod_1.z.string() },
implementation: async ({ filePath }) => {
const err = (0, config_1.checkReadable)(filePath);
if (err)
return `Error: ${err}`;
try {
const wb = await (0, workbook_1.loadWorkbook)(filePath);
const info = wb.worksheets.map(ws => {
const rows = (0, workbook_1.worksheetToJson)(ws);
return { name: ws.name, rows: rows.length,
columns: rows[0] ? Object.keys(rows[0]).length : 0,
column_names: rows[0] ? Object.keys(rows[0]) : [],
};
});
return JSON.stringify({ file: filePath, sheet_count: info.length, sheets: info }, null, 2);
}
catch (e) {
return `Error: ${e}`;
}
},
}),
// ── 14. excel_create_sheet ───────────────────────────────────────────────
(0, sdk_1.tool)({
name: "excel_create_sheet",
description: "Add a new sheet to an existing workbook. Optionally populate with initial data and set its tab position.",
parameters: {
filePath: zod_1.z.string(),
sheetName: zod_1.z.string(),
data: zod_1.z.array(zod_1.z.record(zod_1.z.union([zod_1.z.string(), zod_1.z.number(), zod_1.z.null()]))).optional(),
position: zod_1.z.number().optional().describe("Tab position index, 0-based (default: append at end)"),
},
implementation: async ({ filePath, sheetName, data, position }) => {
const err = (0, config_1.checkReadable)(filePath);
if (err)
return `Error: ${err}`;
try {
const wb = await (0, workbook_1.loadWorkbook)(filePath);
if (wb.getWorksheet(sheetName))
return `Error: Sheet "${sheetName}" already exists`;
const ws = wb.addWorksheet(sheetName);
if (data?.length)
(0, workbook_1.jsonToWorksheet)(ws, data);
// Reorder if position given
if (position !== undefined) {
const names = wb.worksheets.map(s => s.name).filter(n => n !== sheetName);
names.splice(Math.max(0, position), 0, sheetName);
wb._worksheets = names.map(n => wb.getWorksheet(n));
}
await (0, workbook_1.saveWorkbook)(wb, filePath);
return JSON.stringify({ success: true, file: filePath, sheet_created: sheetName,
position: wb.worksheets.findIndex(s => s.name === sheetName),
rows_written: data?.length ?? 0, all_sheets: wb.worksheets.map(s => s.name) });
}
catch (e) {
return `Error: ${e}`;
}
},
}),
// ── 15. excel_delete_sheet ───────────────────────────────────────────────
(0, sdk_1.tool)({
name: "excel_delete_sheet",
description: "Delete a sheet from a workbook by name. Cannot delete the last remaining sheet.",
parameters: {
filePath: zod_1.z.string(),
sheetName: zod_1.z.string(),
},
implementation: async ({ filePath, sheetName }) => {
const err = (0, config_1.checkReadable)(filePath);
if (err)
return `Error: ${err}`;
try {
(0, config_1.backupFile)(filePath);
const wb = await (0, workbook_1.loadWorkbook)(filePath);
if (!wb.getWorksheet(sheetName))
return `Error: Sheet "${sheetName}" not found. Available: ${wb.worksheets.map(s => s.name).join(", ")}`;
if (wb.worksheets.length === 1)
return "Error: Cannot delete the only sheet in a workbook";
wb.removeWorksheet(wb.getWorksheet(sheetName).id);
await (0, workbook_1.saveWorkbook)(wb, filePath);
return JSON.stringify({ success: true, file: filePath, sheet_deleted: sheetName,
remaining_sheets: wb.worksheets.map(s => s.name) });
}
catch (e) {
return `Error: ${e}`;
}
},
}),
// ── 16. excel_rename_sheet ───────────────────────────────────────────────
(0, sdk_1.tool)({
name: "excel_rename_sheet",
description: "Rename a sheet. Sheet names must be ≤31 characters and unique within the workbook.",
parameters: {
filePath: zod_1.z.string(),
oldName: zod_1.z.string(),
newName: zod_1.z.string(),
},
implementation: async ({ filePath, oldName, newName }) => {
const err = (0, config_1.checkReadable)(filePath);
if (err)
return `Error: ${err}`;
if (newName.length > 31)
return "Error: Sheet name must be ≤31 characters";
try {
const wb = await (0, workbook_1.loadWorkbook)(filePath);
const ws = wb.getWorksheet(oldName);
if (!ws)
return `Error: Sheet "${oldName}" not found. Available: ${wb.worksheets.map(s => s.name).join(", ")}`;
if (wb.getWorksheet(newName))
return `Error: Sheet "${newName}" already exists`;
ws.name = newName;
await (0, workbook_1.saveWorkbook)(wb, filePath);
return JSON.stringify({ success: true, file: filePath,
renamed: { from: oldName, to: newName }, all_sheets: wb.worksheets.map(s => s.name) });
}
catch (e) {
return `Error: ${e}`;
}
},
}),
// ── 17. excel_copy_sheet ─────────────────────────────────────────────────
(0, sdk_1.tool)({
name: "excel_copy_sheet",
description: "Duplicate a sheet within the same workbook or copy it to another file.",
parameters: {
filePath: zod_1.z.string(),
sheetName: zod_1.z.string(),
newSheetName: zod_1.z.string(),
targetFile: zod_1.z.string().optional().describe("Target .xlsx path (default: same file)"),
},
implementation: async ({ filePath, sheetName, newSheetName, targetFile }) => {
const err = (0, config_1.checkReadable)(filePath);
if (err)
return `Error: ${err}`;
try {
const srcWb = await (0, workbook_1.loadWorkbook)(filePath);
const srcWs = srcWb.getWorksheet(sheetName);
if (!srcWs)
return `Error: Sheet "${sheetName}" not found`;
const rows = (0, workbook_1.worksheetToJson)(srcWs);
const destPath = targetFile ?? filePath;
const destWb = targetFile ? new exceljs_1.default.Workbook() : srcWb;
if (destWb.getWorksheet(newSheetName))
return `Error: Sheet "${newSheetName}" already exists in destination`;
const newWs = destWb.addWorksheet(newSheetName);
(0, workbook_1.jsonToWorksheet)(newWs, rows);
await (0, workbook_1.saveWorkbook)(destWb, destPath);
return JSON.stringify({ success: true,
source: { file: filePath, sheet: sheetName },
destination: { file: destPath, sheet: newSheetName },
rows_copied: rows.length, all_sheets: destWb.worksheets.map(s => s.name) });
}
catch (e) {
return `Error: ${e}`;
}
},
}),
// ── 18. excel_move_sheet ─────────────────────────────────────────────────
(0, sdk_1.tool)({
name: "excel_move_sheet",
description: "Change the tab position of a sheet (reorder tabs).",
parameters: {
filePath: zod_1.z.string(),
sheetName: zod_1.z.string(),
position: zod_1.z.number().describe("Target position, 0-based"),
},
implementation: async ({ filePath, sheetName, position }) => {
const err = (0, config_1.checkReadable)(filePath);
if (err)
return `Error: ${err}`;
try {
const wb = await (0, workbook_1.loadWorkbook)(filePath);
if (!wb.getWorksheet(sheetName))
return `Error: Sheet "${sheetName}" not found. Available: ${wb.worksheets.map(s => s.name).join(", ")}`;
const sheets = wb.worksheets.map(s => ({ name: s.name, data: (0, workbook_1.worksheetToJson)(s) }));
const idx = sheets.findIndex(s => s.name === sheetName);
const [moved] = sheets.splice(idx, 1);
sheets.splice(Math.max(0, Math.min(position, sheets.length)), 0, moved);
// Rebuild workbook in new order
const newWb = new exceljs_1.default.Workbook();
for (const { name, data } of sheets) {
const ws = newWb.addWorksheet(name);
if (data.length)
(0, workbook_1.jsonToWorksheet)(ws, data);
}
await (0, workbook_1.saveWorkbook)(newWb, filePath);
return JSON.stringify({ success: true, file: filePath, sheet: sheetName,
new_position: position, all_sheets: newWb.worksheets.map(s => s.name) });
}
catch (e) {
return `Error: ${e}`;
}
},
}),
// ── 19. excel_copy_rows_between_sheets ───────────────────────────────────
(0, sdk_1.tool)({
name: "excel_copy_rows_between_sheets",
description: "Copy or move rows from one sheet to another in the same workbook. Optional filter to select which rows to transfer.",
parameters: {
filePath: zod_1.z.string(),
sourceSheet: zod_1.z.string(),
targetSheet: zod_1.z.string(),
filters: zod_1.z.array(zod_1.z.object({
column: zod_1.z.string(),
operator: zod_1.z.enum(["=", "!=", ">", "<", ">=", "<=", "contains", "startsWith", "endsWith", "regex", "isNull", "isNotNull"]),
value: zod_1.z.union([zod_1.z.string(), zod_1.z.number()]).optional(),
})).optional().describe("Filter conditions — omit to transfer all rows"),
move: zod_1.z.boolean().optional().describe("If true, delete transferred rows from source (default: false)"),
},
implementation: async ({ filePath, sourceSheet, targetSheet, filters, move }) => {
const err = (0, config_1.checkReadable)(filePath);
if (err)
return `Error: ${err}`;
try {
(0, config_1.backupFile)(filePath);
const wb = await (0, workbook_1.loadWorkbook)(filePath);
const srcWs = (0, workbook_1.getSheet)(wb, sourceSheet);
if (typeof srcWs === "string")
return `Error: ${srcWs}`;
const tgtWs = (0, workbook_1.getSheet)(wb, targetSheet);
if (typeof tgtWs === "string")
return `Error: ${tgtWs}. Create it first with excel_create_sheet`;
const srcRows = (0, workbook_1.worksheetToJson)(srcWs);
const transfer = filters?.length
? srcRows.filter(r => (0, workbook_1.matchesFilters)(r, filters))
: srcRows;
if (!transfer.length)
return JSON.stringify({ success: true, rows_transferred: 0, message: "No rows matched" });
const tgtRows = (0, workbook_1.worksheetToJson)(tgtWs);
const merged = [...tgtRows, ...transfer];
tgtWs.spliceRows(1, tgtWs.rowCount);
(0, workbook_1.jsonToWorksheet)(tgtWs, merged);
if (move) {
const remaining = filters?.length ? srcRows.filter(r => !(0, workbook_1.matchesFilters)(r, filters)) : [];
srcWs.spliceRows(1, srcWs.rowCount);
if (remaining.length)
(0, workbook_1.jsonToWorksheet)(srcWs, remaining);
}
await (0, workbook_1.saveWorkbook)(wb, filePath);
return JSON.stringify({ success: true, file: filePath,
operation: move ? "move" : "copy",
source: { sheet: sourceSheet, rows_after: move ? srcRows.length - transfer.length : srcRows.length },
target: { sheet: targetSheet, rows_after: merged.length },
rows_transferred: transfer.length, preview: (0, workbook_1.preview)(transfer) });
}
catch (e) {
return `Error: ${e}`;
}
},
}),
];
}
//# sourceMappingURL=sheets.js.map