Google表格/表格脚本截止日期提醒电子邮件发送错误的响应编辑链接

时间:2017-03-06 11:43:53

标签: email google-apps-script google-sheets google-form

我设置的系统执行以下操作:

  • 用户提交表单
  • 发送确认电子邮件,其中包含编辑回复的链接(到目前为止一直很好)
  • 在表单中,有一个截止日期'问题
  • 触发器脚本每天扫描这些日期
  • 截止日期到来时,会发送提醒电子邮件(这也是正确的,但是)
  • 在此提醒电子邮件中,重复编辑链接,以便用户无需搜索以前的邮件

可悲的是,此链接发送错误。它不是链接到正确的响应,而是提供最后编辑(提交)的响应的链接。

这是脚本:



function sendReminderEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;  // First row of data to process
var numRows = sheet.getLastRow()-1;   // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
// Fetch values for each row in the Range.
var data = dataRange.getValues();
//Logger.log(data)

var form = FormApp.openById("IDremovedByMe");
var formResponses = form.getResponses();
var r = formResponses.length-1;
var editURL = formResponses[r].getEditResponseUrl();
//Get the Edit URL

for (i in data) {
  var row = data[i];
  var date = new Date();
  date.setHours(0);
  date.setMinutes(0);
  date.setSeconds(0);
  //Logger.log(date);
  var sheetDate = new Date(row[13]);
 //Logger.log(sheetDate);
 var Sdate = Utilities.formatDate(date,'GMT+0100','yyyy:MM:dd')
 var SsheetDate = Utilities.formatDate(sheetDate,'GMT+0100', 'yyyy:MM:dd')
     Logger.log(Sdate+' =? '+SsheetDate)
        if (Sdate == SsheetDate){
          var sendTo = row[4];  // Collect email address from the fourth column (number value is always -1).
          var sendMe = "xyzabcloremipsum@gmail.com";  // Enter the email address where you wish to receive a notification about a reminder sent.
          var sendername = "Auto Formbot"; // Name displayed as the sender.
          var myname = "Formbot"; // Name displayed as the sender to you.
          var messageTo = "Based on the data you entered, the '" +row[6] +"' project with the ID: " +row[1] +" has ended.\n\nPlease mark it as 'Completed' and update the details as soon as it's convenient for you.\n\nYou can edit your data by using the following link:\n" +  editURL + "\n\nThank you for your time.\n\n\nWith kind regards,\nFormbot";
          var messageMe = "The '" +row[6] +"' project with the ID: " +row[1] +" has finished today.\n\nA reminder email has been sent to " +row[4] +".\n\nYou can edit the data by using the following link:\n" +  editURL + "\n\n\nSincerely,\nFormbot";
          // Above is the column (number value is always -1) selected for activity name display.
          var subjectTo = "Please update the '" +row[6] +"' activity data.";
          var subjectMe = "An activity has finished today [ID: " +row[1] +"].";
          MailApp.sendEmail(sendTo, subjectTo, messageTo, {name: sendername});
          MailApp.sendEmail(sendMe, subjectMe, messageMe, {name: myname});
            }    
   }
  }




问题显然在这一部分:



var form = FormApp.openById("IDremovedByMe");
var formResponses = form.getResponses();
var r = formResponses.length-1;
var editURL = formResponses[r].getEditResponseUrl();




我只是不确定如何解释脚本如何获得适当的响应。

也许我的方法是错误的,也许我应该告诉脚本扫描表单数据库而不是链接的电子表格?关于如何做到的任何想法?

2 个答案:

答案 0 :(得分:1)

因此,我选择将URL直接插入到响应表中并从那里引用它们。

我使用了here找到的脚本。

function injectEditURLs() {
  // Form ID:
  var form = FormApp.openById('IDremovedByMe');

  // Name of the (main) sheet and NOT the Sheet file name where the URLs will appear:
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Responses');

  var data = sheet.getDataRange().getValues();
  var urlCol = 11; // The number of the column in which the URL will be inserted; A = 1, B = 2 etc.
  var responses = form.getResponses();
  var timestamps = [], urls = [], resultUrls = [];

  for (var i = 0; i < responses.length; i++) {
    timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
    urls.push(responses[i].getEditResponseUrl());
  }
  for (var j = 1; j < data.length; j++) {

    resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]:'']);
  }
  sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);  
}

然后我在提醒电子邮件脚本中引用它(并删除了不必要的位):

" + row[n] +"

所以它现在看起来像这样:

function sendReminderEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;  // First row of data to process
var numRows = sheet.getLastRow()-1;   // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
// Fetch values for each row in the Range.
var data = dataRange.getValues();
//Logger.log(data)

for (i in data) {
  var row = data[i];
  var date = new Date();
  date.setHours(0);
  date.setMinutes(0);
  date.setSeconds(0);
  //Logger.log(date);
  var sheetDate = new Date(row[13]);
 //Logger.log(sheetDate);
 var Sdate = Utilities.formatDate(date,'GMT+0100','yyyy:MM:dd')
 var SsheetDate = Utilities.formatDate(sheetDate,'GMT+0100', 'yyyy:MM:dd')
     Logger.log(Sdate+' =? '+SsheetDate)
        if (Sdate == SsheetDate){
          var sendTo = row[4];  // Collect email address from the fourth column (number value is always -1).
          var sendMe = "xyzabcloremipsum@gmail.com";  // Enter the email address where you wish to receive a notification about a reminder sent.
          var sendername = "Auto Formbot"; // Name displayed as the sender.
          var myname = "Formbot"; // Name displayed as the sender to you.
          var messageTo = "Based on the data you entered, the '" +row[6] +"' project with the ID: " +row[1] +" has ended.\n\nPlease mark it as 'Completed' and update the details as soon as it's convenient for you.\n\nYou can edit your data by using the following link:\n" + row[10] + "\n\nThank you for your time.\n\n\nWith kind regards,\nFormbot";
          var messageMe = "The '" +row[6] +"' project with the ID: " +row[1] +" has finished today.\n\nA reminder email has been sent to " +row[4] +".\n\nYou can edit the data by using the following link:\n" +  row[10] + "\n\n\nSincerely,\nFormbot";
          // Above is the column (number value is always -1 because A=0) selected for activity name display.
          var subjectTo = "Please update the '" +row[6] +"' activity data.";
          var subjectMe = "An activity has finished today [ID: " +row[1] +"].";
          MailApp.sendEmail(sendTo, subjectTo, messageTo, {name: sendername});
          MailApp.sendEmail(sendMe, subjectMe, messageMe, {name: myname});
            }    
   }
  }

答案 1 :(得分:0)

您自己关于从表单数据库进行扫描的想法是最可行的选择。以下是来自Google表单文档页面的代码片段,其中包含以下内容:

// Open a form by ID and log the responses to each question.
 var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
 var formResponses = form.getResponses();
 for (var i = 0; i < formResponses.length; i++) {
   var formResponse = formResponses[i];
   var itemResponses = formResponse.getItemResponses();
   for (var j = 0; j < itemResponses.length; j++) {
     var itemResponse = itemResponses[j];
     Logger.log('Response #%s to the question "%s" was "%s"',
         (i + 1).toString(),
         itemResponse.getItem().getTitle(),
         itemResponse.getResponse());
   }
 }

以下是该页面的链接:https://developers.google.com/apps-script/reference/forms/item-response

基本上,您将使用form.responses()获取表单响应,然后遍历每个响应并获取截止日期,并检查截止日期是否与今天相同。然后发送该特定响应的编辑网址。

希望这会有所帮助,一切顺利