ทริกเกอร์สำหรับส่วนเสริมของ Google Workspace
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ทริกเกอร์ Apps Script จะทำให้ฟังก์ชันสคริปต์ที่ระบุ (ฟังก์ชันทริกเกอร์) ทำงานเมื่อใดก็ตามที่เกิดเหตุการณ์ที่ระบุ
มีเพียงบางเหตุการณ์เท่านั้นที่ทำให้ทริกเกอร์ทำงาน และแอปพลิเคชัน Google Workspace แต่ละรายการรองรับชุดเหตุการณ์ที่แตกต่างกัน
เมื่อทริกเกอร์เริ่มทำงาน ระบบจะสร้างออบเจ็กต์เหตุการณ์ โครงสร้าง JSON นี้
มีรายละเอียดเกี่ยวกับเหตุการณ์ที่เกิดขึ้น ระบบจะจัดระเบียบข้อมูลในโครงสร้างออบเจ็กต์เหตุการณ์
แตกต่างกันไปตามประเภททริกเกอร์
เมื่อสร้างออบเจ็กต์เหตุการณ์แล้ว Apps Script จะส่งออบเจ็กต์ดังกล่าวเป็นพารามิเตอร์ไปยัง
ฟังก์ชันทริกเกอร์ ฟังก์ชันทริกเกอร์คือฟังก์ชันเรียกกลับที่คุณต้อง
ใช้เองเพื่อดำเนินการที่เหมาะสมเพื่อตอบสนองต่อ
เหตุการณ์ ตัวอย่างเช่น ในส่วนเสริม Google Workspace ที่ขยาย Gmail คุณสามารถ
กำหนดทริกเกอร์ที่สร้างอินเทอร์เฟซการ์ดใหม่เมื่อผู้ใช้เปิดชุดข้อความ
ได้ ในกรณีนี้ คุณจะใช้ฟังก์ชันเรียกกลับตามบริบทเพื่อสร้าง
การ์ดที่ประกอบกันเป็น UI ใหม่โดยใช้ข้อมูลที่ส่งใน
ออบเจ็กต์เหตุการณ์
หน้านี้มีหลักเกณฑ์เกี่ยวกับการใช้ทริกเกอร์ในโปรเจ็กต์ส่วนเสริมของ Google Workspace
ทริกเกอร์ไฟล์ Manifest
ปัจจุบันส่วนเสริม Google Workspace ไม่สามารถใช้ทริกเกอร์อย่างง่ายของ Apps Script ได้ ซึ่งต่างจากส่วนเสริมของเครื่องมือแก้ไข
แต่จะใช้ทริกเกอร์ที่ออกแบบมาสำหรับ
ส่วนเสริม Google Workspace โดยเฉพาะแทน นั่นคือทริกเกอร์ของไฟล์ Manifest
ทริกเกอร์ของไฟล์ Manifest จะกำหนดไว้ทั้งหมดในไฟล์ Manifest ของส่วนเสริม Google Workspace ตัวอย่างทริกเกอร์ของไฟล์ Manifest มีดังนี้
- ทริกเกอร์หน้าแรกที่สร้างและแสดงหน้าแรกของส่วนเสริม
- ทริกเกอร์เปิดกิจกรรมในปฏิทินที่แสดงการ์ดใหม่หรือดำเนินการอื่นๆ
เมื่อเปิดกิจกรรมในปฏิทิน
- ทริกเกอร์การอัปเดตกิจกรรมในปฏิทินที่แสดงการ์ดใหม่หรือดำเนินการอื่นๆ
เมื่อผู้ใช้แก้ไขและบันทึกกิจกรรมในปฏิทิน
- ทริกเกอร์ Drive onItemsSelected ที่แสดงการ์ดใหม่หรือดำเนินการอื่นๆ
เมื่อผู้ใช้เลือกไฟล์หรือโฟลเดอร์อย่างน้อย 1 รายการในไดรฟ์
- ทริกเกอร์การเขียน Gmail ที่แสดงการ์ดส่วนเสริมเมื่อ
ผู้ใช้เปิดส่วนเสริมในหน้าต่างการเขียน Gmail
- ทริกเกอร์ตามบริบทของ Gmail ที่แสดงการ์ดใหม่หรือดำเนินการอื่นๆ
เมื่อผู้ใช้เปิดข้อความ Gmail
- ทริกเกอร์ Editor onFileScopeGranted ที่แสดงการ์ดใหม่เมื่อผู้ใช้
ให้สิทธิ์สำหรับ
drive.file
ขอบเขต OAuth ในเอกสาร Editor ปัจจุบัน
ในรายการด้านบน มีเพียงทริกเกอร์หน้าแรกเท่านั้นที่เป็นแบบไม่ตามบริบท ส่วนที่เหลือเป็นทริกเกอร์ตามบริบท ดูข้อมูลเพิ่มเติมเกี่ยวกับคำจำกัดความของทริกเกอร์ในไฟล์ Manifest ได้ที่ไฟล์ Manifest
นอกจากทริกเกอร์ในไฟล์ Manifest แล้ว ส่วนเสริม Google Workspace
ยังใช้ Apps Script
ทริกเกอร์ที่ติดตั้งได้ด้วย
ข้อจำกัด
ทริกเกอร์ของไฟล์ Manifest มีข้อจำกัดบางอย่างในการใช้งาน
- ทริกเกอร์เหล่านี้ใช้ในโปรเจ็กต์ส่วนเสริมของ Google Workspace เท่านั้น และไม่มีวัตถุประสงค์ในการใช้งานในแอปพลิเคชันอื่นๆ
- เนื่องจากมีการกำหนดไว้ในไฟล์ Manifest ของส่วนเสริม ไม่ใช่ในโค้ด คุณจึงใช้บริการ
Script
ของ Apps Script เพื่อสร้างหรือแก้ไขทริกเกอร์เหล่านี้ไม่ได้
- ปัจจุบันทริกเกอร์ตามบริบทของ Gmail มีได้เพียง
unconditional
เกณฑ์เท่านั้น
ซึ่งหมายความว่าทริกเกอร์ตามบริบทจะทํางานสําหรับข้อความอีเมลทุกฉบับโดยไม่คํานึงถึงเนื้อหา
- ส่วนเสริมแต่ละรายการจะมีทริกเกอร์แต่ละประเภทได้เพียง 1 รายการต่อผู้ใช้ 1 รายการต่อเอกสาร 1 รายการ
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-31 UTC
[null,null,["อัปเดตล่าสุด 2025-07-31 UTC"],[[["\u003cp\u003eApps Script triggers execute a specific function when a defined event occurs within a Google Workspace application.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Workspace add-ons utilize manifest triggers, defined in the add-on's manifest, to respond to events like opening a document or composing an email.\u003c/p\u003e\n"],["\u003cp\u003eManifest triggers can be contextual, reacting to specific user actions within a document or application, or non-contextual, like those that build the add-on's homepage.\u003c/p\u003e\n"],["\u003cp\u003eWhile manifest triggers offer event-driven functionality, they have limitations, including restrictions on their use and creation methods.\u003c/p\u003e\n"]]],["Apps Script triggers initiate a script function when specific events occur, creating an event object with event details. This object is then passed to a user-implemented trigger function, enabling actions based on the event. Google Workspace add-ons use manifest triggers, defined in the add-on's manifest, to trigger actions like displaying cards on homepage, when opening/updating Calendar events, selecting Drive items, or opening a message in Gmail. Contextual triggers and installable triggers are also supported but with some restrictions.\n"],null,["# Triggers for Google Workspace add-ons\n\n[Apps Script triggers](/apps-script/guides/triggers) cause a specified script\nfunction (the *trigger function*) to execute whenever a specified event\noccurs. Only certain events can cause triggers to fire, and each\nGoogle Workspace application supports a different set of events.\n\nWhen a trigger fires, an *event object* is created. This JSON structure\ncontains details about the event that occurred. The information in the event\nobject structure is organized differently based on the trigger type.\n\nOnce the event object is created, Apps Script passes it as a parameter to the\ntrigger function. The trigger function is a callback function that you must\nimplement yourself, to take whatever actions are appropriate to respond to the\nevent. For example, in a\nGoogle Workspace add-on that extends Gmail, you can\ndefine a trigger that creates a new card interface when the user opens a message\nthread. In this case, you implement a contextual callback function to create\nthe cards making up the new UI using the data passed in the\n[event object](/workspace/add-ons/concepts/event-objects).\n\nThis page provides guidelines on using triggers in\nGoogle Workspace\nadd-on projects.\n\nManifest triggers\n-----------------\n\nUnlike Editor add-ons, Google Workspace add-ons\ncurrently can't use Apps Script [simple triggers](/apps-script/guides/triggers)\nInstead, they use triggers designed specifically for\nGoogle Workspace add-ons: *manifest triggers*.\n\nManifest triggers are defined completely in the\nGoogle Workspace add-on's\n[manifest](/workspace/add-ons/concepts/workspace-manifests). Examples of manifest triggers include the\nfollowing:\n\n- **Homepage triggers** that build and display the add-on homepage.\n- **Calendar eventOpen triggers** that display a new card or take other actions when a Calendar event is opened.\n- **Calendar eventUpdate triggers** that display a new card or take other actions when a user edits and saves a Calendar event.\n- **Drive onItemsSelected triggers** that display a new card or take other actions when a user selects one or more files or folders in Drive.\n- **Gmail compose triggers** that display an add-on card when the user opens the add-on in the Gmail compose window.\n- **Gmail contextual triggers** that display a new card or take other actions when the user opens a Gmail message.\n- **Editor onFileScopeGranted triggers** that display a new card when users grant authorization for the `drive.file` OAuth scope in the current Editor document.\n\nIn the above list, only homepage triggers are non-contextual; the rest are\ncontextual triggers. See [Manifest](/workspace/add-ons/concepts/workspace-manifests)\nfor more information about manifest trigger definitions.\n\nIn addition to manifest triggers, Google Workspace\nadd-ons can also use Apps Script\n[installable triggers](/apps-script/guides/triggers/installable).\n\n### Restrictions\n\nManifest triggers have certain restrictions to their use.\n\n- These triggers are only used in Google Workspace add-on projects; they have no purpose in any other application.\n- Since they are defined in the add-on manifest and not in its code, you can't use the Apps Script [`Script`](/apps-script/reference/script) service to create or modify these triggers.\n- Gmail contextual triggers currently can only have an `unconditional` criteria. This means contextual triggers fire for every email message, regardless of content.\n- Each add-on can only have one trigger of each type, per user, per document."]]