function clearRange() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Form Responses 1');
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
sheet.getRange('A8:V100').clearContent();
for (var row=451; row <= numRows; row++) {
sheet.deleteRow(row);
}
}
How do I create a loop to delete all rows after 450?
-
30-07-2022 - |
Question
I am working on a spreadsheet that populates from a google form. I figured out how to delete all the data after each week to reset the form and spreadsheet. The problem I am facing is that the form adds new rows with each entry. Eventually the spreadsheet will reach the row limit but I need the form to continue for several years. The code I have thus far is:
herefunction clearRange() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Form Responses 1');
sheet.getRange('A8:V100').clearContent();
for (row>=451; row<1000; row++) {
sheet.deleteRow(451)
}
}
As it resolves it gives me an error saying: ReferenceError: "row" is not defined. I am new to the programing world and would appreciate any assistance.
Solution 2
OTHER TIPS
While the other answer works its slow and will break when there are many rows. Use deleteRows instead to delete them all with a single call. https://developers.google.com/apps-script/reference/spreadsheet/sheet#deleteRows(Integer,Integer)