Class Trigger
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Điều kiện kích hoạt
Một điều kiện kích hoạt tập lệnh.
Tài liệu chi tiết
getEventType()
Trả về loại sự kiện mà điều kiện kích hoạt sẽ kích hoạt.
const triggers = ScriptApp.getProjectTriggers();
for (let 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
}
}
Cầu thủ trả bóng
EventType
– loại sự kiện mà điều này là điều kiện kích hoạt
Ủy quyền
Các tập lệnh sử dụng phương thức này yêu cầu được uỷ quyền với một hoặc nhiều phạm vi sau:
-
https://www.googleapis.com/auth/script.scriptapp
getHandlerFunction()
Trả về hàm sẽ được gọi khi điều kiện kích hoạt được kích hoạt.
// Create a trigger for the script.
ScriptApp.newTrigger('myFunction')
.forSpreadsheet('id of my spreadsheet')
.onEdit()
.create();
Logger.log(ScriptApp.getProjectTriggers()[0]
.getHandlerFunction()); // logs "myFunction"
Cầu thủ trả bóng
String
– tên phương thức
Ủy quyền
Các tập lệnh sử dụng phương thức này yêu cầu được uỷ quyền với một hoặc nhiều phạm vi sau:
-
https://www.googleapis.com/auth/script.scriptapp
getTriggerSource()
Trả về nguồn sự kiện sẽ kích hoạt điều kiện kích hoạt.
Ví dụ: trình kích hoạt onEdit của bảng tính sẽ trả về SPREADSHEETS hoặc trình kích hoạt dựa trên thời gian sẽ trả về CLOCK.
const triggers = ScriptApp.getProjectTriggers();
for (let 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`);
}
}
Cầu thủ trả bóng
TriggerSource
– nhà xuất bản mà điều kiện kích hoạt này dành cho
Ủy quyền
Các tập lệnh sử dụng phương thức này yêu cầu được uỷ quyền với một hoặc nhiều phạm vi sau:
-
https://www.googleapis.com/auth/script.scriptapp
getTriggerSourceId()
Trả về mã nhận dạng dành riêng cho nguồn.
Ví dụ: nếu nguồn điều kiện kích hoạt là một bảng tính, thì đây sẽ là mã nhận dạng của bảng tính đó. Đối với các sự kiện đồng hồ, giá trị này sẽ trả về giá trị rỗng.
Cầu thủ trả bóng
String
– mã nhận dạng của thực thể trong nhà xuất bản mà đây là điều kiện kích hoạt
Ủy quyền
Các tập lệnh sử dụng phương thức này yêu cầu được uỷ quyền với một hoặc nhiều phạm vi sau:
-
https://www.googleapis.com/auth/script.scriptapp
getUniqueId()
Trả về một giá trị nhận dạng duy nhất có thể dùng để phân biệt các trình kích hoạt với nhau.
Cầu thủ trả bóng
String
– giá trị nhận dạng duy nhất của điều kiện kích hoạt
Ủy quyền
Các tập lệnh sử dụng phương thức này yêu cầu được uỷ quyền với một hoặc nhiều phạm vi sau:
-
https://www.googleapis.com/auth/script.scriptapp
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[[["\u003cp\u003eA script trigger is a function that automatically runs in response to a certain event, like opening a document, editing a cell, or a timed event.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eTrigger\u003c/code\u003e object provides methods to access information about the trigger such as its event type, associated function, source, source ID, and a unique identifier.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003egetEventType()\u003c/code\u003e, \u003ccode\u003egetHandlerFunction()\u003c/code\u003e, \u003ccode\u003egetTriggerSource()\u003c/code\u003e, \u003ccode\u003egetTriggerSourceId()\u003c/code\u003e, and \u003ccode\u003egetUniqueId()\u003c/code\u003e methods provide information on the trigger's configuration and behavior.\u003c/p\u003e\n"],["\u003cp\u003eAll methods associated with the Trigger object necessitate authorization with the scope \u003ccode\u003ehttps://www.googleapis.com/auth/script.scriptapp\u003c/code\u003e.\u003c/p\u003e\n"]]],["This document details the functionality of script triggers, which are automated actions. Key actions include retrieving information about a trigger using five methods: `getEventType()`, which identifies the event type that activates the trigger; `getHandlerFunction()`, which returns the function executed when the trigger fires; `getTriggerSource()`, which specifies the event source; `getTriggerSourceId()`, which provides the source's ID; and `getUniqueId()`, which offers a unique identifier for the trigger. All methods require specific authorization scopes.\n"],null,["# Class Trigger\n\nTrigger\n\nA script trigger. \n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------------------|---------------------------------------------------------------|---------------------------------------------------------------------------------------|\n| [getEventType()](#getEventType()) | [EventType](/apps-script/reference/script/event-type) | Returns the event type that the trigger fires on. |\n| [getHandlerFunction()](#getHandlerFunction()) | `String` | Returns the function that will be called when the trigger fires. |\n| [getTriggerSource()](#getTriggerSource()) | [TriggerSource](/apps-script/reference/script/trigger-source) | Returns the source of events that will cause the trigger to fire. |\n| [getTriggerSourceId()](#getTriggerSourceId()) | `String` | Returns the id specific to the source. |\n| [getUniqueId()](#getUniqueId()) | `String` | Returns a unique identifier that can be used to distinguish triggers from each other. |\n\nDetailed documentation\n----------------------\n\n### `get``Event``Type()`\n\nReturns the event type that the trigger fires on.\n\n```javascript\nconst triggers = ScriptApp.getProjectTriggers();\nfor (let i = 0; i \u003c triggers.length; i++) {\n if (triggers[i].getEventType() === ScriptApp.EventType.CLOCK) {\n // Some code here - other options are:\n // ScriptApp.EventType.ON_EDIT\n // ScriptApp.EventType.ON_FORM_SUBMIT\n // ScriptApp.EventType.ON_OPEN\n }\n}\n```\n\n#### Return\n\n\n[EventType](/apps-script/reference/script/event-type) --- the event type that this is a trigger for\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/script.scriptapp`\n\n*** ** * ** ***\n\n### `get``Handler``Function()`\n\nReturns the function that will be called when the trigger fires.\n\n```javascript\n// Create a trigger for the script.\nScriptApp.newTrigger('myFunction')\n .forSpreadsheet('id of my spreadsheet')\n .onEdit()\n .create();\nLogger.log(ScriptApp.getProjectTriggers()[0]\n .getHandlerFunction()); // logs \"myFunction\"\n```\n\n#### Return\n\n\n`String` --- the method name\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/script.scriptapp`\n\n*** ** * ** ***\n\n### `get``Trigger``Source()`\n\nReturns the source of events that will cause the trigger to fire.\n\nFor example, a spreadsheet onEdit trigger would return SPREADSHEETS, or a time based trigger\nwould return CLOCK.\n\n```javascript\nconst triggers = ScriptApp.getProjectTriggers();\nfor (let i = 0; i \u003c triggers.length; i++) {\n if (triggers[i].getTriggerSource() === ScriptApp.TriggerSource.CLOCK) {\n Logger.log(`${triggers[i].getUniqueId()} source is clock`);\n } else if (\n triggers[i].getTriggerSource() === ScriptApp.TriggerSource.SPREADSHEETS) {\n Logger.log(`${triggers[i].getUniqueId()} source is spreadsheets`);\n }\n}\n```\n\n#### Return\n\n\n[TriggerSource](/apps-script/reference/script/trigger-source) --- the publisher this is a trigger for\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/script.scriptapp`\n\n*** ** * ** ***\n\n### `get``Trigger``Source``Id()`\n\nReturns the id specific to the source.\n\nFor example, if the trigger source is a spreadsheet, this would be the id of the\nspreadsheet. For clock events this returns null.\n\n#### Return\n\n\n`String` --- the id of the entity in the publisher that this is a trigger for\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/script.scriptapp`\n\n*** ** * ** ***\n\n### `get``Unique``Id()`\n\nReturns a unique identifier that can be used to distinguish triggers from each other.\n\n#### Return\n\n\n`String` --- the unique identifier of the trigger\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/script.scriptapp`"]]