【发布时间】:2022-01-08 13:56:05
【问题描述】:
有人可以帮忙完成这项任务吗:
在表格(在 excel 中)的列中,我有一些数字(A)。
我希望流程采用该数字 (A) 并创建等于数字 (A) 的行数
例如如果number(A)是4,那么在另一个表中要添加4行
谢谢
【问题讨论】:
-
您需要提供源表和目标表的示例。没有它,它并不完全清楚。你能提供一些截图或类似的东西吗?
标签: excel power-automate flow
有人可以帮忙完成这项任务吗:
在表格(在 excel 中)的列中,我有一些数字(A)。
我希望流程采用该数字 (A) 并创建等于数字 (A) 的行数
例如如果number(A)是4,那么在另一个表中要添加4行
谢谢
【问题讨论】:
标签: excel power-automate flow
我对源表和目标表做了假设。可以调整和应用此概念以适合您自己的场景。
我将使用 Office 脚本来执行此操作。如果您从未使用过它,请随时查阅 Microsoft 文档以帮助您开始...
https://docs.microsoft.com/en-us/office/dev/scripts/tutorials/excel-tutorial
这是您需要创建的脚本(相应地更改表的名称)...
function main(workbook: ExcelScript.Workbook)
{
var addRowsTable = workbook.getTable('TableRowsToAdd');
var addRowsToTable = workbook.getTable('TableAddRowsToTable');
var addRowsTableDataRange = addRowsTable.getRangeBetweenHeaderAndTotal();
var addRowsTableDataRangeValues = addRowsTableDataRange.getValues();
// Sum the values so we can determine how many more rows need to be added
// to the destination table.
var sumOfAllRowsToBeInExistence = 0;
for (var i = 0; i < addRowsTableDataRangeValues.length; i++) {
if (!isNaN(addRowsTableDataRangeValues[i][0])) {
sumOfAllRowsToBeInExistence += Number(addRowsTableDataRangeValues[i][0]);
}
}
var currentRowCount = addRowsToTable.getRangeBetweenHeaderAndTotal().getRowCount();
var rowsToAdd = sumOfAllRowsToBeInExistence - currentRowCount;
console.log(`Current row count = ${currentRowCount}`);
console.log(`Rows to add = ${rowsToAdd}`);
if (rowsToAdd > 0) {
/*
The approach below is contentious given the performance impact but this approach ...
for (var i = 1; i <= rowsToAdd; i++) {
... didn't always yield the correct result. May be a bug but needs investigation.
Ultimately, there are a few ways to achieve the same result, like using the resize method.
This was the easiest option for a StackOverflow answer.
*/
while (addRowsToTable.getRangeBetweenHeaderAndTotal().getRowCount() <
sumOfAllRowsToBeInExistence) {
addRowsToTable.addRows();
}
}
}
然后,您可以使用Run script 下的Excel Online (Business) 操作从 PowerAutomate 调用它...
您可以使用该方法或 PowerAutomate 中可用的所有操作来实现相同的目的。
IMO,使用 Office 脚本要容易得多。考虑到您需要投入一大堆操作才能达到相同的结果,因此创建一个大流程可能是一个真正的痛苦。
【讨论】:
我会将要添加到 office 脚本脚本中的行数作为参数传递。获得值后,创建一个二维数组的 JSON 字符串。您想使用要添加的行数创建一个循环。在循环中,您继续连接二维数组。退出循环后,解析 JSON 字符串并将二维数组添加到表中。您可以在下面看到您的代码的外观:
function main(workbook: ExcelScript.Workbook, rowsToAdd: number)
{
//set table name
let tbl = workbook.getTable("table2")
//initialize json string with open bracket
let jsonArrString = "["
//set the temp json string with a 2d array
let tempJsonArr = '["",""],'
//concatenate json string equal to the number of rows to add
for (let i = 0; i < rowsToAdd; i++){
jsonArrString += tempJsonArr
}
//remove extra comma from JSON string
jsonArrString = jsonArrString.slice(0, jsonArrString.length-1)
//add closing bracket to JSON string
jsonArrString += "]"
//parse json string into array
let jsonArr: string[][] = JSON.parse(jsonArrString)
//add array to table to add the number of rows
tbl.addRows(null,jsonArr)
}
【讨论】: