I want to delete all the rows of a sheet that match my criteria which is:
I've tried this code but the formula doesn't work:
function deleterows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var lastrowtraining = ss.getSheetByName("Training").getRange("D:D").getValues().filter(String).length
var training = ss.getSheetByName("Training").getRange(1,1,lastrowtraining+4,42).getValues()
//Here I define TODAY - 15
var today = new Date(new Date().setHours(23,59,59,0,0))
var todayminus15 = new Date(today.setDate(today.getDate()-15))
// Here I define the columns
for(i=0;j<lastrowtraining+3;i++){
var rowtraining = i+1;
var date = training[i][0]
var city = training[i][1]
var trainingcomplete = training[i][5]
if(trainingcomplete =='YES' && date <= todayminus15) {training.deleteRow(rowtraining)}
}
}
But I get this error:
TypeError: Cannot find function deleteRow in object "Date,City Code,Name,Email,Phone,Successfully completed training,Days since registration if not complete,Send reminder?,Acuity ID,Successfully doc
... (which is the content of the entire sheet)
Thanks @ADW, @Chris G and @lazy.lizard.
Main problem was that I was triggering action deleteRow
on training which was a getValues
an deleteRow
needs a sheet.
So this half solved the problem:
var training = trainingsheet.getRange(1,1,lastrowtraining+4,42).getValues(); // change this line
But, (I already don't know why) this code only works on the first row that matches the criteria so I add a trigger to the deleterowsnew()
function after the row was deleted:
if(trainingcomplete =='YES' && date <= todayminus15) {trainingsheet.deleteRow(rowtraining);deleterowsnew()}
So the final code ends up like this:
function deleterowsnew() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var lastrowtraining = ss.getSheetByName("Training").getRange("D:D").getValues().filter(String).length
var trainingsheet = ss.getSheetByName("Training")
var training = trainingsheet.getRange(1,1,lastrowtraining+4,42).getValues()
//Here I define TODAY - 15
var today = new Date(new Date().setHours(23,59,59,0,0))
var todayminus15 = new Date(today.setDate(today.getDate()-15))
// Here I define the columns:
for(i=0;i<lastrowtraining+3;i++){
var rowtraining = i+1;
var date = training[i][0]
var city = training[i][1]
var trainingcomplete = training[i][5]
//I define here the condition and after it's completed and trigger the action again to be completed if another row matches the criteria
if(trainingcomplete =='YES' && date <= todayminus15) {trainingsheet.deleteRow(rowtraining);deleterowsnew()}
}
}
Thank you all for the help and hope this can help you on the future!