Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Trying to have an onEdit trigger with GScript that would send a slack message

I've been trying to automate sending messages when some of my reports are uploaded through a checkbox trigger on Gscript. (Idea being report is updated-> User check the checkbox -> message is sent to slack)

So far, I've been able to create the script to send the message to slack, and even reset my checkboxes at night. The only issue I have is with the trigger.

I've used onEdit as a trigger, on my three checkboxes (A1,B1,C1):

function onEdit(e) {
  const as = e.source.getActiveSheet();
  const cell = e.range.getA1Notation();

  if(as.getName() == 'Sheet1' && cell.getRange('A1').ischecked() === true) {
    SendSlackMessage1();
  }
  else if (as.getName() == 'Sheet1' && cell.getRange('B1').ischecked() === true) {
    SendSlackMessage2();
  }
  else if (as.getName() == 'Sheet1' && cell.getRange('C1').ischecked() === true) {
    SendSlackMessage3();
  }}

As a reference here's the function to send messages :

function SendSlackMessage1() {
  const url = "https://hooks.slack.com/services/XXXXXXXXXXXXX";
  const params = {
    method: "post",
    contentType: "application/json",
    payload: JSON.stringify({
      "text":"Report A has been uploaded"
    })
  }

  const sendMsg = UrlFetchApp.fetch(url, params)
  var respCode = sendMsg.getResponseCode()
  Logger.log(sendMsg)
  Logger.log(respCode)
}

(All of SendSlackmessages function are the same except for text (Report A/B/C), and I've already authorized the functions to run on google.

So obviously, I'm doing something wrong, but what ? my first guess would be that I'm not properly defining the e range, but I don't know what to do next.

I'd be grateful if someone can give me a headstart or pointing me at where to look.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

back here because I've found the answers to my issues, and I want to share:

First of all, with an onEdit(e), you can't run it within the editor, hence my issues finding what was wrong. So, here's the code i'm using now, that works :

function SendSlackMessages(e) {
  const col = e.range.getColumn();
  const row = e.range.getRow();
  const checked = e.range.isChecked();
  var rtexte = "";
  const url = "https://hooks.slack.com/services/XXXX";


  if (col == 3 && row == 3){
    rtexte = "Short EPGS Horizon";
  } else if (col == 4 && row == 3){
    rtexte = "Generic Days with EPGS Data";
  } else if (col == 5 && row == 3){
   rtexte = "TBAs";
  } else {return false;}

  const params = {
      method: "post",
      contentType: "application/json",
      payload: JSON.stringify({
        "text":"Report "+ rtexte +" has been uploaded"
      })
    }

  if(checked === true){
    const sendMsg = UrlFetchApp.fetch(url, params);
    var respCode = sendMsg.getResponseCode();
    Logger.log(sendMsg);
    Logger.log(respCode);
  }
}

On this function, I have added an onEdit trigger, and it works, send slack messages only when the edit happens in the right cells.

Hope it can help someone in the future.

about 4 years ago · Juan Pablo Isaza Report

0

  1. The simply trigger onEdit() can't be used to perform an action that needs an authorization. Probably an installed variant of the trigger can do. It can be the one of the causes of the problem.

  2. The code looks weird. Are you sure it will work at all?

  • The constant cell is a string. Thus you can't get a range this way: cell.getRange('A1'). Perhaps you want to change its definitions this way (if you need the constant at all):
const cell = e.range;
  • cell.getRange('A1') probably it should be:
as.getRange('A1')
  • And I'm not sure if ischecked() will work. It should be isChecked():

https://developers.google.com/apps-script/reference/spreadsheet/range#ischecked

etc...

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!