google-apps-script - 如何使用 Google 表格通过电子邮件发送单行中的指定单元格
问题描述
我正在尝试在 Google 表格脚本中编写代码,以便发送包含三个单元格(D2、H2 和 L2)的值的电子邮件。
我已经接近了这段代码:
function sendCounts() {
//setup function
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = ss.getSheetByName("PART COUNTS");
var message = "";
{
//set current row
var CurrentRow = 2;
//set HTML template for information
message +=
"<p><b>First Count: </b>" + CurrentRow[4] + "</p>" +
"<p><b>Second Count: </b>" + CurrentRow[8] + "</p>" +
"<p><b>Third Count: </b>" + CurrentRow[12] + "</p><br><br>";
//set the row to look at
var setRow = CurrentRow;
}
//define who to send updates to
var SendTo = "me@emailaddress.com";
//set subject line
var Subject = "DAILY PART COUNTS";
//send the actual email
MailApp.sendEmail({
to: SendTo,
cc: "",
subject: Subject,
htmlBody: message,
});
}
电子邮件以正确的格式发送,但三个值中的每一个都在电子邮件正文中显示为“未定义”,而不是单元格 D2、H2 和 L2 的值。
有谁知道我在这里想念什么?
解决方案
这个改装怎么样?
修改点:
- 的错误原因
each of the three values appear as 'undefined' in the email body instead of the values of cells D2, H2, and L2
是CurrentRow
。CurrentRow
是一个数字。但是在您的脚本中,您将其用作数组。这就是问题的原因。- 为了避免此问题,请从
PART COUNTS
.
- 为了避免此问题,请从
- 当检索到工作表第 2 行的值时,
CurrentRow
可以将其用作数组。但在这种情况下,数组索引0
为 ,“D”、“H”和“L”的索引分别为 3、7 和 11。
修改后的脚本:
请进行如下修改。
从:var CurrentRow = 2;
//set HTML template for information
message +=
"<p><b>First Count: </b>" + CurrentRow[4] + "</p>" +
"<p><b>Second Count: </b>" + CurrentRow[8] + "</p>" +
"<p><b>Third Count: </b>" + CurrentRow[12] + "</p><br><br>";
到:
var CurrentRow = sh0.getRange("A2:L2").getValues()[0];
//set HTML template for information
message +=
"<p><b>First Count: </b>" + CurrentRow[3] + "</p>" +
"<p><b>Second Count: </b>" + CurrentRow[7] + "</p>" +
"<p><b>Third Count: </b>" + CurrentRow[11] + "</p><br><br>";
参考:
如果我误解了您的问题并且这不是您想要的结果,我深表歉意。
推荐阅读
- java - 在 Spring 集成测试期间刷新/重建特定 bean
- c# - C# RSA 使用自定义密钥对加密和解密
- r - 查找上个月的结束日期
- firefox - WebExtension 中的目录选择器对话框
- ibm-cloud - 使用 IBM 连接 Cloud API 添加社区所有者
- sql-server - dm_sql_referenced_entities 中的 is_column 是否可靠?
- javascript - 与其他系统共享本地存储
- java - 项目作为spring mongo中的嵌套文档
- jquery-ui - jquery ui - div中可调整大小的图像问题
- node.js - bot-framework v4 (node.js) 中的动态提示选择