2017-03-06 37 views
0

我建立的系統將執行以下操作:谷歌表/表單腳本截止日期提醒電子郵件發送錯誤的反應編輯鏈接

  • 用戶提交
  • 的確認電子郵件中的鏈接發送給編輯表單響應(到目前爲止好)的形式
  • ,有一個「截止日期」的問題
  • 觸發腳本掃描這些日期的每一天
  • 當到期日已經到來,它會發送提醒郵件(這也發生公關operly,但)
  • 在此提醒電子郵件
  • ,編輯鏈接是重複的,這樣用戶不必搜索以前的郵件

可悲的是這個環節送出錯誤的。它不鏈接到正確的響應,而是鏈接到最後編輯(提交)的響應。

下面是腳本:

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 = "[email protected]"; // 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();

我只是不知道浩w解釋腳本如何獲得適當的響應。

也許我的做法是錯誤的,也許我應該告訴腳本掃描形式的數據庫,而不是鏈接的電子表格?任何想法如何做到這一點?

+0

我可以想到兩種可能的解決方案。兩種可能的解決方案都涉及將一些額外信息保存到電子表格。最好的解決方案是將原始編輯URL保存到電子表格中。所以,不要從表單中獲取編輯網址。另一種解決方案是將響應ID保存到電子表格中,並通過ID查找正確的表單響應,然後從該響應中獲取編輯URL。這兩個解決方案看起來很簡單,但有一些複雜性。如果不是不可能在電子表格中找到正確的行來追加ID或編輯網址,這非常困難。 –

+0

因此,您需要將所有數據保存到不同的電子表格中,而不是使用「內置」功能。因此,您需要進行更多編程才能將響應數據保存到完全不同的電子表格中,而不是使用「內置」功能的電子表格。您可以嘗試通過匹配時間戳和其他信息來查找表單中的正確響應,但可能在表單中具有多個完全相同的時間戳。所以,這不是一個可行的解決方案,除非您確定沒有人在同一秒內提交表格。 –

+0

感謝您的建議,併爲最近的回覆感到抱歉。最後,我選擇在表格中記錄編輯網址並從那裏引用它們。 – mozzribo

回答

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 = "[email protected]"; // 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}); 
      }  
    } 
    } 
0

您對從形式數據庫掃描自己的想法是最可行的選擇。這裏是一個正是這麼做的代碼片段,從谷歌的形式文檔頁面:

// 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

基本上,你會使用形式獲取表單迴應.responses()然後遍歷每個響應並獲得截止日期並檢查截止日期是否與今天相同。然後發送該特定響應的編輯網址。

希望這有助於,萬事如意

+0

非常感謝您的回答,並對已故的回覆感到抱歉。我通過將編輯網址保存到答卷中,然後引用正確的行來做了一個解決方法。我可能會在未來給你的腳本去。 – mozzribo

相關問題