Search code examples
google-apps-scriptgoogle-sheetsrow

Google sheets script to add new rows with value by default


In a sheet, I use a script that deletes the empty rows and adds new ones at the bottom. Everything works fine but I would like to change one little thing.

All the columns of the new rows added do not contain formulas except for the L and U columns. These formulas would not be necessary if I could have " NO " values in the L and U columns of these new added rows.

So I would like that instead of adding the new row with formulas, add it with the value "NO" in the L and U columns.

https://docs.google.com/spreadsheets/d/1CPcMx3Dhbqi-zO4D3jYNxO-PGjyW3iTfRo5gRmEB9p4/edit#gid=0

function removeEmpty() {
  const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients');
  const empty_rows = [];
  const lastRow = sh.getLastRow()
  const data = sh.getRange("C6:G" + lastRow).getValues();
  for (var i in data) if (data[i].join('') == '') empty_rows.push(+i + 6);
  empty_rows.reverse().forEach(x => sh.deleteRow(x));
  sh.insertRowsAfter(lastRow - empty_rows.length, 5)

  var rng = sh.getRange('A7:Z7')
  rng.copyTo(sh.getRange('A' + (lastRow - empty_rows.length + 1) + ':Z' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);

  // L
  var rng = sh.getRange('L' + (lastRow - empty_rows.length) + ':L' + (lastRow - empty_rows.length))
  rng.copyTo(sh.getRange('L' + (lastRow - empty_rows.length + 1) + ':L' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
  // U
  var rng = sh.getRange('U' + (lastRow - empty_rows.length) + ':U' + (lastRow - empty_rows.length))
  rng.copyTo(sh.getRange('U' + (lastRow - empty_rows.length + 1) + ':U' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);

}

Solution

  • Finally I found by myself

    function removeEmpty() {
      const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients');
      const empty_rows = [];
      const lastRow = sh.getLastRow()
      const data = sh.getRange("C6:G" + lastRow).getValues();
      for (var i in data) if (data[i].join('') == '') empty_rows.push(+i + 6);
      empty_rows.reverse().forEach(x => sh.deleteRow(x));
      sh.insertRowsAfter(lastRow - empty_rows.length, 5)
    
      var rng = sh.getRange('A7:Z7')
      rng.copyTo(sh.getRange('A' + (lastRow - empty_rows.length + 1) + ':Z' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
    
      // L
      var rng = sh.getRange('L' + (lastRow - empty_rows.length) + ':L' + (lastRow - empty_rows.length))
      rng.copyTo(sh.getRange('L' + (lastRow - empty_rows.length + 1) + ':L' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients').getRange("L6:L").setValue('NON'));
      // U
      var rng = sh.getRange('U' + (lastRow - empty_rows.length) + ':U' + (lastRow - empty_rows.length))
      rng.copyTo(sh.getRange('U' + (lastRow - empty_rows.length + 1) + ':U' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients').getRange("U6:U").setValue('NON'));
    
    }