Google Apps 脚本:如何检查空过滤器/变量?



我一直在研究一些Google Apps Script代码,该代码可以根据特定条件过滤工作表。

将发送一封电子邮件,过滤后的数据显示在 HTML 表中。

我想这样做,以便仅在该过滤器中有数据时才发送此电子邮件。

我尝试了以下方法:

var newdata = tableRangeValues.filter(function(item) {
return item[0] === filt && item[1] === endDate;
});
if(newdata !== "") {
GmailApp.sendEmail(
"someemail@somedomain.com", 
"New EMEA Distribution Onboarding Request(s)",
"You have received the following new Request(s). Please see the details below.",
{htmlBody: htmlForEmail}
);
}

(仅发布相关片段(

不幸的是,这不起作用。有谁知道实现它的方法吗?

答案:

Array.prototype.filter()函数有一个数组返回,因此您可以检查其长度,并且仅在不为零时才进行调用,而不是将其与空字符串进行比较。

法典:

只需将 if 条件从:

if (newdata !== "") {
//...
}

自:

if (newdata.length != 0) {
//...
}

引用:

  • Array.prototype.filter()- JavaScript |多核

最新更新