/** * Lists 10 activity for a Drive user. * @see https://developers.google.com/drive/activity/v2/reference/rest/v2/activity/query */functionlistDriveActivity(){constrequest={pageSize:10// Use other parameter here if needed.};try{// Activity.query method is used Query past activity in Google Drive.constresponse=DriveActivity.Activity.query(request);constactivities=response.activities;if(!activities||activities.length===0){console.log('No activity.');return;}console.log('Recent activity:');for(constactivityofactivities){// get time information of activity.consttime=getTimeInfo(activity);// get the action details/informationconstaction=getActionInfo(activity.primaryActionDetail);// get the actor's details of activityconstactors=activity.actors.map(getActorInfo);// get target information of activity.consttargets=activity.targets.map(getTargetInfo);// print the time,actor,action and targets of drive activity.console.log('%s: %s, %s, %s',time,actors,action,targets);}}catch(err){// TODO (developer) - Handle error from drive activity APIconsole.log('Failed with an error %s',err.message);}}/** * @param {object} object * @return {string} Returns the name of a set property in an object, or else "unknown". */functiongetOneOf(object){for(constkeyinobject){returnkey;}return'unknown';}/** * @param {object} activity Activity object. * @return {string} Returns a time associated with an activity. */functiongetTimeInfo(activity){if('timestamp'inactivity){returnactivity.timestamp;}if('timeRange'inactivity){returnactivity.timeRange.endTime;}return'unknown';}/** * @param {object} actionDetail The primary action details of the activity. * @return {string} Returns the type of action. */functiongetActionInfo(actionDetail){returngetOneOf(actionDetail);}/** * @param {object} user The User object. * @return {string} Returns user information, or the type of user if not a known user. */functiongetUserInfo(user){if('knownUser'inuser){constknownUser=user.knownUser;constisMe=knownUser.isCurrentUser||false;returnisMe?'people/me':knownUser.personName;}returngetOneOf(user);}/** * @param {object} actor The Actor object. * @return {string} Returns actor information, or the type of actor if not a user. */functiongetActorInfo(actor){if('user'inactor){returngetUserInfo(actor.user);}returngetOneOf(actor);}/** * @param {object} target The Target object. * @return {string} Returns the type of a target and an associated title. */functiongetTargetInfo(target){if('driveItem'intarget){consttitle=target.driveItem.title||'unknown';return'driveItem:"'+title+'"';}if('drive'intarget){consttitle=target.drive.title||'unknown';return'drive:"'+title+'"';}if('fileComment'intarget){constparent=target.fileComment.parent||{};consttitle=parent.title||'unknown';return'fileComment:"'+title+'"';}returngetOneOf(target)+':unknown';}
[null,null,["最后更新时间 (UTC):2025-08-28。"],[],[],null,["Create a\n[Google Apps Script](/apps-script/overview)\nthat makes requests to the Google Drive Activity API.\n\nQuickstarts explain how to set up and run an app that calls a\nGoogle Workspace API. This quickstart uses a\nsimplified authentication approach that is appropriate for a testing\nenvironment. For a production environment, we recommend learning about\n[authentication and authorization](/workspace/guides/auth-overview)\nbefore\n[choosing the access credentials](/workspace/guides/create-credentials#choose_the_access_credential_that_is_right_for_you)\nthat are appropriate for your app.\n\nIn Apps Script, Google Workspace\nquickstarts use\n[Advanced Google services](/apps-script/guides/services/advanced) to call\nGoogle Workspace APIs and handle some details of the authentication\nand authorization flow.\n\nObjectives\n\n- Configure the environment.\n- Create and configure the script.\n- Run the script.\n\nPrerequisites\n\n- A Google Account\n\n\u003c!-- --\u003e\n\n- Access to Google Drive\n\nCreate the script\n\n1. Create a new script by going to [script.google.com/create](https://script.google.com/create).\n2. Replace the contents of the script editor with the following code:\n\n\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```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n3. Click Save .\n4. Click **Untitled project** , type **Quickstart** , and click **Rename**.\n\n\u003cbr /\u003e\n\nConfigure the script\n\nEnable the Google Drive Activity API\n\nOpen the Apps Script project.\n\n1. Click **Editor** code.\n2. Next to **Services** , click Add a service add .\n3. Select Drive Activity API and click **Add**.\n\nRun the sample\n\nIn the Apps Script editor, click **Run**.\n\nThe first time you run the sample, it prompts you to authorize access:\n\n1. Click **Review permissions**.\n2. Choose an account.\n3. Click **Allow**.\n\nThe script's execution log appears at the bottom of the window. \ndone It worked! **Great!** Check out the further reading section below to learn more.\nwarning There was a problem **Bummer** , [let us know what went wrong](#). Check out our [troubleshooting](#troubleshooting) section below for some common errors and solutions. If you have found a bug in the code, [report the issue on GitHub](https://github.com/googleworkspace/apps-script-samples/issues) or submit a pull request.\n\nNext steps\n\n- [Google Apps Script Advanced Services documentation](/apps-script/guides/services/advanced)\n- [Try the Google Workspace APIs in the APIs explorer](/workspace/explore)\n- [Troubleshoot authentication and authorization issues](/workspace/drive/activity/v2/troubleshoot-authentication-authorization)\n- [Drive Activity API reference documentation](/workspace/drive/activity/v2/reference/rest)"]]