简体   繁体   English

从谷歌表发送个性化 email

[英]Send personalise email from google sheet

I'm having difficults trying to write a script to send an email based on the below table我很难根据下表编写脚本来发送 email

The "agent ldap" is the recipient the "TL ldap" is the CC and the rest 4 columns are inside the message as a table “agent ldap”是收件人,“TL ldap”是 CC,rest 4 列在消息内作为表格

I'd like the message be like:我希望消息如下:

Hello, follow your feedback regarding your requests that we have registered:您好,请关注您对我们已注册的请求的反馈:

The table with the columns C, D, E and F包含 C、D、E 和 F 列的表格

You can already check it on你已经可以检查了

My main difficult is cause the number of rows may change (no more than five rows), also the status can change of this three ways.我的主要困难是导致行数可能会改变(不超过五行),这三种方式的状态也可以改变。 I'll copy past the values on this sheet, then send the email.我将复制此表上的值,然后发送 email。

Is there a way to add a conditional statement for the column F depending the name, to add the color the backgroung?有没有办法根据名称为 F 列添加条件语句,为背景添加颜色? Because I wanna include this column on the table too.因为我也想把这一列包括在桌子上。

My script is:我的脚本是:

function VacationRequest() {
    var sheet = SpreadsheetApp.getActiveSpreadsheet();
    var startRow = 2; // First row of data to process
    var numRows = 2; // Number of rows to process
    var dataRange = sheet.getRange(startRow, 1, numRows, 2);
    // Fetch values for each row in the Range.
    var data = dataRange.getValues();
    var emailRecipients = data[0] + "@google.com";
    var CCEmail = data[1] + "@google.com";
    var emailSubject = "Vacation request";
    var emailBody = "Hello, follow your feedback regarding your requests that we have registered: \
    \
    "The table with the columns C, D, E and F"
    \
    You can already check"


MailApp.sendEmail({
  to: emailRecipients,
  subject: emailSubject,
  htmlBody: emailBody,
  CC: CCEmail
 });

}



在此处输入图像描述

Approach方法

You can build a table in HTML using the new Javascript template literals ->`.您可以使用新的 Javascript 模板文字 ->` 在 HTML 中构建表。

Here is an example that builds a table in a for loop checking for the value in column F with a plain text fallback body:这是一个在 for 循环中构建表的示例,该表使用纯文本后备正文检查 F 列中的值:

function myFunction() {
  let ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  let rows = ss.getDataRange().getValues();
  for (let i in rows) {
    let bg_color;
    switch(rows[i][5]) {
        case 'Reflected':
              bg_color = 'green';
              break;
        case 'Rejected':
              bg_color = 'pink';
              break;
        case 'Pending':
              bg_color = 'orange';
              break;
        default:
              bg_color = 'white';
    }
    let htmlBody = `
      <p>Hello, follow your feedback regarding your requests that we have registered:<\/p>
        <table>
        <tr>
        <th>From the<\/th><th>to the (included)<\/th><th>Date of request<\/th>
        <\/tr>
        <tr>
          <td>
            ${rows[i][2]}
          <\/td>
          <td>
            ${rows[i][3]}
          <\/td>
          <td>
            ${rows[i][4]}
          <\/td>
          <td style="background-color: ${bg_color}">
            ${rows[i][5]}
          <\/td>
        <\/tr>
      <\/table>
      <p>You can already check<\/p>`;
      let body = `
      Hello, follow your feedback regarding your requests that we have registered:
      ${rows[i][2]}, ${rows[i][3]}, ${rows[i][4]} .
      You can already check.`;
      let options = {
        "htmlBody": htmlBody
      }
      MailApp.sendEmail(rows[i][1], "Vacation request", body, options);
   }
}

References:参考:

Send Mail 发邮件

JS Template literals JS 模板字面量

Is this what you are looking for:这是你想要的:

function VacationRequest() {
  var sheet = SpreadsheetApp.getActive();
  var startRow = 2; 
  var numRows = 2; 
  var dataRange = sheet.getRange(startRow, 1, numRows, 2);
  var data = dataRange.getValues();
  for(var i=0;i<data.length;i++) {
    var emailRecipients = data[i][0] + "@google.com";
    var CCEmail = data[i][1] + "@google.com";
    var emailSubject = "Vacation request";
    var emailBody = "Hello, follow your feedback regarding your requests that we have registered: You can already check";
    MailApp.sendEmail({to: emailRecipients,subject: emailSubject,htmlBody: emailBody,CC: CCEmail});
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM