Generate multiple copies of one row, given user input to define # of copies?

I need a way to generate a user defined number of copies of a given row. What is the best practice for this?

I've tried this method but it seems to not sync properly to the source sheet. https://www.youtube.com/watch?v=C0y-1cb8WbU&t=1s It's like it happens to fast or something, and then acts like it worked, but then later the rows vanish. Also it doesn't seem like best practice.

Perhaps someone has an AppScripts example where they iterate and push rows or something?

 

Solved Solved
0 2 171
1 ACCEPTED SOLUTION

I ended up creating a script:

function bulkCopy(numCopies, parentRow, parentID, table) {

  var ss = SpreadsheetApp.openById("your id here")

 
//table is the name of the table as a string, i.e. "myTable"
  var sheet = ss.getSheetByName(table);
  var lastCol = sheet.getLastColumn();

  // parentRow is [_RowNumber] of the row to copy
  var row = sheet.getRange(parentRow, 1, 1, lastCol).getValues()[0];

   

  for (let i = 0; i < numCopies; i++) {
    var copyArr = Array.from(row);
    var now = new Date();

//generate a uuid for my key column (the third row in)
   
copyArr[2] = Utilities.getUuid().replace(/-/g, "");
//create a QR link
    copyArr[3] = 'https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=' + copyArr[2];
//set time created
copyArr[18] = now;

    console.log(copyArr);
    var newRow = sheet.appendRow(copyArr);

    console.log('added row: ' + sheet.getLastColumn())
    console.log('new lot id: ' + copyArr[2])

  }

}

View solution in original post

2 REPLIES 2

One way is to use Bot/Webhook. The json would be something like this.. The [ROW] is a number column where you specify the qty.

{
"Action": "Add",
"Properties": {
"Locale": "en-US",
"Location": "48.220599, 16.239976",
"Timezone": "W. Europe Standard Time"
},
"Rows": [
<<Start: TOP(ORDERBY(Asset[ID],[_ROWNUMBER],FALSE),[_THISROW].[ROWS])>>
{
"ID": "<<UNIQUEID()>>",
"Data": "<<[_THISROW].[Data]>>",
"Name": "<<[_THISROW].[Name]>>",
},
<<End>>
]
}

I ended up creating a script:

function bulkCopy(numCopies, parentRow, parentID, table) {

  var ss = SpreadsheetApp.openById("your id here")

 
//table is the name of the table as a string, i.e. "myTable"
  var sheet = ss.getSheetByName(table);
  var lastCol = sheet.getLastColumn();

  // parentRow is [_RowNumber] of the row to copy
  var row = sheet.getRange(parentRow, 1, 1, lastCol).getValues()[0];

   

  for (let i = 0; i < numCopies; i++) {
    var copyArr = Array.from(row);
    var now = new Date();

//generate a uuid for my key column (the third row in)
   
copyArr[2] = Utilities.getUuid().replace(/-/g, "");
//create a QR link
    copyArr[3] = 'https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=' + copyArr[2];
//set time created
copyArr[18] = now;

    console.log(copyArr);
    var newRow = sheet.appendRow(copyArr);

    console.log('added row: ' + sheet.getLastColumn())
    console.log('new lot id: ' + copyArr[2])

  }

}