跳过带有超链接的文本脚本 Google 工作表中的空单元格

Skip Empty Cells in Google Sheets for Text Script with Hyperlink

我有一个在 Google Apps 脚本中创建的脚本,它向具有超链接的 Google Sheet 列表中具有特定 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。任何指导将不胜感激。

您需要检查每个值以查看它们是否已填充,然后才将它们添加到消息中。您可以通过多种方式执行此操作,但最直接的可能是遍历值、构建数组,然后 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(), but it all really depends on how your data is structured. Personally, I don't like having a variable number of columns as they doesn't convert as neatly into arrays as variable rows do. I also prefer to work with objects 使它更干净一些。考虑如何从您的 city 数组生成此对象可能会使您的代码更易于理解:

var record = {
  name: 'Brian',
  slackId: '123',
  cities: [
    'New York',
    'London'
  ]
};