
[英]Google Apps Script - how to applies this code for multiple sheets in google sheets
[英]How to print multiple calls to Google Analytics API to Google Sheets with Apps Script
我正在嘗試將多個調用的多個結果打印到同一Google表格中。 我正在使用Google Apps腳本調用Google Analytics(分析)API。
我目前能夠打印兩組數據,但是第二組數據會強制執行我的第一個電話。 這幾乎就像第二個調用的結果被第一個調用所覆蓋。 我敢肯定,在嘗試打印兩組結果時,我的問題都在底部。
這是我的代碼:
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());
}
任何幫助,將不勝感激。
您的JavaScript邏輯不正確。 您得到第一個結果,然后得到: if (x) return y; else throw exception
if (x) return y; else throw exception
所以它永遠不會超越。 使用調試器並逐步執行每一行。 您將看到無法進入第二查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.