简体   繁体   English

使用 g-sheet 表中的数据发送 Email

[英]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.我是 Google App Scripts 的新手,并且有一个包含五列的电子表格:ISIN、Company、SecAccNo、Size 和 Type。 I have named the range of my table A1:E20 Shares.我已将表的范围命名为 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.这些列是由公式自动填充的,所以有时我用 5 行数据填充这个表,有时用 20 行数据填充。 Right now my script sends an email with 20 rows even if I only have 5 rows of data.现在,即使我只有 5 行数据,我的脚本也会发送一个包含 20 行的 email。 So 15 rows of blank rows.所以15行空白行。

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?我想添加一个 function “校验值”,检查最后一列“类型”中是否在填充行时始终为“条目”,以便发送 email 时仅包含最后一个“条目”的行“类型”列,你能帮我吗?

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.您可以通过另一种使用.getRange()方法获得动态结尾。 The use is: .getRange(startrow, startcol, numRows, numCols ).用途是: .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仅发送 Email 中表中的数据

gs: 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) html:(文件名: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: Email 草案:

在此处输入图像描述

Sheet1: Range: A1:E20 Sheet1:范围:A1:E20

ISIN伊辛 Company公司 SecAccNo SecAccNo Size尺寸 Type类型
1 1 2 2 3 3 4 4 5 5
2 2 3 3 4 4 5 5 6 6
3 3 4 4 5 5 6 6 7 7
4 4 5 5 6 6 7 7 8 8
5 5 6 6 7 7 8 8 9 9
6 6 7 7 8 8 9 9 10 10
7 7 8 8 9 9 10 10 11 11
8 8 9 9 10 10 11 11 12 12
9 9 10 10 11 11 12 12 13 13

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

相关问题 G-script:从存储在 G-Sheet 中的变量中列出和过滤 G-Drive 文件 - G-script : List and Filter G-Drive Files from Variables Stored in a G-Sheet 清除 g-sheet 内容永远不会完成 - clearing a g-sheet content never finishes 将数据提交到我的 G-Sheet 后获取页面刷新 - Get page to refresh when after submitting data to my G-Sheet 从 API 获取事件/ID/名称的脚本,然后列出它们,G-sheet - Script to fetch events/ID/name from an API, then list them, G-sheet 从 API 获取用户和 ID 的脚本,然后在 G-sheet A10:B 中一一列出 - Script to fetch users and ID from an API, then list them, one by one in G-sheet A10:B 强制仅复制和粘贴文本G-Sheet - Force Copy and Paste of the text only G-Sheet G-Sheet 脚本使用其 API 在 Guilded 上发布消息 - G-Sheet script to post a message on Guilded using its API 如何使用gs从谷歌表格发送带有格式化表格的电子邮件? - How to send an email with a formatted table from google sheet using gs? 从 Google 工作表发送电子邮件作为表格而不使用工作表转换器 - Send Email from Google sheet as a table without using sheets convertor 如果列值超过阈值,则将 G-Sheet 列值替换为单元格中“n”个先前值的平均值 - Replace the G-Sheet column value, if the column value exceeds a threshold value, with the average of 'n' previous values in a Cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM