Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

Identify and delete the first checked checkbox in a column [Google Sheets]

  • 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

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

1 Reply

0 votes
by (71.8m points)
  • 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...