我有一个在Google Apps脚本中创建的脚本,该脚本向具有超链接的Google表格中列表上的某些ID的用户发送一条松弛消息。每个用户的列表不包含相同数量的ID,导致空单元格的超链接URL出现在松弛消息中。是否可以跳过或隐藏每个用户都为空的单元格的超链接URL。抱歉,如果不清楚。请参阅下面的有效负载脚本:
// custom slack webhook
var url = "https://hooks.slack.com/services/XXXXXSBGW/XXXXXXU1K/XXXXXXXXn9jfZXAAAAAA";
var payload = {
"channel": "@"+city[2],
"username": "Alerts",
"text": "Hi " + city[0] + "\n"+
"\n Here are your most pending kits for review.\n" +
"\n <https://google.com/maps/'"+"|"+city[5]+">" +
"\n <https://google.com/maps/'"+"|"+city[6]+">" +
"\n <https://google.com/maps/'"+"|"+city[7]+">" +
"\n <https://google.com/maps/'"+"|"+city[8]+">" +
"\n <https://google.com/maps/'"+"|"+city[9]+">" +
"\n Please review these kits as soon as possible.\n"
};
提供了通用超链接,但“城市”电子表格上的第7至9列有时是空白的。如果留空或至少没有使URL出现,是否可以跳过这些单元格?当单元格为空白时,通常在松弛消息中显示超链接URL。任何指导将不胜感激。
答案 0 :(得分:0)
您需要检查每个值以查看它们是否已填充,然后才将其添加到消息中。您可以通过多种方式执行此操作,但最直接的方法可能是遍历值,构建数组,然后join数组。
// Example city array
var city = ["name", 1, 2, 3, 4, "city1", "city2", "", "city3"];
// Compile links
var linkStartIndex = 5;
var linkEndIndex = 9;
var links = [];
for (var i = linkStartIndex; i <= linkEndIndex; i++) {
var value = city[i]
if (value != null && value != "") {
links.push("\n <https://google.com/maps/'"+"|"+value+">");
}
}
// Compile full text
var text = [
"Hi " + city[0] + "\n",
"\n Here are your most pending kits for review.\n",
links.join(""),
"\n Please review these kits as soon as possible.\n"
];
var joined = text.join("");
console.log(joined);
使用map()可以使这一点更简洁,但这实际上取决于数据的结构。就个人而言,我不喜欢具有可变数量的列,因为它们不能像可变行那样整齐地转换为数组。我还更愿意在可能的情况下与objects合作。考虑一下如何从city
数组生成此对象可能会使您的代码更易于理解:
var record = {
name: 'Brian',
slackId: '123',
cities: [
'New York',
'London'
]
};