简体   繁体   中英

Google Sheets script deleting rows based on a timestamp and column value. Why is one working and not the other?

I have two functions running on a trigger at midnight on a Google Sheet. One of them, Delete60() deletes all rows after 60 days based on a timestamp in Column A. The other, Delete30() is supposed to delete rows early after 30 days if the value in Column D is "Books."

For some reason, the Delete60() function works exactly as expected, so just after midnight it deletes any rows 60 days old. But the Delete30() function seems to be running one day behind - it runs at roughly the same time (just after midnight) and correctly deletes based on Column D, but only rows that are 31 days old. Anything 30 days old stays. Am I missing something? The code seems identical besides the number of days and the extra check for the value of Column D. Does having that extra check somehow affect the timing?

function Delete60() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Request Info");//applies to active requests only
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array
  
var currentDate = new Date();//today
var monthOld = Date.now() + -60*24*3600*1000; 
for (i=lastrow;i>=1;i--) {
var tempDate = values[i-1][0];// arrays are 0 indexed so row1 = values[0] and col1 = [0]
if ((tempDate!="") && (tempDate <= (monthOld)))
{
  sheet.deleteRow(i);
}
}
}

function Delete30() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Request Info");//applies to active requests only
var datarange = sheet.getDataRange();
var lastrow = datarange.getLastRow();
var values = datarange.getValues();// get all data in a 2D array
  
var currentDate = new Date();//today
var monthOld = Date.now() + -30*24*3600*1000; 
for (i=lastrow;i>=1;i--) {
var tempDate = values[i-1][0];// arrays are 0 indexed so row1 = values[0] and col1 = [0]
var bookCheck = values[i-1][3];// values in column D
if ((tempDate!="") && (tempDate <= (monthOld)) && bookCheck=="Books")
{
  sheet.deleteRow(i);
}
}
}

Try this:

function Delete60() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName("Request Info");
  var rg=sh.getDataRange();
  var values=rg.getValues();
  var cd=new Date();
  var mold=new Date(cd.getFullYear(),cd.getMonth(),cd.getDate()-60).getTime();
  var d=0;
  values.forEach(function(r,i){if(new Date(values[i][0]).getTime()<=mold){sh.deleteRow(i+1-d++)}});
}

function Delete30() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName("Request Info");
  var rg=sh.getDataRange();
  var values=rg.getValues();
  var cd=new Date();
  var mold=new Date(cd.getFullYear(),cd.getMonth(),cd.getDate()-30).getTime();
  var d=0;
  values.forEach(function(r,i){if(new Date(values[i][0]).getTime()<=mold){sh.deleteRow(i+1-d++)}});
}

function Delete30Equal2Books() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName("Request Info");
  var rg=sh.getDataRange();
  var values=rg.getValues();
  var cd=new Date();
  var mold=new Date(cd.getFullYear(),cd.getMonth(),cd.getDate()-30).getTime();
  var d=0;
  values.forEach(function(r,i){if(r[3]=="Books"){sh.deleteRow(i+1-d++)}});
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM