最简单的方法知道的日期是10天之后可能是数毫秒!
我知道这听起来像笑话但它不是;-)
实施例:
function test(){
var today = new Date();
var tendaysBefore = new Date(today.getTime()-10*24*60*60*1000);// 10 times 24 hours
Logger.log(today+' is 10 later than '+tendaysBefore);
}
的方法的getTime()返回一个从基准日期的毫秒数,它将工作直到2070所以我想这是可以放心使用,现在;-)
触发问题在科尼利厄斯的回答已经解决了,THX
编辑:这里是一个可能的代码做你想要什么:(你的表测试)
var sh = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();// replace these with openbyId''ID') and getSheetByName(name) when you run it with trigger since the sheet wil be 'closed'
var lastrow = ss.getLastRow();
function onOpen() {
var menuEntries = [ {name: "check late paiments", functionName: "test"},
{name: "send mails to tagged users", functionName: "mailunpaid"},
];
ss.addMenu("custom functions",menuEntries);// custom menu
}
function test(){ // check column D and set it to "no" if not paid
var paidcol = sh.getRange(2, 4, lastrow-1, 1).getValues();//read only col D
for(nn=0;nn<paidcol.length;++nn){
if(paidcol[nn][0].toString().toLowerCase().match('yes')!='yes'){
paidcol[nn][0]='no'
}
}
sh.getRange(2, 4, lastrow-1, 1).setValues(paidcol);// write back to sheet
}
function mailunpaid(){
var data = sh.getDataRange().getValues();//read the whole sheet in an array, col D is idx3, timestamp is idx0 email is idx 23
var today = new Date();
var tendaysBefore = new Date(today.getTime()-10*24*60*60*1000);// 10 times 24 hours
for (nn=1;nn<data.length;++nn){ // iterate from row 2 to end ie idx 0 to last
// Logger.log(data[nn][3]+' '+data[nn][0])
if(data[nn][0]<=tendaysBefore && data[nn][3]=='no'){
// MailApp.sendEmail(data[nn][23], 'subject', 'body'); // you have to define the mail subject & content somewhere ;-) and uncomment when finished your tests
Logger.log('row '+Number(nn+1)+' = to send because '+data[nn][0])
sh.getRange(nn+1,4).setValue('SENT');// tag this user to know that mail has been sent to avoid multiple emails
}
}
}
注意,我在2个功能清晰分裂的代码,但是当它作为触发运行,你应该将两种功能组合在一起以获得自动检查...