[英]How to print multiple calls to Google Analytics API to Google Sheets with Apps Script
I'm trying to print multiple results from multiple calls into the same Google sheet. 我正在尝试将多个调用的多个结果打印到同一Google表格中。 I'm using Google Apps Script to call the Google Analytics API.
我正在使用Google Apps脚本调用Google Analytics(分析)API。
I'm currently able to print two sets of data, but the second set coerces to my first call. 我目前能够打印两组数据,但是第二组数据会强制执行我的第一个电话。 It's almost like the results of the second call are overridden by the first call.
这几乎就像第二个调用的结果被第一个调用所覆盖。 I'm sure my problem is towards the bottom in my attempt to print both sets of results.
我敢肯定,在尝试打印两组结果时,我的问题都在底部。
Here is my code: 这是我的代码:
function getReportDataForProfile(firstProfile) {
var profileId = firstProfile;
var tableId = 'ga:' + profileId;
var startDate = getMonthsAgo(3); // Months ago.
var endDate = getEndDate (0); // Today.
var optArgs = {
'dimensions': 'ga:month', // Comma separated list of dimensions.
'segment':'gaid::-lPvLp6LTbyDv3jOuYgEQA', // Female 18-24
'start-index': '1',
'max-results': '250' // Display the first 250 results.
};
var optArgs1 = {
'dimensions': 'ga:month', // Comma separated list of dimensions.
'segment':'gaid::Uu6nRXdoQxipnELhe94ejg', //Female 25 - 30
'start-index': '1',
'max-results': '250' // Display the first 250 results.
};
// Make a request to the API.
var results = Analytics.Data.Ga.get(
tableId, // Table id (format ga:xxxxxx).
startDate, // Start-date (format yyyy-MM-dd).
endDate, // End-date (format yyyy-MM-dd).
'ga:goal19Completions', // Comma seperated list of metrics.
optArgs);
if (results.getRows()) {
return results;
} else {
throw new Error('No views (profiles) found');
};
var results1 = Analytics.Data.Ga.get(
tableId, // Table id (format ga:xxxxxx).
startDate, // Start-date (format yyyy-MM-dd).
endDate, // End-date (format yyyy-MM-dd).
'ga:goal19Completions', // Comma seperated list of metrics.
optArgs1);
if (results1.getRows()) {
return results1;
} else {
throw new Error('No views (profiles) found');
};
}
function outputToSpreadsheet(results) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Print the headers.
var headerNames = [];
for (var i = 0, header; header = results.getColumnHeaders()[i]; ++i) {
headerNames.push(header.getName());
}
sheet.getRange(1, 1, 1, headerNames.length)
.setValues([headerNames]);
// Print the rows of data.
sheet.getRange(2, 1, results.getRows().length, headerNames.length)
.setValues(results.getRows());
}
function outputToSpreadsheet(results1) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
// Print the headers.
var headerNames = [];
for (var i = 0, header; header = results1.getColumnHeaders()[i]; ++i) {
headerNames.push(header.getName());
}
sheet.getRange(1, 3, 1, headerNames.length)
.setValues([headerNames]);
// Print the rows of data.
sheet.getRange(2, 3, results1.getRows().length, headerNames.length)
.setValues(results1.getRows());
}
Any help would be appreciated. 任何帮助,将不胜感激。
your javascript logic is not right. 您的JavaScript逻辑不正确。 you get your first result and then have:
if (x) return y; else throw exception
您得到第一个结果,然后得到:
if (x) return y; else throw exception
if (x) return y; else throw exception
so it will never get past that. 所以它永远不会超越。 use the debugger and step each line.
使用调试器并逐步执行每一行。 you will see its impossible to get to the 2nd query.
您将看到无法进入第二查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.