【问题标题】:Google Apps Script IF trigger Push to SlackGoogle Apps 脚本 IF 触发器 Push to Slack
【发布时间】:2018-10-18 18:09:12
【问题描述】:

我有一个 google 表格,其中包含与日期绑定在一起的交互式待办事项列表,如果其中一个日期过期,它会复制到表格中名为“过去”的单独选项卡。我使用下面的代码将这些过期警报推送到具有每日设置 Google Apps 触发器的 Slack 频道,但我想做的是添加一个 IF 变量,该变量只有在该选项卡中有一行要发布时才会发布。例如,如果 A2="" 则不推送到松弛,如果 A2=Not Blank 推送到松弛。任何帮助将不胜感激!

这是我正在使用的代码:

 function createMeetingMessage(sheet) {
 var facilitatorCell = "Past!C2";
 var noteTakerCell = "Past!E2";
 var emojiCell = "Past!L2";
var message = "> *Hello* "  + "<" +sheet.getRange(noteTakerCell).getValue() +">" + " *Please Update Us On*\n"
+ "> *Task:* " +sheet.getRange(facilitatorCell).getValue() +"\n"
+ "> *Due Date:* " +sheet.getRange(emojiCell).getValue() +"\n"
+ "> _you can submit an update by typing task;task name;in progress or complete; comments_ ";

 return message;
}

function triggerSlackRequest(channel, msg) {
 var slackWebhook = "[Slack Webhook]";
  var payload = { "channel": channel, "text": msg, "link_names": 1, "username": "Past Due Alert", "icon_emoji": ":rotating_light:" };
 var options = { "method": "post", "contentType": "application/json", "muteHttpExceptions": true, "payload": JSON.stringify(payload) };

Logger.log(UrlFetchApp.fetch(slackWebhook, options));
}

function notifySlackChannelOfFacilitatorAndNoteTaker() {
 var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Past");
 var channel = getChannel();

var msg = createMeetingMessage(sheet);
 triggerSlackRequest(channel, msg);
}

function isTest() {
 return false;
}

function getChannel() {
 if(isTest()) {
 return "integration_tests";
 } else {
 return "integration_tests";
 }
}

下面是当 A2 中有数据推送到 Slack 时的样子:

下面是 A2 中没有数据并将其推送到 Slack 时的样子:

谢谢!

【问题讨论】:

  • 如果不想在“Past”表中的“A2”没有值的情况下运行脚本,可以通过将if (!sheet.getRange("A2").getValue()) return放在var channel = getChannel()前面来实现。但我不确定这是否是你想要做的。所以如果这不是你想要的,为了正确理解你想要做什么,你能用一个示例电子表格来解释它吗?
  • @Tanaike 这工作得很好!非常感谢!现在看起来像这样:function notifySlackChannelOfFacilitatorAndNoteTaker() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Past"); if (!sheet.getRange("B2").getValue()) return; var channel = getChannel();
  • 感谢您的回复。我很高兴你的问题得到了解决。您可以将其发布为答案并接受吗?这样,它对有同样问题的用户很有用。
  • @Tanaike 完成。再次感谢您的帮助!!!
  • 感谢您的回复。

标签: google-apps-script google-sheets webhooks slack


【解决方案1】:

如果您不想在“过去”表中的“A2”没有值时运行脚本,可以通过将 if (!sheet.getRange("A2").getValue()) return 放在 var channel = getChannel() 之前来实现。工作完美!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多