The following code is pulling data to Google sheets from Google workspace using Reports API. However it is giving me only last 2 days of data not sure why, there are no limits set in the script.
The code is the reference of the following question:
function listUsers() {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var values = [];
var userKey = 'all';
var applicationName = 'admin';
var optionalArgs = {
maxResults: 100
};
var response = AdminReports.Activities.list(userKey, applicationName, optionalArgs);
var activities = response.items;
if (activities && activities.length > 0) {
Logger.log('REPORTS:');
for (i = 0; i < activities.length; i++) {
var activity = activities[i];
//ONLY GET DATA IF ACTION IS EITHER "SUSPEND_USER", "DELETE_USER", or "ARCHIVE_USER"
if(activity.events[0].name == "SUSPEND_USER" || activity.events[0].name == "DELETE_USER" || activity.events[0].name == "ARCHIVE_USER"){
Logger.log('%s: %s (%s)', activity.id.time, activity.events[0].parameters[0].value,
activity.events[0].name);
//RETRIEVES THE TIMESTAMP, USER'S EMAIL, & THE EVENT NAME THAT WAS PERFORMED TO THE USER
values = [[activity.id.time, activity.events[0].parameters[0].value,activity.events[0].name]];
//SET THE DATA TO SHEET
var lrow = sheet.getLastRow()+1;
sheet.getRange("A"+lrow+":C"+lrow).setValues(values);
}
}
} else {
Logger.log('No reports found.');
}
}
var optionalArgs = {
maxResults: 100
};
Set this value to 1000
instead of 100
or remove it - the default value for maxResults
is 1000
- as per documentation
Should you want to retrieve more than 1000 results, you will need to retrieve nextPageToken
from the response and pass it as pageToken
to the optionalArgs
for the next request.
You can reapet the request in a loop until you obtain all results.
Code modification:
function listUsers() {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var userKey = 'all';
var applicationName = 'admin';
var optionalArgs = {
maxResults: 1000
};
var response = AdminReports.Activities.list(userKey, applicationName, optionalArgs);
var activities = response.items;
if (activities && activities.length > 0) {
Logger.log('REPORTS:');
for (i = 0; i < activities.length; i++) {
var activity = activities[i];
//ONLY GET DATA IF ACTION IS EITHER "SUSPEND_USER", "DELETE_USER", or "ARCHIVE_USER"
if (activity.events[0].name == "SUSPEND_USER" || activity.events[0].name == "DELETE_USER" || activity.events[0].name == "ARCHIVE_USER") {
Logger.log('%s: %s (%s)', activity.id.time, activity.events[0].parameters[0].value,
activity.events[0].name);
//RETRIEVES THE TIMESTAMP, USER'S EMAIL, & THE EVENT NAME THAT WAS PERFORMED TO THE USER
values = [
[activity.id.time, activity.events[0].parameters[0].value, activity.events[0].name]
];
//SET THE DATA TO SHEET
var lrow = sheet.getLastRow() + 1;
sheet.getRange("A" + lrow + ":C" + lrow).setValues(values);
}
}
} else {
Logger.log('No reports found.');
}
//////// ADD THE FOLLOWING TO YOUR EXISTING CODE:
while (response.nextPageToken) {
var optionalArgs = {
maxResults: 1000,
pageToken: response.nextPageToken
}
var response = AdminReports.Activities.list(userKey, applicationName, optionalArgs);
var activities = response.items;
if (activities && activities.length > 0) {
Logger.log('REPORTS:');
for (i = 0; i < activities.length; i++) {
var activity = activities[i];
//ONLY GET DATA IF ACTION IS EITHER "SUSPEND_USER", "DELETE_USER", or "ARCHIVE_USER"
if (activity.events[0].name == "SUSPEND_USER" || activity.events[0].name == "DELETE_USER" || activity.events[0].name == "ARCHIVE_USER") {
Logger.log('%s: %s (%s)', activity.id.time, activity.events[0].parameters[0].value,
activity.events[0].name);
//RETRIEVES THE TIMESTAMP, USER'S EMAIL, & THE EVENT NAME THAT WAS PERFORMED TO THE USER
values = [
[activity.id.time, activity.events[0].parameters[0].value, activity.events[0].name]
];
//SET THE DATA TO SHEET
var lrow = sheet.getLastRow() + 1;
sheet.getRange("A" + lrow + ":C" + lrow).setValues(values);
}
}
} else {
Logger.log('No reports found.');
}
}
}