簡體   English   中英

Google 表格不斷發送電子郵件

[英]Google Sheets keeps sending emails

一直在構建登陸頁面以啟動 email 列表。 決定使用 Google 表格作為后端來存儲電子郵件和 Google 腳本以發送第一條歡迎消息。

我使用 API 將 HTML 頁面連接到 Google 表格。用戶提交他們的姓名和 email,然后它轉到 Google 表格。 編輯文檔時,將觸發此 function 發送歡迎消息。

為了防止重復消息外出,我建立了一個簡單的系統。 當用戶收到電子郵件時,第三行 (C) 會自動填寫“EMAIL_SENT”。

當 function 被觸發時,如果沒有“EMAIL_SENT”,它應該只向用戶發送 email,但每次新用戶提交他們的信息時,列表中的每個 email 都會收到另一條歡迎消息。

我將附上電子表格的圖像電子表格的屏幕截圖


// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'EMAIL_SENT';

/**
 * Sends non-duplicate emails with data from the current spreadsheet.
 */
function sendEmails2() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = sheet.getLastRow();
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, numRows, 3);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var emailAddress = row[0]; // First column
    var nameThisMan = row[1]; // Person's name
    var emailSent = row[2]; // Third column
    
    if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
    var message = "Hey there " + nameThisMan + "," + "\n \n Thanks for joining the email list! You’re about to be getting some AMAZING programming resources. Here’s the link to the top 10 open source Github repos PDF. \n https://www.dropbox.com/s/wm8ctsdojlsoss6/10%20Most%20Useful%20Open%20Source%20Repos%20for%20Developers.pdf?dl=0\n\n And here’s the link to the archive of all the resources from this email list. These are from all past emails. \n https://docs.google.com/document/d/1hWmGNYkn0czRI29JJu9HgUVC6L4AsNYBxwt45ABnT6w/edit?usp=sharing \n \n Thank you again for joining the list! I will do my best to give you absolutely amazing programming resources. \n \n best, \n Michael Macaulay, Techtime.";
    var replyTo = "TechTimeDev@gmail.com";
    var subject = "Welcome to TechTime's Email List";
      MailApp.sendEmail(emailAddress, replyTo, subject, message);
      sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}
function sendEmails2() {
  var sheet=SpreadsheetApp.getActiveSheet();
  var startRow=2;
  var numRows=sheet.getLastRow()-startRow+1;
  var dataRange=sheet.getRange(startRow, 1, numRows, 3);
  var data=dataRange.getValues();
  for (var i=0; i < data.length; ++i) {
    var row=data[i];
    var emailAddress=row[0];
    var nameThisMan=row[1];
    var emailSent=row[2]; 
    var replyTo="TechTimeDev@gmail.com";
    var subject="Welcome to TechTime's Email List";
    if (emailSent!='EMAIL_SENT') { 
      var message="Hey there " + nameThisMan + "," + "\n \n Thanks for joining the email list! You’re about to be getting some AMAZING programming resources. Here’s the link to the top 10 open source Github repos PDF. \n https://www.dropbox.com/s/wm8ctsdojlsoss6/10%20Most%20Useful%20Open%20Source%20Repos%20for%20Developers.pdf?dl=0\n\n And here’s the link to the archive of all the resources from this email list. These are from all past emails. \n https://docs.google.com/document/d/1hWmGNYkn0czRI29JJu9HgUVC6L4AsNYBxwt45ABnT6w/edit?usp=sharing \n \n Thank you again for joining the list! I will do my best to give you absolutely amazing programming resources. \n \n best, \n Michael Macaulay, Techtime.";
      MailApp.sendEmail(emailAddress, replyTo, subject, message);
      sheet.getRange(startRow + i, 3).setValue('EMAIL_SENT');
    }
  }
}

getLastRow()方法將返回包含內容的最后一行的索引。 在您提供的示例中,這將是3 但是,您真正想要執行getRange()的是您需要獲取的行數。 如果你使用三個,你會得到一個額外的空行,因此,你會得到錯誤的值。

要更改此設置以獲得所需的行數,您只需執行以下操作:

var numrows = sheet.getLastRow() - 1; 而不是var numRows = sheet.getLastRow();

因為您需要從索引中減去您不想進入范圍內的行數(在您的情況下,因為您忽略了第一行,所以只需要減去1行)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM