【问题标题】:Google Apps Script: How do I check for an empty filter/variable?Google Apps 脚本:如何检查空过滤器/变量?
【发布时间】:2020-05-14 12:01:09
【问题描述】:

我一直在研究一些基于特定条件过滤工作表的 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}
  );
}

(只贴了相关的sn-p)

很遗憾,这不起作用。有谁知道实现它的方法吗?

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    答案:

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

    代码:

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

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

    到:

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

    参考资料:

    【讨论】:

      猜你喜欢
      • 2021-10-25
      • 2022-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 1970-01-01
      相关资源
      最近更新 更多