進階 Google Workspace 事件服務
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Advanced Google Workspace Events 服務可讓您在 Apps Script 中使用 Google Workspace Events API。有了這個 API,您就能訂閱 Google Workspace 資源,掌握您感興趣的相關事件資訊。事件是指對資源進行的變更,例如建立、更新或刪除。
必要條件
參考資料
如要進一步瞭解這項服務,請參閱 Google Workspace Events API 參考說明文件。與 Apps Script 中的所有進階服務一樣,Google Workspace Events 服務使用的物件、方法和參數,都與公開 API 相同。
程式碼範例
這些範例說明如何使用進階服務,執行常見的 Google Workspace Events API 動作。
建立訂閱項目
如要建立 Google Workspace 資源的訂閱項目,請將下列函式新增至 Apps Script 專案的程式碼:
可列出訂閱項目
如要列出依事件類型和目標資源篩選的訂閱項目,請將下列函式新增至 Apps Script 專案的程式碼:
可取得訂閱項目
如要取得訂閱資訊,請在 Apps Script 專案的程式碼中加入下列函式:
更新訂閱項目
如要更新或續訂訂閱項目,請在 Apps Script 專案的程式碼中加入下列函式:
重新啟用訂閱方案
如要重新啟用訂閱項目,請在 Apps Script 專案的程式碼中加入下列函式:
刪除訂閱項目
如要刪除訂閱項目,請將下列函式新增至 Apps Script 專案的程式碼:
取得作業
大多數 Google Workspace Events API 方法都會傳回長時間執行的作業。如要判斷作業狀態,可以使用 operations.get()
方法。
如要取得作業相關資訊,請將下列函式新增至 Apps Script 專案的程式碼:
如要取得作業名稱,請使用 Google Workspace Events API 方法 (例如 subscriptions.create()
或 subscriptions.patch()
) 傳回的 name
欄位值。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eThe Advanced Google Workspace Events service enables you to use the Google Workspace Events API in Apps Script to subscribe to Google Workspace resources and receive relevant events.\u003c/p\u003e\n"],["\u003cp\u003eTo utilize this service, you need an Apps Script project linked to a standard Google Cloud project, a Pub/Sub topic for receiving events, and specific authorization scopes in your project's \u003ccode\u003eappsscript.json\u003c/code\u003e file.\u003c/p\u003e\n"],["\u003cp\u003eThe service offers functionality to create, list, get, update, reactivate, and delete subscriptions, as well as retrieve information about long-running operations through the provided sample code snippets.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Workspace Events represent changes to resources such as creation, updates, or deletions, providing insights into resource activities within your Workspace environment.\u003c/p\u003e\n"]]],[],null,["The Advanced Google Workspace Events service lets you use the\n[Google Workspace Events API](/workspace/events) in\nApps Script. This API lets you subscribe to\nGoogle Workspace resources so that you receive relevant events that you're\ninterested in. Events represent changes to resources, such as when resources are\ncreated, updated, or deleted.\n\nPrerequisites\n\n- An Apps Script project using a standard Google Cloud project instead of the default one created automatically by Apps Script.\n- A [Pub/Sub](https://cloud.google.com/pubsub/docs/) topic created in the same Google Cloud project to receive subscription events. To create a Pub/Sub topic, see [Create and subscribe to a Pub/Sub topic](/workspace/events/guides/create-subscription#pubsub).\n- To subscribe to Chat events, you must have a Google Chat app configured on the Chat API configuration page in the Google Cloud console. To create a Google Chat app, see [Build a Google Chat app with Apps Script](/apps-script/quickstart/chat-app).\n- The necessary authorization scopes added to the Apps Script\n project's `appsscript.json` file. The necessary scopes depend on the types of\n the subscriptions' target resources and events. For details, see\n [Choose Google Workspace Events API scopes](/workspace/events/guides/auth).\n For example:\n\n \"oauthScopes\": [\n \"https://www.googleapis.com/auth/chat.messages.readonly\"\n ]\n\n| **Note:** This is an advanced service that you must [turn on before use](/apps-script/guides/services/advanced).\n\nReference\n\nFor more information about this service, see the\n[Google Workspace Events API reference documentation](/workspace/events/reference/rest/v1).\nLike all advanced services in Apps Script, the\nGoogle Workspace Events service uses the same objects, methods, and\nparameters as the public API.\n\nSample code\n\nThese samples show you how to perform common\n[Google Workspace Events API](/workspace/events)\nactions using the advanced service.\n\nCreate a subscription\n\nTo create a subscription to a Google Workspace resource, add\nthe following function to the Apps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Creates a subscription to receive events about a Google Workspace resource.\n * For a list of supported resources and event types, see the\n * [Google Workspace Events API Overview](https://developers.google.com/workspace/events#supported-events).\n * For additional information, see the\n * [subscriptions.create](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/create)\n * method reference.\n * @param {!string} targetResource The full resource name of the Google Workspace resource to subscribe to.\n * @param {!string|!Array\u003cstring\u003e} eventTypes The types of events to receive about the resource.\n * @param {!string} pubsubTopic The resource name of the Pub/Sub topic that receives events from the subscription.\n */\nfunction createSubscription(targetResource, eventTypes, pubsubTopic) {\n try {\n const operation = WorkspaceEvents.Subscriptions.create({\n targetResource: targetResource,\n eventTypes: eventTypes,\n notificationEndpoint: {\n pubsubTopic: pubsubTopic,\n },\n });\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to create subscription with error %s', err.message);\n }\n}\n```\n\nList subscriptions\n\nTo list subscriptions filtered by event types and target resource,\nadd the following function to the Apps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Lists subscriptions created by the calling app filtered by one or more event types and optionally by a target resource.\n * For additional information, see the\n * [subscriptions.list](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/list)\n * method reference.\n * @param {!string} filter The query filter.\n */\nfunction listSubscriptions(filter) {\n try {\n const response = WorkspaceEvents.Subscriptions.list({ filter });\n console.log(response);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to list subscriptions with error %s', err.message);\n }\n}\n```\n\nGet subscription\n\nTo get information about a subscription, add the following function to\nthe Apps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Gets details about a subscription.\n * For additional information, see the\n * [subscriptions.get](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/get)\n * method reference.\n * @param {!string} name The resource name of the subscription.\n */\nfunction getSubscription(name) {\n try {\n const subscription = WorkspaceEvents.Subscriptions.get(name);\n console.log(subscription);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to get subscription with error %s', err.message);\n }\n}\n```\n\nUpdate subscription\n\nTo update or renew a subscription, add the following function to the\nApps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Updates an existing subscription.\n * This can be used to renew a subscription that is about to expire.\n * For additional information, see the\n * [subscriptions.patch](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/patch)\n * method reference.\n * @param {!string} name The resource name of the subscription.\n */\nfunction patchSubscription(name) {\n try {\n const operation = WorkspaceEvents.Subscriptions.patch({\n // Setting the TTL to 0 seconds extends the subscription to its maximum expiration time.\n ttl: '0s',\n }, name);\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to update subscription with error %s', err.message);\n }\n}\n```\n\nReactivate subscription\n\nTo reactivate a subscription, add the following function to the\nApps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Reactivates a suspended subscription.\n * Before reactivating, you must resolve any errors with the subscription.\n * For additional information, see the\n * [subscriptions.reactivate](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/reactivate)\n * method reference.\n * @param {!string} name The resource name of the subscription.\n */\nfunction reactivateSubscription(name) {\n try {\n const operation = WorkspaceEvents.Subscriptions.reactivate({}, name);\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to reactivate subscription with error %s', err.message);\n }\n}\n```\n\nDelete subscription\n\nTo delete a subscription, add the following function to the\nApps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Deletes a subscription.\n * For additional information, see the\n * [subscriptions.delete](https://developers.google.com/workspace/events/reference/rest/v1/subscriptions/delete)\n * method reference.\n * @param {!string} name The resource name of the subscription.\n */\nfunction deleteSubscription(name) {\n try {\n const operation = WorkspaceEvents.Subscriptions.remove(name);\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to delete subscription with error %s', err.message);\n }\n}\n```\n\nGet operation\n\nMost Google Workspace Events API methods return a\n[long-running operation](/workspace/events/reference/rest/v1/operations).\nTo determine the status of the operation, you can use the\n[`operations.get()`](/workspace/events/reference/rest/v1/operations/get)\nmethod.\n\nTo get information about an operation, add the following function\nto the Apps Script project's code: \nadvanced/events.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/events.gs) \n\n```javascript\n/**\n * Gets details about an operation returned by one of the methods on the subscription\n * resource of the Google Workspace Events API.\n * For additional information, see the\n * [operations.get](https://developers.google.com/workspace/events/reference/rest/v1/operations/get)\n * method reference.\n * @param {!string} name The resource name of the operation.\n */\nfunction getOperation(name) {\n try {\n const operation = WorkspaceEvents.Operations.get(name);\n console.log(operation);\n } catch (err) {\n // TODO (developer) - Handle exception\n console.log('Failed to get operation with error %s', err.message);\n }\n}\n```\n\nTo get the name of an operation, use the value from the `name` field returned\nfrom one of the Google Workspace Events API methods, such as\n[`subscriptions.create()`](#create-subscription) or\n[`subscriptions.patch()`](#update-subscription)."]]