首页 > 解决方案 > 如何使用 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 的值。

有谁知道我在这里想念什么?

标签: google-apps-script

解决方案


这个改装怎么样?

修改点:

  • 的错误原因each of the three values appear as 'undefined' in the email body instead of the values of cells D2, H2, and L2CurrentRowCurrentRow是一个数字。但是在您的脚本中,您将其用作数组。这就是问题的原因。
    • 为了避免此问题,请从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>";

参考:

如果我误解了您的问题并且这不是您想要的结果,我深表歉意。


推荐阅读