Kurt
Kurt

Reputation: 23

How to run a function in Google Script if Cell Value equals specific word

I have two scripts in a project. Script #1 is on a time trigger so it runs every 5 minutes. If a cell value in my spreadsheet is above a set value it triggers and sends an email. After the email is sent it sets a specific cell to say "Triggered"

function myBTCupFunction() {
  var ss = SpreadsheetApp.openById("MYIDHERE"); 
  var sheet = ss.getSheetByName("Trading");
  var cell = sheet.getRange('I4')

  if(sheet.getRange(5,12).getValue()>sheet.getRange(4,9).getValue()){      //change row and column in get range to match what you need
    MailApp.sendEmail("[email protected]", "Bitcoin Alert!", "Bitcoin has experienced a large upside move in the last hour.");

  if(sheet.getRange(5,12).getValue()>sheet.getRange(4,9).getValue()){      cell.setValue("Triggered") ;


                                                                    }}}

So if my cell value is set to "Triggered" I want script #2 to run which is below. This is the part of the code that I do not understand.

function postMessageToDiscord(message) {

  message = message || "Bitcoin has experienced a large upside move.";

  var discordUrl = 'https://discordapp.com/api/webhooks/sdjfkjjhdklfkjhd';
  var payload = JSON.stringify({content: message});

  var params = {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    payload: payload,
    muteHttpExceptions: true
  };

  var response = UrlFetchApp.fetch(discordUrl, params);

  Logger.log(response.getContentText());

}

Upvotes: 0

Views: 11240

Answers (1)

Brian
Brian

Reputation: 4354

It would be easier to call the second function when myBTCupFunction() runs. That way, you don't have to check backwards with a second trigger.

Example:

function myBTCupFunction() {
  var ss = SpreadsheetApp.openById("MYIDHERE"); 
  var sheet = ss.getSheetByName("Trading");
  var cell = sheet.getRange('I4')

  if(sheet.getRange(5,12).getValue()>sheet.getRange(4,9).getValue()) {      
    //change row and column in get range to match what you need
    MailApp.sendEmail("[email protected]", "Bitcoin Alert!", "Bitcoin has experienced a large upside move in the last hour.");
    cell.setValue("Triggered");
    postMessageToDiscord("Your string message here");
  }
}

Upvotes: 1

Related Questions