diff --git a/src/main/capabilities/office-aliases.test.ts b/src/main/capabilities/office-aliases.test.ts index 3e8f33c..36d7444 100644 --- a/src/main/capabilities/office-aliases.test.ts +++ b/src/main/capabilities/office-aliases.test.ts @@ -49,4 +49,12 @@ describe("common Excel aliases", () => { const created = await run("Excel_NewExcel", { filePath: directory, name: "open-me.xlsx" }) as { path: string }; await expect(run("Excel_OpenExcel", { filePath: created.path })).resolves.toMatchObject({ path: created.path }); }); + + it("writes a range through the Excel_WriteRange catalog keys (objExcel/cellPos/data/isSave)", async () => { + const directory = await mkdtemp(path.join(os.tmpdir(), "rpa-excel-range-")); dirs.push(directory); + const created = await run("Excel_NewExcel", { filePath: directory, name: "range.xlsx" }) as { path: string }; + await run("Excel_WriteRange", { objExcel: created, sheet: "Sheet1", cellPos: "B2", data: [[1, "壹"], [2, "贰"]], isSave: "True" }); + const reopened = await run("Excel_OpenExcel", { filePath: created.path }) as { id: string }; + await expect(run("Excel_Cs_ReadContentV2", { workbook: reopened, sheet: "Sheet1", excelRng: "B2:C3" })).resolves.toEqual([[1, "壹"], [2, "贰"]]); + }); }); diff --git a/src/main/capabilities/office.ts b/src/main/capabilities/office.ts index 388308d..5a8ed11 100644 --- a/src/main/capabilities/office.ts +++ b/src/main/capabilities/office.ts @@ -153,14 +153,18 @@ const writeCell: CapabilityHandler = async (_ctx, node, args) => { const writeRange: CapabilityHandler = async (_ctx, node, args) => { const { handle, workbook } = await resolveWorkbook(args, node.id, node.componentId); const sheet = sheetOf(workbook, text(args, ["sheet", "sheetName"])); - const start = `${text(args, ["column"], "A")}${text(args, ["row"], "1")}`; + const cellPos = text(args, ["cellPos"]); + const cellMatch = cellPos.match(/^([A-Za-z]+)(\d+)$/); + const column = cellMatch ? cellMatch[1] : text(args, ["column"], "A"); + const row = cellMatch ? cellMatch[2] : text(args, ["row"], "1"); + const start = `${column}${row}`; const startCell = sheet.getCell(start); const startRow = startCell.row; const startColumn = startCell.col; - for (const [rowIndex, row] of rowsFrom(value(args, ["contents", "table"])).entries()) { + for (const [rowIndex, row] of rowsFrom(value(args, ["contents", "data", "table"])).entries()) { for (const [columnIndex, cell] of row.entries()) sheet.getCell(startRow + rowIndex, startColumn + columnIndex).value = cell as ExcelJS.CellValue; } - if (bool(value(args, ["save"]), true)) await workbook.xlsx.writeFile(handle.path); + if (bool(value(args, ["save", "isSave"]), true)) await workbook.xlsx.writeFile(handle.path); return handle; };