使用 Reports API 从 Google 工作区提取数据的 AppScript 有问题

Having issues with the Appscript pulling data from Google workspace using Reports API

以下代码使用报表 API 从 Google 工作区将数据提取到 Google 工作表。但是它只给我最近 2 天的数据,不知道为什么,脚本中没有设置限制。

代码为以下问题的参考:

How to pull deleted, archived, suspended users data to Google sheets from Admin SDK >> Reports API using Appscript

函数 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
};

将此值设置为 1000 而不是 100 或将其删除 - maxResults 的默认值为 1000 - 根据 documentation

如果您想检索超过 1000 个结果,您需要从响应中检索 nextPageToken 并将其作为 pageToken 传递给下一个请求的 optionalArgs

您可以循环重复请求,直到获得所有结果。

代码修改:

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.');
        }
    }
}