Ricky Adams
Ricky Adams

Reputation: 175

Google Apps Script IF trigger Push to Slack

I have a google sheet that interactive to-do list tied together with dates, and if one of the dates goes past due it copies over to a separate tab within the sheet called 'Past'. I use the code below to push these Past Due Alerts into a Slack channel with a daily setup Google Apps Trigger, but what I want to do is add in an IF variable that would only post if there was a row in that tab to post. For example, if A2=""then don't push to slack, if A2=Not Blank push to slack. Any help would be greatly appreciated!

Here is the code I'm working with:

 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";
 }
}

And here is what it looks like when there is data in A2 when pushed to Slack: enter image description here

And then here is what it looks like when there isn't data in A2 and it pushed to Slack: enter image description here

Thanks!

Upvotes: 0

Views: 376

Answers (1)

Ricky Adams
Ricky Adams

Reputation: 175

If you don't want to run the script when "A2" in a sheet of "Past" has no value, you can achieve it by putting if (!sheet.getRange("A2").getValue()) return before var channel = getChannel(). Worked perfectly!

Upvotes: 1

Related Questions