首页 > 解决方案 > Google Apps 脚本:如何检查空过滤器/变量?

问题描述

我一直在研究一些基于特定条件过滤工作表的 Google Apps 脚本代码。

发送一封电子邮件,过滤后的数据显示在 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}
  );
}

(只发布了相关的片段)

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

标签: google-apps-script

解决方案


回答:

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

代码:

只需将您的 if 条件更改为:

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

至:

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

参考:


推荐阅读