2012-07-19 143 views
0

因此,我有一个Google表单供给Google文档电子表格。这种形式是为我们的城市的新人注册我们的新人组。撰写Google Apps脚本,向发送电子邮件的用户发送电子邮件,满足其他标准

我想编写一个Google Apps脚本,它将以编程方式向发送提醒电子邮件的用户在提交表单10天后未支付其费用。

应该很简单吧?

这里的节录出来的个人数据电子表格的副本的链接:用某种比较来

https://docs.google.com/spreadsheet/ccc?key=0AjsoIob8dJfodG9WN0ZmWUE1ek9rc3JrVFpDQ0J0OGc

好像我应该能够使用A列(“时间戳”)现在()确定10天的部分。并且为了得到未缴纳的会费,只需要D列不等于是。显然,收件人的电子邮件地址在X栏中。

我已经编写了一个脚本来向收件人“在表单提交”上发送确认电子邮件。所以我对MailApp.sendEmail类感到满意。

如果您发现“Updaid”选项卡,您会发现我已经使用查询来解决那些未付款的人。

但我不知道如何让MailApp.sendEmail类对已经在工作表中的数据进行操作。当提交表单时不会自动触发。

而且我不知道如何使我的查询适应10天前的位。

而我甚至不确定我应该使用此应用程序的查询。

任何人都可以对我的路?

谢谢。

回答

2

最简单的方法知道的日期是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个功能清晰分裂的代码,但是当它作为触发运行,你应该将两种功能组合在一起以获得自动检查...