Google 雲端硬碟活動服務
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Google Drive Activity 服務可讓您在 Apps Script 中使用 Google Drive Activity API。使用者可透過這個 API,以程式輔助方式存取及擷取 Google 雲端硬碟活動的相關資訊。
參考資料
如要進一步瞭解這項服務,請參閱 Google 雲端硬碟活動記錄 API 的參考說明文件。與 Apps Script 中的所有進階服務一樣,Google Drive Activity 服務使用的物件、方法和參數,都與公開 API 相同。詳情請參閱「如何判斷方法簽章」。
如要回報問題及尋求其他支援,請參閱 Google 雲端硬碟活動支援指南。
程式碼範例
下列程式碼範例使用 API 的第 2 版。
查看 Google 雲端硬碟活動
這個範例會取得使用者 Google 雲端硬碟的近期活動,並記錄每項活動的時間、參與者、動作和目標等資訊。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\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```"]]