简体   繁体   中英

Send Email with data from g-sheet table

I am new to Google App Scripts and have a spreadsheet with five columns: ISIN, Company, SecAccNo, Size and Type. I have named the range of my table A1:E20 Shares.

These columns are automatically populated by a formula, so sometimes I have this table populated with 5 rows of data and sometimes with 20 rows of data. Right now my script sends an email with 20 rows even if I only have 5 rows of data. So 15 rows of blank rows.

I would like to add a function "checkvalue" that checks if in the last column "Type" that is always "Entry" when the row is filled, so that an email is sent with only the rows that have "Entry" in the last column "Type", could you help me with this?

Here is my script:

function getEmailHtml(stockData) {
  var htmlTemplate = HtmlService.createTemplateFromFile("Template.html");
  htmlTemplate.stocks = stockData;
  var htmlBody = htmlTemplate.evaluate().getContent();
  return htmlBody;
}


function sendEmail() {
  var stockData = getData();
  var body = getEmailText(stockData);
  var htmlBody = getEmailHtml(stockData);
  if (stock.type === "Entry")

  MailApp.sendEmail({
    to: "xyz@gmail.com", //Enter your email address
    subject: "Manual Posad",
    body: body,
    htmlBody: htmlBody
  });
}
function getEmailText(stockData) {
  var text = "";
  stockData.forEach(function(stock) {
    text = text + stock.isin + "\n" + stock.company + "\n" + stock.secaccno + "\n" + stock.size + "\n" + stock.type + "\n-----------------------\n\n";
  });
  return text;
}
/**
 * @OnlyCurrentDoc
 */

function getData() {
  var values = SpreadsheetApp.getActive().getSheetByName("Data").getRange("Stocks").getValues();
  values.shift(); //remove headers
  var stocks = [];
  values.forEach(function(value) {
    var stock = {};
    //Logger.log("stocks:"+stocks);
    stock.isin = value[0];
    stock.company = value[1];
    stock.secaccno = value[2];
    stock.size = value[3];
    stock.type = value[4];
    stocks.push(stock);

  })
  //Logger.log(JSON.stringify(stocks));
  return stocks;
}

You can get an dynamic ending with another use of the .getRange() method. The use is: .getRange(startrow, startcol, numRows, numCols ). Below the change:

var values = SpreadsheetApp.getActive().getSheetByName("Data").getRange("Stocks").getValues();

To:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Data");
var values = sheet.getRange(1, 1, sheet.getLastRow(), 5).getValues();

Sending only the data in a table in an Email

gs:

function getMyData() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet1");
  const vs = sh.getRange("A1:E20").getValues().filter(r => r[4] !== '');
  Logger.log(JSON.stringify(vs));
  return vs;
}

function sendEmailTable() {
  let htmlTemplate = HtmlService.createTemplateFromFile('ah1');
  let html = htmlTemplate.evaluate().getContent();
  let recipients = "recip@gmail.com";
  let subject = "My Data"
  GmailApp.createDraft(recipients, subject, '', { htmlBody: html });
}

html: (filename: ah1.html)

<!DOCTYPE html>
<html>
<head>
  <base target="_top">
</head>
<body>
  <div id="tabledata">
       <? var vs = getMyData(); ?>
       <table>
         <? vs.forEach((r,i)=>{ ?>
           <tr>
           <? r.forEach((c,j)=>{ ?>
             <? if(i == 0) { ?>
            <th style="padding:2px 5px;font-weight:bold;border:1px solid black;"><?= c ?> </th>           
           <? } else { ?>
             <td style="padding:2px 5px;border:1px solid black;"><?= vs[i][j] ?> </td>
           <? } ?>
         <?  }); ?>
           </tr>
         <? }); ?>
       </table>
     </div>
</body>
</html>

Email Draft:

在此处输入图像描述

Sheet1: Range: A1:E20

ISIN Company SecAccNo Size Type
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
6 7 8 9 10
7 8 9 10 11
8 9 10 11 12
9 10 11 12 13

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