Serviço de atividades do Google Drive
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
O serviço de atividade do Google Drive permite usar a
API Google Drive Activity no Apps Script. Essa API
permite que os usuários acessem informações sobre a atividade do Google Drive de forma programática.
Referência
Para informações detalhadas sobre esse serviço, consulte a
documentação de referência da API Google
Drive Activity. Assim como todos os serviços avançados no Apps Script, o serviço de atividade do Google Drive usa os mesmos objetos, métodos e parâmetros da
API pública. Para mais informações, consulte Como as assinaturas de método são determinadas.
Para reportar problemas e encontrar outras opções de suporte, consulte o
guia de suporte da Atividade no Google Drive.
Código de amostra
O exemplo de código abaixo usa a versão 2 da API.
Receber atividade no Google Drive
Essa amostra recebe a atividade recente no Google Drive de um usuário e registra informações sobre o horário, o ator, a ação e o destino de cada atividade.
Exceto em caso de indicação contrária, o conteúdo desta página é licenciado de acordo com a Licença de atribuição 4.0 do Creative Commons, e as amostras de código são licenciadas de acordo com a Licença Apache 2.0. Para mais detalhes, consulte as políticas do site do Google Developers. Java é uma marca registrada da Oracle e/ou afiliadas.
Última atualização 2025-08-31 UTC.
[null,null,["Última atualização 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```"]]