0

In a table (in excel) in a column I have some number(A).

I want the flow to take that number (A) and to create number of rows equels to Number (A)

For example if number(A) is 4, then in another table to be added 4 rows

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • You need to provide an example of what the source and destination tables look like. Without that, it's not entirely clear. Can you please provide some screenshots or something of that nature? – Skin Jan 09 '22 at 09:28

2 Answers2

0

I've made an assumption on the source and destination tables. This concept can be adjusted and applied to suit your own scenario.

I'd be using Office Scripts to do this. If you've never used it then feel free to consult the Microsoft documentation to get you going ...

https://learn.microsoft.com/en-us/office/dev/scripts/tutorials/excel-tutorial

This is the script you need to create (change the name of your tables accordingly) ...

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();
    }
  }
}

You can then call that from PowerAutomate using the Run script action under Excel Online (Business) ...

Run script

You can use that approach or all of the actions that are available in PowerAutomate which will achieve the same sort of thing.

Result

IMO, Using Office Scripts is much easier. Creating a large flow can be a real pain in the backside to deal with given there'll be a whole heap of actions that you'll need to throw in to reach the same outcome.

Skin
  • 9,085
  • 2
  • 13
  • 29
  • Instead of using your while loop at the end, you should be able to just use a JSON string and add that to the table after it's parsed. With that, you should only need to call addRows once. You can see how to do that in my post on the thread. – Brian Gonzalez Jan 10 '22 at 18:25
  • @BrianGonzalez, I'm aware of that approach, to be honest, I just couldn't be bothered given there's little to no value when this is going run in the background. I only see an issue if they request 1000's of rows at once. I made my assumption and that's the way I went. – Skin Jan 10 '22 at 21:44
0

I would pass the number of rows to add in an office scripts script as a parameter. Once you have the value, create a JSON string of a 2d array. You want to create a loop using the number of rows to add. In the loop you continue to concatenate the 2d array. Once you've exited the loop, parse the JSON string and add the 2d array to the table. You can see how you code might look below:

    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)
    }
Brian Gonzalez
  • 1,178
  • 1
  • 3
  • 15