最簡單的方法知道的日期是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個功能清晰分裂的代碼,但是當它作爲觸發運行,你應該將兩種功能組合在一起以獲得自動檢查...