Skip to content Skip to sidebar Skip to footer

Delete Blank Rows In Spreadsheet Using Google Script

Spreadsheet-1: Data present in Spreadsheet-1, Name apple android windows linux Germany 3 4 6 7 America 4 1 6 2 Sweden

Solution 1:

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;
  vardata = 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;
      vardata = 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);
    }
    }

Post a Comment for "Delete Blank Rows In Spreadsheet Using Google Script"