Slack > Gsheet > VLookup > Slack

Viewed 643

I've been struggling with this for the past couple of weeks, so I'm really hoping someone can help me with this. Also I'm very new to code, this basically sparked my interest in it.

What I'm trying to do is write a Google Apps Script for a Google Sheet and link that to an inbound and outbound Slack webhook, so that I can post in Slack a command like a PO#, and it will fill in a spreadsheet that has vlookup columns built in with an array formula, then I want to post back in Slack what data was pulled from the Vlookup. I originally had the idea from this link.

I've gotten this really close, but I cant figure out how to get the script to post the Vlookup data into Slack. Here is a photo of what it looks like, where the highlighted cells are the vlookup columns:

Example Photo

And then here is the Google Apps Script code I'm working on:

function doPost(req) {
  var sheets = SpreadsheetApp.openById('googlesheet_link');
  var params = req.parameters;

  var nR = getNextRow(sheets) + 1;

  if (params.token == "slack_webhook") {

    //ALREADY IN SHEETS
    var supplier

    // PROCESS TEXT FROM MESSAGE
    var textRaw = String(params.text).replace(/^\s*update\s*:*\s*/gi,'');
    var text = textRaw.split(/\s*;\s*/g);

    // FALL BACK TO DEFAULT TEXT IF NO UPDATE PROVIDED
    var project   = text[0] || "No Project Specified";
    var purchaseorder = text[1] || "No update provided";
    var today     = text[2] || "No update provided";
    var blockers  = text[3] || "No update provided";

    // RECORD TIMESTAMP AND USER NAME IN SPREADSHEET
    sheets.getRangeByName('timestamp').getCell(nR,1).setValue(new Date());
    sheets.getRangeByName('user').getCell(nR,1).setValue(params.user_name);

    // RECORD UPDATE INFORMATION INTO SPREADSHEET
    sheets.getRangeByName('project').getCell(nR,1).setValue(project);
    sheets.getRangeByName('purchaseorder').getCell(nR,1).setValue(purchaseorder);
    sheets.getRangeByName('today').getCell(nR,1).setValue(today);
    sheets.getRangeByName('blockers').getCell(nR,1).setValue(blockers);


    var channel = "updates";

    postResponse(channel,params.channel_name,project,params.user_name,purchaseorder,today,blockers,supplier);

  } else {
    return;
  }
}

function getNextRow(sheets) {
  var timestamps = sheets.getRangeByName("timestamp").getValues();
  for (i in timestamps) {
    if(timestamps[i][0] == "") {
      return Number(i);
      break;
    }
  }

  //AND THEN THIS IS THE RETURN//
  function postResponse(channel, srcChannel, project, userName, purchaseorder, today, blockers,supplier) {

  var payload = {
    "channel": "#" + channel,
    "username": "Trackbot3000",
    "icon_emoji": ":robot_face:",
    "link_names": 1,
    "attachments":[
       {
          "fallback": "This is an update from a Slackbot integrated into your organization. Your client chose not to show the attachment.",
          "pretext": "*" + project + "* posted an update for stand-up. (Posted by @" + userName + " in #" + srcChannel + ")",
          "mrkdwn_in": ["pretext"],
          "color": "#D00000",
          "fields":[
             {
                "title":"Yesterday",
                "value": purchaseorder,
                "short":false
             },
             {
                "title":"Today",
                "value": today,
                "short":false
             },
             {
                "title":"Blockers",
                "value": blockers,
                "short": false
             },
             {
                "title":"Supplier",
                "value": supplier,
                "short": false
             }
          ]
       }
    ]
  };

  var url = 'slack_webhook_link';
  var options = {
    'method': 'post',
    'payload': JSON.stringify(payload)
  };

  var response = UrlFetchApp.fetch(url,options);
}
1 Answers

I semi-resolved this. I ended up just using the first portion of the Google Script App code to fill in the data from slack onto the google sheet and got rid of the //AND THEN THIS IS THE RETURN// portion. Then I used an IF and ArrayFormula for the Vlookup columns in the google sheet. Then I used automate.io for a new row trigger to send it back into slack. Just in case anyone else is looking to do this.

Related