- 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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…