3

我需要使用电源自动化 + Office 脚本从一个 Excel 工作表中复制数据并粘贴(仅限值)到另一个工作表上

我开始使用下面链接中的答案创建一个流程。

Power Automate:将 Excel OneDrive 表复制到另一个 Excel OneDrive 表的底部

问题是我不理解第二个脚本,所以我无法将它修改为我需要的内容(工作簿末尾的那个粘贴)

链接上的脚本

For Run script I have

function main(workbook: ExcelScript.Workbook) {
  const sheet = workbook.getWorksheets()[0];
  let lastRow = sheet.getUsedRange(true).getLastCell().getRowIndex() + 1;
  let rng = "A3:P" + lastRow
  let tableTest = sheet.getRange(rng).getValues();
  console.log(tableTest);
}
Then under Compose

@{outputs('Run_script')?['body']?['Logs'][0]}
Then Initialize the "RemoveString" variable

@{split(outputs('Compose'),' ')[0]}
Then Initialize the "NewString" variable

@{replace(outputs('Compose'),variables('RemoveString'),'')}
Then Run Script 2 and add "NewString" as the parameter.

function main(workbook: ExcelScript.Workbook, rangeTest: string) {
  let table = workbook.getTable("BacklogTable");
  let str = rangeTest;
  let testerTest = JSON.parse(str);
  table.addRows(null, testerTest);
}

RemoveString 的原因是从输出中删除日期和时间戳

4

2 回答 2

2

这需要一些不同的工作流程。

运行脚本

function main(workbook: ExcelScript.Workbook) {
  const sheet = workbook.getWorksheets()[0];
  let lastRow = sheet.getUsedRange(true).getLastCell().getRowIndex() + 1;
  let rng = "A2:C" + lastRow
  let tableTest = sheet.getRange(rng).getValues();
  console.log(tableTest);
  console.log(tableTest.length)
}

撰写

@{outputs('Run_script')?['body']?['Logs'][0]}

撰写 2

@{outputs('Run_script')?['body']?['Logs'][1]}

删除字符串

@{split(outputs('Compose'),' ')[0]}

新字符串

@{replace(outputs('Compose'),variables('RemoveString'),'')}

删除字符串2

@{split(outputs('Compose_2'),' ')[0]}

新字符串2

@{int(replace(outputs('Compose_2'),variables('RemoveString2'),''))}

编号

@{int(variables('NewString2'))}

运行脚本 2

function main(workbook: ExcelScript.Workbook, rangeTest: string, length: number) {
  let str = rangeTest;
  const arr = JSON.parse(str);
  let sheet = workbook.getWorksheet("Sheet2");
  let rng = "A7:C" + (6 + length); //Change C to whichever column you want to end on
  sheet.getRange(rng).setValues(arr);
  sheet.getRange(rng).setNumberFormatLocal("0.00");
}

在此处输入图像描述

在此处输入图像描述

于 2021-04-22T00:54:10.687 回答
1

我可能没有正确遵循这一点,但您也可以返回值并将它们传递给 Power Automate 中的另一个连接器。以下是有关如何使用 Office 脚本返回值的一些文档。此外,下面是一个带有参数和数字类型返回值的示例脚本。通过返回值而不是 console.logging 它们,您无需在 Flow 步骤中删除任何输出。如果您有任何问题,请告诉我!

function main(
  workbook: ExcelScript.Workbook,
  issueId: string,
  issueTitle: string): number {
  // Get the "GitHub" worksheet.
  let worksheet = workbook.getWorksheet("GitHub");

  // Get the first table in this worksheet, which contains the table of GitHub issues.
  let issueTable = worksheet.getTables()[0];

  // Add the issue ID and issue title as a row.
  issueTable.addRow(-1, [issueId, issueTitle]);

  // Return the number of rows in the table, which represents how many issues are assigned to this user.
  return issueTable.getRangeBetweenHeaderAndTotal().getRowCount();
}
于 2021-04-23T20:12:28.010 回答