简体   繁体   English

如何使用Apps脚本将对Google Analytics API的多次调用打印到Google Sheets

[英]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.

相关问题 Google Apps Script - 如何将此代码应用于 Google 表格中的多个表格 - Google Apps Script - how to applies this code for multiple sheets in google sheets 如何将 Google Apps 脚本应用于多个工作表 - How to apply Google Apps Script to multiple sheets 如何通过 Apps Script 更改 Google Sheets 中的多个单元格值? - How to change multiple cell values in Google Sheets via Apps Script? getSheets() Google Apps Script 多次调用问题 - getSheets() Google Apps Script multiple calls issue Google Apps脚本多个在Google表格中查找和替换正则表达式 - Google Apps Script Multiple Find and replace regex in Google Sheets 用于在 Google 表格中进行多次查找和替换的 Google Apps 脚本 - Google Apps Script for Multiple Find and Replace in Google Sheets 获取Google Analytics(分析)访问令牌以及如何在Google表格脚本中使用它 - getting a google analytics access token and how to use it in google sheets script 如何通过 Google 表格中的 Google Apps 脚本找到多个 IP 地址的地理位置? - How can I find geolocation of MULTIPLE IP addresses via Google Apps Script in Google Sheets? Google Sheets API和Chrome应用 - Google Sheets API and Chrome Apps Google Analytics Data API - HTML 到 Apps 脚本:未定义 gapi - Google Analytics Data API - HTML to Apps Script: gapi is not defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM