Class Trigger

觸發條件

指令碼觸發條件。

方法

方法傳回類型簡短說明
getEventType()EventType傳回觸發觸發條件的事件類型。
getHandlerFunction()String傳回觸發條件觸發時要呼叫的函式。
getTriggerSource()TriggerSource傳回會導致觸發條件啟動的事件來源。
getTriggerSourceId()String傳回來源專屬的 ID。
getUniqueId()String傳回一組專屬 ID,用來區分不同觸發條件。

內容詳盡的說明文件

getEventType()

傳回觸發觸發條件的事件類型。

var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
  if (triggers[i].getEventType() == ScriptApp.EventType.CLOCK) {
    // Some code here - other options are:
    // ScriptApp.EventType.ON_EDIT
    // ScriptApp.EventType.ON_FORM_SUBMIT
    // ScriptApp.EventType.ON_OPEN
  }
}

回攻員

EventType:要做為觸發條件的事件類型

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/script.scriptapp

getHandlerFunction()

傳回觸發條件啟動時要呼叫的函式。

// Create a trigger for the script.
ScriptApp.newTrigger('myFunction').forSpreadsheet('id of my spreadsheet').onEdit().create();
Logger.log(ScriptApp.getProjectTriggers()[0].getHandlerFunction()); // logs "myFunction"

回攻員

String:方法名稱

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/script.scriptapp

getTriggerSource()

傳回會導致觸發條件啟動的事件來源。

舉例來說,onEdit 試算表觸發條件會傳回 SPREADSHEETS,否則時間型觸發條件會傳回 CLOCK。

var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
  if (triggers[i].getTriggerSource() == ScriptApp.TriggerSource.CLOCK) {
    Logger.log(triggers[i].getUniqueId() + " source is clock");
  } else if (triggers[i].getTriggerSource() == ScriptApp.TriggerSource.SPREADSHEETS) {
    Logger.log(triggers[i].getUniqueId() + " source is spreadsheets");
  }
}

回攻員

TriggerSource:由發布商觸發

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/script.scriptapp

getTriggerSourceId()

傳回來源專屬的 ID。

舉例來說,如果觸發條件來源是試算表,這個值就會是試算表 ID。如為時鐘事件,則會傳回空值。

回攻員

String:這是發布者中屬於觸發事件的實體 ID

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/script.scriptapp

getUniqueId()

傳回一組專屬 ID,用來區分不同觸發條件。

回攻員

String:觸發條件的專屬 ID

授權

使用這個方法的指令碼必須取得以下一或多個範圍的授權:

  • https://www.googleapis.com/auth/script.scriptapp