0

我正在尝试从我的域中针对所有用户生成使用情况报告。我希望能够使用这些参数:num_1day_logins、num_7day_logins、num_30day_logins。这样我就可以展示我们领域中 Google Apps 的采用是如何增加或减少的。下面的代码来自谷歌。这与我想做的很接近,但不完全是。我认为可以将此代码自定义为我想做的事情,但我不知道如何。

谢谢。

function generateUserUsageReport() {
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, 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);

if (rows.length > 0) {
var spreadsheet = SpreadsheetApp.create('Google Apps User Usage Report');
var sheet = spreadsheet.getActiveSheet();

// Append the headers.
var headers = ['Date', 'User', 'Last Login', 'Num Emails Received',
    'Num Docs'];
sheet.appendRow(headers);

// Append the results.
sheet.getRange(2, 1, rows.length, headers.length).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

这是带有您正在寻找的参数的代码。

function generateUserUsageReport() {
var today = new Date();
var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
var timezone = Session.getScriptTimeZone();
var date = Utilities.formatDate(oneWeekAgo, timezone, 'yyyy-MM-dd');
var parameters = [
'accounts:num_1day_logins',
'accounts:num_7day_logins',
'accounts:num_30day_logins'
];
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_1day_logins'],
      parameterValues['accounts:num_7day_logins'],
      parameterValues['accounts:num_30day_logins']
    ];
    rows.push(row);
  }
}
pageToken = page.nextPageToken;
} while (pageToken);

if (rows.length > 0) {
var spreadsheet = SpreadsheetApp.create('Google Apps User Usage Report');
var sheet = spreadsheet.getActiveSheet();

// Append the headers.
var headers = ['Date', '1day_logins', '7day_logins',
    '30day_logins'];
sheet.appendRow(headers);

// Append the results.
sheet.getRange(2, 1, rows.length, headers.length).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;
}, {});
}
于 2015-04-27T19:04:39.803 回答