[英]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();
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.