我对谷歌应用脚本很烂,需要将 AdSense 报告导入谷歌电子表格。我尝试了用于高级服务的 Google Apps 脚本参考指南中提供的示例,但我无法让它工作。
code.gs 的示例如下所示:
function generateReport(adClientId) {
var today = new Date();
var oneWeekAgo = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
var timezone = Session.getTimeZone();
var startDate = Utilities.formatDate(oneWeekAgo, timezone, 'yyyy-MM-dd');
var endDate = Utilities.formatDate(today, timezone, 'yyyy-MM-dd');
var report = AdSense.Reports.generate(startDate, endDate, {
filter: ['AD_CLIENT_ID==' + escapeFilterParameter(adClientId)],
metric: ['PAGE_VIEWS', 'AD_REQUESTS', 'AD_REQUESTS_COVERAGE', 'CLICKS',
'AD_REQUESTS_CTR', 'COST_PER_CLICK', 'AD_REQUESTS_RPM',
'EARNINGS'],
dimension: ['DATE'],
sort: ['+DATE'],
});
if (report.rows) {
var spreadsheet = SpreadsheetApp.create('AdSense Report');
var sheet = spreadsheet.getActiveSheet();
var headers = report.headers.map(function(header) {
return header.name;
});
sheet.appendRow(headers);
sheet.getRange(2, 1, report.rows.length, headers.length)
.setValues(report.rows);
Logger.log('Report spreadsheet created: %s',
spreadsheet.getUrl());
Logger.log('No rows returned.');
我如何让它调用报告?