且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Google表格 - 删除日期过期行的脚本

更新时间:2023-12-04 13:53:30

日期在列C,如果该信息是有帮助的

"date in column C, if that info is helpful"

确实是这样!而不是试图在列A中获得一个日期,就像在这行中所做的那样:

It is indeed ! instead of trying to get a date in column A as it it done in this line :

var tempdate = sheet.getRange(i, 1).getValue();

您应该查看列C中的值,如下所示:

you should look at the value in column C like this :

var tempdate = sheet.getRange(i, 3).getValue();

但为了更有效率,您应该在数组级别进行这些比较,尝试如下,它将运行得更快...

but to be more efficient you should do these comparisons at array level, try it like below and it will run much faster ...

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Foglio1");
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array

var currentDate = new Date();
var oneweekago = new Date();
oneweekago.setDate(currentDate.getDate() - 7);

for (i=lastrow;i>=2;i--) {
var tempdate = values[i-1][2];// arrays are 0 indexed so row1 = values[0] and col3 = [2]

if(tempdate < oneweekago)  
{
  sheet.deleteRow(i);
}
}
}