fix: Excel_SaveAs 支持 filename/isAbsolutePath 字段并处理相对路径
This commit is contained in:
parent
12cf94da27
commit
97fda1696a
@ -65,4 +65,21 @@ describe("common Excel aliases", () => {
|
|||||||
const reopened = await run("Excel_OpenExcel", { filePath: created.path }) as { id: string };
|
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, "贰"]]);
|
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"));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -178,9 +178,11 @@ const saveExcel: CapabilityHandler = async (_ctx, node, args) => {
|
|||||||
|
|
||||||
const saveAsExcel: CapabilityHandler = async (_ctx, node, args) => {
|
const saveAsExcel: CapabilityHandler = async (_ctx, node, args) => {
|
||||||
const { handle, workbook } = await resolveWorkbook(args, node.id, node.componentId);
|
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;
|
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 fs.mkdir(path.dirname(handle.path), { recursive: true });
|
||||||
await workbook.xlsx.writeFile(handle.path);
|
await workbook.xlsx.writeFile(handle.path);
|
||||||
// 另存后同一实例以新路径登记,旧路径的缓存不再代表最新内容,释放之
|
// 另存后同一实例以新路径登记,旧路径的缓存不再代表最新内容,释放之
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user