【问题标题】:Hyperlinking specific text in email mesage sent from google sheets script从谷歌表格脚本发送的电子邮件中的超链接特定文本
【发布时间】:2020-05-05 21:24:14
【问题描述】:

我想在这行代码引用的电子邮件中添加指向特定文本部分的超链接: var message = row[2]; // Third column?

我已经尝试过这个建议<p> Please check out my <a href=https://example.com>site</a>,但我不知道如何将它插入到htmlbody 的变量中。我还尝试在脚本中硬编码实际的电子邮件,但一直收到错误。

完整代码:

// 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 = 1000; // Number of rows to process
  // Fetch the range of cells A2:D1000
  var dataRange = sheet.getRange(startRow, 1, numRows, 1000);
  // 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 subject = row[1]; // Second column
    var message = row[2]; // Third column
    var emailSent = row[3]; // Fourth column
    if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
      MailApp.sendEmail(emailAddress, subject, "", {
        htmlBody: message.replace(/\n/g,'<br>')});
      sheet.getRange(startRow + i, 4).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
      SpreadsheetApp.flush();
    }
  }
}```

【问题讨论】:

  • 我必须为我糟糕的英语水平道歉。我无法理解I want to add a hyperlink to a specific portion of text in the email message this line of code is referencing: var message = row[2]; // Third column?。您想将文本值放入电子表格并检索该值吗?或者您想在从电子表格检索到值后替换 row[2] 的值?
  • 它回答了你的问题吗? stackoverflow.com/questions/40465808/…
  • @JeffRush 我想,但我不确定如何将您建议的内容添加到我的脚本中。你能告诉我你的建议的哪一部分我应该包括,我应该把它放在我的脚本中的什么地方?
  • @MattStapleton 看看我的回答,它包含如何使用应用程序脚本发送 html 电子邮件的基本示例,您可以根据您的情况调整此示例

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


【解决方案1】:

TL;DR

使用MailApp.sendEmail(emailAddress, subject, '', options)

看看选项:sendEmail(recipient, subject, body, options)

分步指南:

  1. 用以下数据填写您的工作表:
  2. 打开 Apps 脚本并输入此代码
function sendEmail() {

  let data = SpreadsheetApp.getActiveSheet().getRange("A1:C1").getValues()
  
  let emailAddress = data[0][0]; 
  let subject = data[0][1]; 
  let message = data[0][2];

  let options = {
    htmlBody: `<a href="https://google.com">${message}</a>`
  }
  
  MailApp.sendEmail(emailAddress, subject, '', options);

}
  1. 运行您的代码并检查您的收件箱。你会看到这样的东西

【讨论】:

    猜你喜欢
    • 2021-11-10
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多