如果Cell Value等于特定单词,如何在Google Script中运行函数

时间:2018-05-04 15:35:35

标签: google-apps-script

我在项目中有两个脚本。脚本#1处于时间触发器上,因此它每5分钟运行一次。如果我的电子表格中的单元格值高于设定值,则会触发并发送电子邮件。发送电子邮件后,它会将特定单元格设置为“已触发”

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("johndoe@gmail.com", "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") ;


                                                                    }}}

因此,如果我的单元格值设置为“已触发”,我希望脚本#2运行在下面。这是我不理解的代码的一部分。

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());

}

1 个答案:

答案 0 :(得分:1)

myBTCupFunction()运行时调用第二个函数会更容易。这样,你就不必用第二个触发器向后检查。

示例:

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("johndoe@gmail.com", "Bitcoin Alert!", "Bitcoin has experienced a large upside move in the last hour.");
    cell.setValue("Triggered");
    postMessageToDiscord("Your string message here");
  }
}