Duplicate Rows in Google Spreadsheet based on Value

后端 未结 2 1972
隐瞒了意图╮
隐瞒了意图╮ 2021-01-03 08:10

I\'m trying to programatically duplicate rows in a Google spreadsheet. I would like the number of times the row is duplicated to be based on one of the values in the row its

2条回答
  •  孤城傲影
    2021-01-03 09:03

    This is fairly simple and you should have tried it yourself. I'm sure that when you'll read the code below you'll say "Oh, of course, I could have done it easily"... right ?

    function autoDup() {
      var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
      var data = sheet.getDataRange().getValues();
      var newData = [];
      for(var n in data){
        newData.push(data[n]);
        if(!Number(data[n][2])){continue};// if column 3 is not a number then do nothing
        for(var c=1 ; c < Number(data[n][2]) ; c++){ // start from 1 instead of 0 because we have already 1 copy
          newData.push(data[n]);//store values
        }
      }
      sheet.getRange(1,1,newData.length,newData[0].length).setValues(newData);// write new data to sheet, overwriting old data
    }
    

提交回复
热议问题