Delete blank rows in spreadsheet using Google script

前端 未结 1 470
北海茫月
北海茫月 2021-01-26 07:37

Spreadsheet-1: Data present in Spreadsheet-1,

Name     apple  android   windows    linux
Germany    3      4         6          7
America    4      1         6           


        
相关标签:
1条回答
  • 2021-01-26 08:25

    This should work if all blanks are at the bottom of the source and there is nothing else below the data.

    function triggerOnTime() {
      var SpreadSheetKeyA = "MY key";
      var SpreadSheetKeyB = "MY key";
      var sheet1 = SpreadsheetApp.openById(SpreadSheetKeyA).getSheetByName("Source Name");
      var sheet2 = SpreadsheetApp.openById(SpreadSheetKeyB).getSheetByName("Target Name");
      var startRow = 5;
      var data = sheet1.getRange(startRow,11,sheet1.getLastRow() - startRow + 1,5).getValues();
      var time = new Date ().toJSON().slice(0,10);;
      for (var r = 0; r < data.length; r++) {
        data[r].unshift(time);
        }
      if(data.length > 0 ) {
        sheet2.insertRowsAfter(sheet2.getLastRow(), data.length);
        sheet2.getRange(sheet2.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
        }
    }
    

    If for whatever reason the above conditions cannot be met and you need to hardcode the number of rows to 40 you might try the following:

    function triggerOnTime() {
          var SpreadSheetKeyA = "MY key";
          var SpreadSheetKeyB = "MY key";
          var sheet1 = SpreadsheetApp.openById(SpreadSheetKeyA).getSheetByName("Source Name");
          var sheet2 = SpreadsheetApp.openById(SpreadSheetKeyB).getSheetByName("Target Name");
          var startRow = 5;
          var data = sheet1.getRange(5,11,40,6).getValues();
          var time = new Date ().toJSON().slice(0,10);;
          for(var i = data.length - 1; i > -1; i--) {
            if(data[i].join("").length == 0) data.splice(i, 1);
          }
          for (var r = 0; r < data.length; r++) {
            data[r].unshift(time);
            }
      if(data.length > 0 ) {
        sheet2.insertRowsAfter(sheet2.getLastRow(), data.length);
        sheet2.getRange(sheet2.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
        }
        }
    
    0 讨论(0)
提交回复
热议问题