שירות מתקדם של Google Workspace Event
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
השירות 'אירועים מתקדמים ב-Google Workspace' מאפשר לכם להשתמש ב-Google Workspace Events API ב-Apps Script. ה-API הזה מאפשר להירשם למשאבי Google Workspace כדי לקבל אירועים רלוונטיים שמעניינים אתכם. אירועים מייצגים שינויים במשאבים, למשל כשמשאבים נוצרים, מתעדכנים או נמחקים.
דרישות מוקדמות
חומרי עזר
מידע נוסף על השירות הזה זמין במאמרי העזרה של Google Workspace Events API.
בדומה לכל השירותים המתקדמים ב-Apps Script, שירות האירועים של Google Workspace משתמש באותם אובייקטים, שיטות ופרמטרים כמו ממשק ה-API הציבורי.
קוד לדוגמה
בדוגמאות האלה מוצגות פעולות נפוצות של Google Workspace Events API באמצעות השירות המתקדם.
יצירת מינוי
כדי ליצור מינוי למשאב ב-Google Workspace, מוסיפים את הפונקציה הבאה לקוד של פרויקט Apps Script:
הצגת רשימה של מינויים
כדי להציג רשימה של מינויים שסוננו לפי סוגי אירועים ומשאב יעד, מוסיפים את הפונקציה הבאה לקוד של פרויקט Apps Script:
קבלת מינוי
כדי לקבל מידע על מינוי, מוסיפים את הפונקציה הבאה לקוד של פרויקט Apps Script:
עדכון המינוי
כדי לעדכן או לחדש מינוי, מוסיפים את הפונקציה הבאה לקוד של פרויקט Apps Script:
הפעלה מחדש של המינוי
כדי להפעיל מחדש מינוי, מוסיפים את הפונקציה הבאה לקוד של פרויקט Apps Script:
מחיקת מינוי
כדי למחוק מינוי, מוסיפים את הפונקציה הבאה לקוד של פרויקט Apps Script:
קבלת פעולה
רוב ה-methods של Google Workspace Events API מחזירות פעולה ממושכת.
כדי לזהות את סטטוס הפעולה, אפשר להשתמש בשיטה operations.get()
.
כדי לקבל מידע על פעולה, מוסיפים את הפונקציה הבאה לקוד של פרויקט Apps Script:
כדי לקבל את השם של פעולה, משתמשים בערך מהשדה name
שמוחזר מאחת מהשיטות של Google Workspace Events API, כמו subscriptions.create()
או subscriptions.patch()
.
אלא אם צוין אחרת, התוכן של דף זה הוא ברישיון Creative Commons Attribution 4.0 ודוגמאות הקוד הן ברישיון Apache 2.0. לפרטים, ניתן לעיין במדיניות האתר Google Developers. Java הוא סימן מסחרי רשום של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-08-31 (שעון UTC).
[null,null,["עדכון אחרון: 2025-08-31 (שעון UTC)."],[[["\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)."]]