diff --git a/src/main/capabilities/office-aliases.test.ts b/src/main/capabilities/office-aliases.test.ts index 454c431..2f5228d 100644 --- a/src/main/capabilities/office-aliases.test.ts +++ b/src/main/capabilities/office-aliases.test.ts @@ -65,4 +65,21 @@ describe("common Excel aliases", () => { 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, "贰"]]); }); + + it("saves as through the Excel_SaveAs catalog keys (filename/isAbsolutePath)", async () => { + const directory = await mkdtemp(path.join(os.tmpdir(), "rpa-excel-saveas-")); dirs.push(directory); + const created = await run("Excel_NewExcel", { filePath: directory, name: "origin.xlsx" }) as { path: string }; + await run("Excel_WriteCell", { workbook: created, sheet: "Sheet1", row: 1, column: "A", content: "saveas" }); + const savedAs = await run("Excel_SaveAs", { objExcel: created, filename: path.join(directory, "legacy-saveas.xlsx"), isAbsolutePath: "True" }) as { path: string }; + expect(savedAs.path).toBe(path.resolve(directory, "legacy-saveas.xlsx")); + const reopened = await run("Excel_OpenExcel", { filePath: savedAs.path }) as { id: string }; + await expect(run("Excel_Cs_ReadContentV2", { workbook: reopened, sheet: "Sheet1", excelRng: "A1" })).resolves.toEqual([["saveas"]]); + }); + + it("resolves a relative Excel_SaveAs filename against the original workbook directory", async () => { + const directory = await mkdtemp(path.join(os.tmpdir(), "rpa-excel-saveas-rel-")); dirs.push(directory); + const created = await run("Excel_NewExcel", { filePath: directory, name: "rel-origin.xlsx" }) as { path: string }; + const savedAs = await run("Excel_SaveAs", { objExcel: created, filename: "rel-target.xlsx", isAbsolutePath: "False" }) as { path: string }; + expect(savedAs.path).toBe(path.resolve(directory, "rel-target.xlsx")); + }); }); diff --git a/src/main/capabilities/office.ts b/src/main/capabilities/office.ts index 7532b48..134b06d 100644 --- a/src/main/capabilities/office.ts +++ b/src/main/capabilities/office.ts @@ -178,9 +178,11 @@ const saveExcel: CapabilityHandler = async (_ctx, node, args) => { const saveAsExcel: CapabilityHandler = async (_ctx, node, args) => { const { handle, workbook } = await resolveWorkbook(args, node.id, node.componentId); - const target = required(value(args, ["path", "file", "savePath"]), "另存为路径", node.id, node.componentId); + const raw = required(value(args, ["path", "file", "savePath", "filename", "saveFilePath"]), "另存为路径", node.id, node.componentId); + // Legacy Excel_SaveAs 用 isAbsolutePath 标记 filename 是否为绝对路径,相对路径基于原文件目录 + const absolute = bool(value(args, ["isAbsolutePath"]), true); const previousPath = handle.path; - handle.path = path.resolve(target); + handle.path = absolute ? path.resolve(raw) : path.resolve(path.dirname(previousPath), raw); await fs.mkdir(path.dirname(handle.path), { recursive: true }); await workbook.xlsx.writeFile(handle.path); // 另存后同一实例以新路径登记,旧路径的缓存不再代表最新内容,释放之