Usługa Aktywność na Dysku Google
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Usługa Aktywność na Dysku Google umożliwia korzystanie z interfejsu Google Drive Activity API w Apps Script. Ten interfejs API umożliwia użytkownikom programowy dostęp do informacji o aktywności na Dysku Google.
Dokumentacja
Szczegółowe informacje o tej usłudze znajdziesz w dokumentacji referencyjnej interfejsu Google Drive Activity API. Podobnie jak wszystkie usługi zaawansowane w Apps Script, usługa Google Drive Activity używa tych samych obiektów, metod i parametrów co publiczny interfejs API. Więcej informacji znajdziesz w artykule Jak określane są sygnatury metod.
Aby zgłosić problemy i uzyskać inną pomoc, zapoznaj się z przewodnikiem pomocy dotyczącym aktywności na Dysku Google.
Przykładowy kod
Poniższy przykładowy kod korzysta z wersji 2 interfejsu API.
Otrzymywanie informacji o aktywności na Dysku Google
Ten przykład pobiera ostatnią aktywność na Dysku Google użytkownika i rejestruje informacje o czasie, osobie, działaniu i obiekcie każdej aktywności.
O ile nie stwierdzono inaczej, treść tej strony jest objęta licencją Creative Commons – uznanie autorstwa 4.0, a fragmenty kodu są dostępne na licencji Apache 2.0. Szczegółowe informacje na ten temat zawierają zasady dotyczące witryny Google Developers. Java jest zastrzeżonym znakiem towarowym firmy Oracle i jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-08-31 UTC.
[null,null,["Ostatnia aktualizacja: 2025-08-31 UTC."],[[["\u003cp\u003eThe Google Drive Activity service enables programmatic access to Google Drive activity data using the Google Drive Activity API within Apps Script.\u003c/p\u003e\n"],["\u003cp\u003eThis advanced service requires enabling before use and mirrors the functionality of the public API, using the same objects, methods, and parameters.\u003c/p\u003e\n"],["\u003cp\u003eComprehensive reference documentation, support resources, and sample code are available to guide developers in utilizing this service effectively.\u003c/p\u003e\n"],["\u003cp\u003eThe provided sample code demonstrates retrieving and logging recent Drive activity, including time, actor, action, and target information, using version 2 of the API.\u003c/p\u003e\n"]]],[],null,["# Google Drive Activity Service\n\nThe Google Drive Activity service allows you to use the\n[Google Drive Activity API](/drive/activity) in Apps Script. This API\nallows users programmatic access to retrieve information about their Google Drive activity.\n| **Note:** This is an advanced service that must be [enabled before use](/apps-script/guides/services/advanced).\n\nReference\n---------\n\nFor detailed information on this service, see the\n[reference documentation](/drive/activity/v2/reference/rest) for the Google\nDrive Activity API. Like all advanced services in Apps Script, the Google Drive Activity service uses the same objects, methods, and parameters as the\npublic API. For more information, see [How method signatures are determined](/apps-script/guides/services/advanced#how_method_signatures_are_determined).\n\nTo report issues and find other support, see the\n[Google Drive Activity support guide](/drive/activity/v2/support).\n\nSample code\n-----------\n\nThe sample code below uses [version 2](/drive/activity/v2) of\nthe API.\n\n### Get activity in Google Drive\n\nThis sample gets recent activity in a user's Google Drive and logs information about the time, actor, action, and target of each activity. \ndrive/activity-v2/quickstart.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/drive/activity-v2/quickstart.gs) \n\n```javascript\n/**\n * Lists 10 activity for a Drive user.\n * @see https://developers.google.com/drive/activity/v2/reference/rest/v2/activity/query\n */\nfunction listDriveActivity() {\n const request = {\n pageSize: 10\n // Use other parameter here if needed.\n };\n try {\n // Activity.query method is used Query past activity in Google Drive.\n const response = DriveActivity.Activity.query(request);\n const activities = response.activities;\n if (!activities || activities.length === 0) {\n console.log('No activity.');\n return;\n }\n console.log('Recent activity:');\n for (const activity of activities) {\n // get time information of activity.\n const time = getTimeInfo(activity);\n // get the action details/information\n const action = getActionInfo(activity.primaryActionDetail);\n // get the actor's details of activity\n const actors = activity.actors.map(getActorInfo);\n // get target information of activity.\n const targets = activity.targets.map(getTargetInfo);\n // print the time,actor,action and targets of drive activity.\n console.log('%s: %s, %s, %s', time, actors, action, targets);\n }\n } catch (err) {\n // TODO (developer) - Handle error from drive activity API\n console.log('Failed with an error %s', err.message);\n }\n}\n\n/**\n * @param {object} object\n * @return {string} Returns the name of a set property in an object, or else \"unknown\".\n */\nfunction getOneOf(object) {\n for (const key in object) {\n return key;\n }\n return 'unknown';\n}\n\n/**\n * @param {object} activity Activity object.\n * @return {string} Returns a time associated with an activity.\n */\nfunction getTimeInfo(activity) {\n if ('timestamp' in activity) {\n return activity.timestamp;\n }\n if ('timeRange' in activity) {\n return activity.timeRange.endTime;\n }\n return 'unknown';\n}\n\n/**\n * @param {object} actionDetail The primary action details of the activity.\n * @return {string} Returns the type of action.\n */\nfunction getActionInfo(actionDetail) {\n return getOneOf(actionDetail);\n}\n\n/**\n * @param {object} user The User object.\n * @return {string} Returns user information, or the type of user if not a known user.\n */\nfunction getUserInfo(user) {\n if ('knownUser' in user) {\n const knownUser = user.knownUser;\n const isMe = knownUser.isCurrentUser || false;\n return isMe ? 'people/me' : knownUser.personName;\n }\n return getOneOf(user);\n}\n\n/**\n * @param {object} actor The Actor object.\n * @return {string} Returns actor information, or the type of actor if not a user.\n */\nfunction getActorInfo(actor) {\n if ('user' in actor) {\n return getUserInfo(actor.user);\n }\n return getOneOf(actor);\n}\n\n/**\n * @param {object} target The Target object.\n * @return {string} Returns the type of a target and an associated title.\n */\nfunction getTargetInfo(target) {\n if ('driveItem' in target) {\n const title = target.driveItem.title || 'unknown';\n return 'driveItem:\"' + title + '\"';\n }\n if ('drive' in target) {\n const title = target.drive.title || 'unknown';\n return 'drive:\"' + title + '\"';\n }\n if ('fileComment' in target) {\n const parent = target.fileComment.parent || {};\n const title = parent.title || 'unknown';\n return 'fileComment:\"' + title + '\"';\n }\n return getOneOf(target) + ':unknown';\n}\n```"]]