1

我正在使用 Google Apps 脚本来获取有关我的 Google 域上的活动的一些信息,并且它可以与 Google 在他的 QuickStart 项目中提供的脚本一起正常工作,但我无法获取某些参数的值,例如 'docs:num_docs'、'accounts :apps_total_licenses' 或 'accounts:apps_used_licenses'。我总是得到一个“未定义”的回应。这是脚本,请您告诉我哪里出了问题。谢谢。

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

  var parameters = [
    'accounts:num_30day_logins',
    'accounts:num_7day_logins',
    'accounts:num_1day_logins',
    'accounts:num_disabled_accounts', 
    'accounts:apps_total_licenses',
    'accounts:apps_used_licenses',
    'accounts:num_users_2sv_enrolled',
    'gmail:num_emails_received',
    'gmail:num_inbound_spam_emails',
    'gmail:num_inbound_non_spam_emails',
    'gmail:num_inbound_delivered_emails',
    'gmail:num_emails_sent',
    'gmail:num_inbound_rejected_emails',
    'gmail:num_inbound_rerouted_emails',
    'gplus:num_video_calls',
    'gplus:num_video_conferences',
    'gplus:num_video_conferences_cfm',
    'gplus:total_video_call_minutes',
    'docs:num_docs'
  ];
  var rows = [];
  var pageToken, page;
  do {
    page = AdminReports.CustomerUsageReports.get(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,
          parameterValues['accounts:num_30day_logins'],
          parameterValues['accounts:num_7day_logins'],
          parameterValues['accounts:num_1day_logins'],
          parameterValues['accounts:num_disabled_accounts'],
          parameterValues['accounts:apps_total_licenses'],
          parameterValues['accounts:apps_used_licenses'],
          parameterValues['accounts:num_users_2sv_enrolled'],
          parameterValues['gmail:num_emails_received'],
          parameterValues['gmail:num_inbound_spam_emails'],
          parameterValues['gmail:num_inbound_non_spam_emails'],
          parameterValues['gmail:num_inbound_delivered_emails'],
          parameterValues['gmail:num_emails_sent'],
          parameterValues['gmail:num_inbound_rejected_emails'],
          parameterValues['gmail:num_inbound_rerouted_emails'],
          parameterValues['gplus:num_video_calls'],
          parameterValues['gplus:num_video_conferences'],
          parameterValues['gplus:num_video_conferences_cfm'],
          parameterValues['gplus:total_video_call_minutes'],
          parameterValues['docs:num_docs']
        ];
        rows.push(row);
      }
    }
    pageToken = page.nextPageToken;
  } while (pageToken);

  if (rows.length > 0) {
    //var spreadsheet = SpreadsheetApp.create('Xavier - Google Apps Customer Usage Report');
    var spreadsheet = SpreadsheetApp.openById('1T_eAl0DSLiG2H3l-LkbdFSoyF9gvB4hUfPWLKFJsGb0');
    var sheet = spreadsheet.getSheetByName('data');

    // Append the headers.
    var headers = ['Date', 
                   'num_30day_logins', 
                   'num_7day_logins', 
                   'num_1day_logins', 
                   'num_disabled_accounts', 
                   'apps_total_licenses', 
                   'apps_used_licenses', 
                   'num_users_2sv_enrolled', 
                   'num_emails_received', 
                   'num_inbound_spam_emails', 
                   'num_inbound_non_spam_emails', 
                   'num_inbound_delivered_emails',
                   'num_emails_sent',
                   'num_inbound_rejected_emails',
                   'num_inbound_rerouted_emails',
                   'num_video_calls',
                   'num_video_conferences',
                   'num_video_conferences_cfm',
                   'total_video_call_minutes',
                   'num_docs'
                  ];
    //sheet.appendRow(headers);

    // Append the results.
    sheet.getRange(sheet.getLastRow() + 1, 1, rows.length, parameters.length + 1).setValues(rows);


    Logger.log('Report spreadsheet created: %s', spreadsheet.getUrl());
  } else {
    Logger.log('No results returned.');
  }
}

/**
 * Gets a map of parameter names to values from an array of parameter objects.
 * @param {Array} parameters An array of parameter objects.
 * @return {Object} A map from parameter names to their values.
 */
function getParameterValues(parameters) {
  return parameters.reduce(function(result, parameter) {
    var name = parameter.name;
    var value;
    if (parameter.intValue !== undefined) {
      value = parameter.intValue;
    } else if (parameter.stringValue !== undefined) {
      value = parameter.stringValue;
    } else if (parameter.datetimeValue !== undefined) {
      value = new Date(parameter.datetimeValue);
    } else if (parameter.boolValue !== undefined) {
      value = parameter.boolValue;
    }
    result[name] = value;
    return result;
  }, {});
}
4

1 回答 1

1

我想我找到了问题所在。似乎这些数据并非每天都可用,因为我的脚本适用于 2017-01-03 和 2017-01-05,但不适用于 2017-01-02、2017-01-04 和 2017-01-06。

于 2017-01-18T14:13:36.467 回答