简体   繁体   English

在Google Apps脚本中获取总值

[英]Get total values in google apps script

I'm trying to use "totalsForAllResults" method in google apps script to push out total values for each metrics. 我正在尝试在Google Apps脚本中使用“ totalsForAllResults”方法来推出每个指标的总价值。 I printed out result of each metrics by each dimension to a spreadsheet. 我将每个维度的每个指标的结果打印到电子表格中。 And I'd like to see the total values at the end of the row. 我想在行末查看总值。 I tried many times but cat't get it work. 我尝试了很多次,但没有成功。 I'll be happy if anybody give me advice. 如果有人给我建议,我会很高兴。

The problem is the last paragraph. 问题是最后一段。 This code worked for get analytics data but "totalsForAllResults" doesn't work. 该代码适用于获取分析数据,但“ totalsForAllResults”无效。

var results = ChannelReportPC(firstProfile); 
   outputToSpreadsheet(results);

function ChannelReportPC(firstProfile) { 

  var profileId = firstProfile.getId();
  var tableId = 'ga:' + profileId;
  var startDate = electiveStartDate('SD');  
  var endDate = electiveEndDate('ED');     

  var optArgs = { 
    'dimensions': 'ga:medium',              // Comma separated list of dimensions.
    'sort': '-ga:sessions',      
    'segment': 'sessions::condition::ga:deviceCategory==desktop,ga:deviceCategory==tablet',  // Process desktop or tablet traffic.
    'start-index': '1',
    'max-results': '20'
  };

  // 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:sessions,ga:percentNewSessions,ga:bounceRate,ga:pageviewsPerSession,ga:avgSessionDuration,ga:transactions,ga:transactionRevenue,ga:transactionsPerSession', // Comma seperated list of metrics.
     optArgs
     );

  var sheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet("media");

}

function outputToSpreadsheet(results) {

  sheet = SpreadsheetApp.getActiveSheet()


  // Print the headers.
  var headerNames = [];
  for (var i = 0, header; header = results.getColumnHeaders()[i]; ++i) {
    headerNames.push(header.getName());
  }
  sheet.getRange(1, 2, 1, headerNames.length)
      .setValues([headerNames])
      .setBackground('#eeeeee');


  // Print the rows of data.
  sheet.getRange(2, 2, results.getRows().length, headerNames.length)
      .setValues(results.getRows());    


  sheet.getRange(getRows().length+1, 2, 1, headerNames.length)
  .setValues(results.totalsForAllResults);
}

I believe your results.totalForAllresults contains array of values. 我相信您的results.totalForAllresults包含值数组。 If it is, try to convert it into array of array. 如果是这样,请尝试将其转换为array数组。 Try to follow these steps for your code, 尝试按照以下步骤操作代码,

  data = [];
  data.push(results.totalsForAllResults);
  sheet.getRange(getRows().length+1, 2, 1, headerNames.length).setValues(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM