スプレッドシートで特定の行列の値が特定の変更をされたらSlackに通知する


以下の sendNotification をトリガーで「変更時」に発火するように設定


function sendNotification() {
  const ss = SpreadsheetApp.getActiveSpreadsheet()
  const sheet = ss.getSheetByName("シート1")
  const cell = ss.getActiveCell().getA1Notation()
  const changedCellRow = sheet.getActiveCell().getRowIndex()
  const value = sheet.getRange(cell).getValue()
  const message = createMessage(changedCellRow)
  if(value === "特定の変更") {
    notificationToSlack(message)
  }
}

function createMessage(changedCellRow) {
  return "ステータスが変更されました"
}

function notificationToSlack(message) {
  const postUrl = "https://hooks.slack.com/services/XXXXXXXX/XXXXXXXX/XXXXXXXXXXXXXXXX"
  const userName = 'マイメロ'
  const room = "#random"
  const jsonData =
  {
     "username" : userName,
     "text": message,
     "channel": room,
  }
  const payload = JSON.stringify(jsonData)
  const options =
  {
    "method" : "post",
    "contentType" : "application/json",
    "payload" : payload
  }
  UrlFetchApp.fetch(postUrl, options)
}