松弛> Gsheet> VLookup>松弛

时间:2018-09-11 17:01:01

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

在过去的两周里,我一直在为此苦苦挣扎,所以我真的希望有人能对此提供帮助。另外,我对代码非常陌生,这基本上激发了我对其的兴趣。

我想做的是为Google表格编写一个Google Apps脚本,并将其链接到入站和出站Slack Webhook,这样我就可以在Slack中发布PO#之类的命令,并且它将填充一个具有vlookup列且内置有数组公式的电子表格,那么我想在Slack中发布从Vlookup提取的数据。我最初的想法是from this link

我已经很接近了,但是我不知道如何获取脚本来将Vlookup数据发布到Slack中。这是一张看起来像的照片,其中突出显示的单元格是vlookup列:

Example Photo

然后是我正在使用的Google Apps脚本代码:

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 个答案:

答案 0 :(得分:0)

我半解决了这个问题。我最终只是使用Google Script App代码的第一部分将数据从松弛状态填充到google工作表中,并且摆脱了///然后这就是RETURN //部分。然后,我将IF和ArrayFormula用于google工作表中的Vlookup列。然后,我将automate.io用于新的行触发器,以将其发送回松弛状态。以防万一其他人正在寻找这样做。