google-apps-script - 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}
);
}
(只发布了相关的片段)
不幸的是,这不起作用。有谁知道实现它的方法?
解决方案
回答:
该Array.prototype.filter()
函数有一个数组返回,因此您可以检查它的长度,而不是将其与空字符串进行比较,并且只有在它不为零时才进行调用。
代码:
只需将您的 if 条件更改为:
if (newdata !== "") {
//...
}
至:
if (newdata.length != 0) {
//...
}
参考:
推荐阅读
- c# - Elasticsearch(C# nest):映射抽象属性
- c# - .NET Core 2.0 可以说 OData v2 吗?
- python - sqlite3 connect python full path失败,在命令行工作
- dart - Dart:处理来自另一个包的异常
- python - 范围内数字的数字平方的乘法
- javascript - 解构的变量值不在模块之间保留
- linux - 如何让 Bash 循环休眠几秒钟?
- xamarin - 如何将模板上的 XAML 元素与 C# 后端而不是在 XAML 中绑定?
- node.js - 使用打字稿在Node js中导入类
- c# - SaveFileDialog 控件不显示在工具箱中