Naiara Caracciolo
Naiara Caracciolo

Reputation: 29

Get a slack notification when spreadsheets changes

I would like to get a notification in my slack when a cell changes in column G in all the sheets from a document I have in google sheets. I currently have this code where I get an email. If I can keep gmail + slack notification would be awesome.

function onSpeEdit(e) {
  var sh = e.source.getActiveSheet();
  var rng = e.source.getActiveRange();
  var col = 7
  if (rng.getColumn() == col) {
    MailApp.sendEmail(
      '[email protected]',
      `Cambio DSID`,
      `Cambio en la celda ${rng.getA1Notation()} antes era "${e.oldValue}" ahora es "${e.value}" `);
  }
}

Upvotes: 0

Views: 1296

Answers (1)

Mike Steelson
Mike Steelson

Reputation: 15318

You will need to create a custom app in Slack

Try

function onSpeEdit(e) {
  var sh = e.source.getActiveSheet();
  var rng = e.source.getActiveRange();
  var col = 7
  if (rng.getColumn() == col) {

    // email
    MailApp.sendEmail(
      '[email protected]',
      `Cambio DSID`,
      `Cambio en la celda '${sh.getName()}'!${rng.getA1Notation()} antes era "${e.oldValue}" ahora es "${e.value}" `);

    // slack
    let message = {
      "blocks": [
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "Cambio DSID"
          }
        },
        {
          "type": "divider"
        },
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": `Cambio en la celda '${sh.getName()}'!${rng.getA1Notation()} `
          }
        },
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": `antes era "${e.oldValue}" `
          }
        },
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": `ahora es "${e.value}" `
          }
        }
      ]
    };
    const webhook = ""; //Paste your webhook URL here
    sendSlackAlert(webhook, message)

  }
}
function sendSlackAlert(webhook, payload) {
  var options = {
    "method": "post",
    "contentType": "application/json",
    "muteHttpExceptions": true,
    "payload": JSON.stringify(payload)
  };

  try {
    UrlFetchApp.fetch(webhook, options);
  } catch (e) {
    Logger.log(e);
  }
}

Upvotes: 0

Related Questions