Google Apps脚本报告返回空值的API示例代码

时间:2019-02-22 19:32:08

标签: google-apps-script google-apps gsuite

我正在尝试使用this snippet of App Script code来从Reports API中打印出一些值。我做了些微调整,就像这样:

function testDocsOwned() {
  var today = new Date();
  var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
  var timezone = Session.getTimeZone();
  var date = Utilities.formatDate(oneWeekAgo, timezone, 'yyyy-MM-dd');

  var parameters = [
    'accounts:last_login_time',
    'gmail:num_emails_received',
    'docs:num_docs'
  ];
  var rows = [];
  var pageToken;
  var page;
  do {
    page = AdminReports.UserUsageReport.get('all', date, {
      parameters: parameters.join(','),
      maxResults: 500,
      pageToken: pageToken
    });
    var reports = page.usageReports;
    if (reports) {
      for (var i = 0; i < reports.length; i++) {
        var report = reports[i];
        var parameterValues = getParameterValues(report.parameters);
        var row = [
          report.date,
          report.entity.userEmail,
          parameterValues['accounts:last_login_time'],
          parameterValues['gmail:num_emails_received'],
          parameterValues['docs:num_docs']
        ];
        rows.push(row);
      }
    }
    pageToken = page.nextPageToken;
  } while (pageToken);
    console.log(rows);
}

此代码运行时,docs_owned属性返回null,而num_emails_recieved为0;并且我已验证这些帐户具有邮件和驱动器文档。在过去六个月中创建了文档并收到了电子邮件。

1 个答案:

答案 0 :(得分:0)

这是我的工作代码(相似但不完全相同):

function runLoginActivityReport(pageToken) {
  var today = new Date();
  var oneWeekAgo = new Date(today.getTime() -30 * 24 * 60 * 60 * 1000);
  var timezone = Session.getScriptTimeZone();
  var date = Utilities.formatDate(oneWeekAgo, timezone, 'yyyy-MM-dd');

  var parameters = [
    'accounts:last_login_time',
    'drive:num_items_created'
  ];
  var rows = [];
  var recordCollection = [];
  var page;

  page = AdminReports.UserUsageReport.get('all', date, {
    parameters: parameters.join(','),
    maxResults: 500,
    pageToken: pageToken
  });
  var reports = page.usageReports;

  if (page.warnings) {
    for (var q = 0; q < page.warnings.length; q++) {
      var warning = page.warnings[q];
      Logger.log(warning.message);
    }
  }
// there was some logic here
  return page.nextPageToken;
}