Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

  • Column C, from row 6 and onwards, is entirely filled with checkboxes.
  • I want to delete all rows for which the box in column C of that row is checked.
  • The name of this sheet is 'Today' and is in the variable of the same name below.

This is what I have so far, which does not successfully run and I cannot identify why:

    var values = Today.getRange('C6:C').getValues();
    var a = 0;
      while (a<=values.length){
        if ( values[a][0] == "True" ) {   
          Today.deleteRow(a+6);           //Here I add 6 to 'a' since the range 'values' started from row 6.
        }
      a++    //Increase 'a' by 1 and continue.
      }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
247 views
Welcome To Ask or Share your Answers For Others

1 Answer

  • You want to delete the rows that the checkbox of column "C" is checked on the sheet name of "Today".
  • The checkbox is put to "C6:C".
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification points:

  • The loop cost can be reduced by retrieving the data range.
  • When the row is deleted, when the reverse loop is used, the process can be simpler. Because when a row is deleted, the row number is changed. Please be careful this.

Pattern 1:

In this pattern, Spreadsheet service like SpreadsheetApp is used.

Modified script:

function myFunction() {
  var sheetName = "Today"; // Please set the sheet name.

  var Today = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  var values = Today.getRange('C6:C' + Today.getLastRow()).getValues();
  for (var i = values.length - 1; i >= 0; i--) {
    if (values[i][0]) Today.deleteRow(i + 6);
  }
}

Pattern 2:

In this pattern, Sheets API is used. So please enable Sheets API at Advanced Google services. When the number of delete rows is large, the process cost of this method is lower than that of the pattern 1.

Sample script:

function myFunction() {
  var sheetName = "Today"; // Please set the sheet name.

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(sheetName);
  var sheetId = sheet.getSheetId();
  var values = sheet.getRange('C6:C' + sheet.getLastRow()).getValues();
  var requests = values.reduce(function(ar, [e], i) {
    if (e) ar.push({deleteDimension:{range:{sheetId:sheetId,dimension:"ROWS",startIndex:(i + 5),endIndex:(i + 6)}}});
    return ar;
  }, []).reverse();
  Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
}

References:

If I misunderstood your question and this was not the direction you want, I apologize.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...