应用程序脚本 - 根据剩余天数发送电子邮件 - 电子邮件内容第一和第二 - 无法在已发送电子邮件中并排显示列

App Script - Send Email based on days left to date - Email content 1st and 2nd - can't display the columns side by side in the sent email

@NaziA 在这里帮我写了这个脚本。

我正在使用它在即将到达单元格中的特定日期(3 天)时从电子表格发送一封自动电子邮件。

作为电子邮件的内容,我收到一条消息,link,以及即将到达特定日期的第 1 和第 2 列元素。

我这里的 objective 是在输出邮件中将第一列和第二列一起显示或并排显示,以便于比较。

这是我正在使用的代码:

function offboardingReminderV2() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  // set active sheet to first sheet
  spreadsheet.setActiveSheet(spreadsheet.getSheets()[0]);
  var sheet = spreadsheet.getActiveSheet();
  // figure out what the last row is
  var lastRow = sheet.getLastRow();
  var startRow = 2;
  // grab all data from user to days left
  var range = sheet.getRange(startRow, 1, lastRow - startRow + 1, 12);
  var values = range.getValues();
  var users1 = [];
  var users2 = [];

  // loop all data per row
  values.forEach(function(row) {
    // if days left is 3
    if(row[11] == 3) {
      // add user if 3 days left
      users1.push(row[0]);
      users2.push(row[1]);
    }
  });

  // if users has elements
  if(users1) {
    // Formatted the message as html to look nicer
    var message = "<html><body></h1><p><b>The following user/s offboarding is due in 3 days</font2></h1><p>https://docs.google.com/spreadsheets/d/1QvJx7AaHT4cPGS6kvfH2Vatpn6jXo4gH1Mae8906ZFQ/edit#gid=372029014:</p>";
    // created bulleted list for list of users
    var emails = "<ul>";
    users1.forEach(function(user){
      emails = emails + "<li>" + user + "</li>";
    });
    
    emails += "</ul>";

  if(users2) {
    // created bulleted list for list of users
    var names = "<ul>";
    users2.forEach(function(user){
      names = names + "<li>" + user + "</li>";
    });
    
    emails += "</ul>";
    names += "</ul>"
    message = message + names + emails + "</body></html>";
    var flexmails = "email@example.com";
    MailApp.sendEmail(flexmails, "Reminder Offboarding Control", "", {htmlBody: message, noReply: true});
  }
}
}

谁能帮我解决这个问题?

我相信你的目标如下。

  • 您想通过排列列将列“A”和“B”中的值放入 <li> 的标记中。

在这种情况下,我想修改if(users1) {}的if语句。当你的脚本修改后,变成如下。

修改后的脚本:

从:
  if(users1) {
    // Formatted the message as html to look nicer
    var message = "<html><body></h1><p><b>The following user/s offboarding is due in 3 days</font2></h1><p>https://docs.google.com/spreadsheets/d/1QvJx7AaHT4cPGS6kvfH2Vatpn6jXo4gH1Mae8906ZFQ/edit#gid=372029014:</p>";
    // created bulleted list for list of users
    var emails = "<ul>";
    users1.forEach(function(user){
      emails = emails + "<li>" + user + "</li>";
    });
    
    emails += "</ul>";

  if(users2) {
    // created bulleted list for list of users
    var names = "<ul>";
    users2.forEach(function(user){
      names = names + "<li>" + user + "</li>";
    });
    
    emails += "</ul>";
    names += "</ul>"
    message = message + names + emails + "</body></html>";
    var flexmails = "email@example.com";
    MailApp.sendEmail(flexmails, "Reminder Offboarding Control", "", {htmlBody: message, noReply: true});
  }
}
到:
if (users1.length > 0 && users2.length > 0) {
  var message = "<html><body></h1><p><b>The following user/s offboarding is due in 3 days</font2></h1><p>https://docs.google.com/spreadsheets/d/###/edit#gid=372029014:</p>";
  var names = "<ul>";
  users2.forEach(function (user, i) {
    names += `<li>${user}, ${users1[i]}</li>`;
  });
  names += "</ul>"
  message = message + names + "</body></html>";
  var flexmails = "email@example.com";
  MailApp.sendEmail(flexmails, "Reminder Offboarding Control", "", { htmlBody: message, noReply: true });
}
  • 如果要替换列,请将names += `<li>${user}, ${users1[i]}</li>`;修改为names += `<li>${users1[i]}, ${user}</li>`;