简体   繁体   中英

Send personalise email from google sheet

I'm having difficults trying to write a script to send an email based on the below table

The "agent ldap" is the recipient the "TL ldap" is the CC and the rest 4 columns are inside the message as a table

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

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.

Is there a way to add a conditional statement for the column F depending the name, to add the color the backgroung? 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 ->`.

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:

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

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});
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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