【问题标题】:Data manipulation with google apps script使用谷歌应用脚​​本进行数据操作
【发布时间】:2019-08-27 21:44:33
【问题描述】:

我正在尝试从谷歌分析中提取数据,对数据进行一些数据操作并将格式化的数据粘贴到谷歌表格中。我知道如何提取数据,并且知道如何将其粘贴到谷歌表格中 - 我不确定如何进行我需要做的操作 (如果我使用 Python,我会使用 Pandas 库来做需要做什么,但我迷失了 javascript/google 应用程序脚本)

我一直在谷歌上搜索如何在谷歌应用脚​​本中操作数据,但找不到任何有用的东西。

这是我目前的代码,它可以正确地提取数据并将其粘贴到谷歌表格中:

function updateReport() {

  var profileId = XXXXXX;
  var tableId = 'ga:' + profileId;

  var startDate = 'yesterday';
  var endDate = 'yesterday';  

  var metrics = 'ga:sessions, ga:transactions, ga:transactionRevenue';
  var optionalArgs = {  
    'dimensions': 'ga:date, ga:source, ga:medium, ga:campaign',
  };

  var report = Analytics.Data.Ga.get(tableId, startDate, endDate, metrics, optionalArgs);


  if (report.rows) {

    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = spreadsheet.getSheetByName('Data'); 
    var firstEmptyRow = sheet.getLastRow() + 1;

    sheet.getRange(firstEmptyRow,1, report.rows.length, report.rows[0].length).setValues(report.rows);
  }

}

我分享的脚本会给出这样的结果:

Date     Source Medium Campaign Sessions Transactions Revenue
20190826 Facebook cpc   Brand     100       10         1,000
20190826 Facebook cpc   Generic   110       20         2,000
20190826 Google   cpc   Brand     120       30         3,000
20190826 Google   cpc   Generic   130       40         4,000
20190826 Google   cpc   Brand     140       50         5,000
20190826 Google   cpc   Generic   150       60         6,000

这是我试图得到的结果:

Date            Channel       Sessions Transactions Revenue
20190826    Facebook - Brand    100       10        1,000
20190826    Facebook - Generic  110       20        2,000
20190826    Google - Brand      260       80        8,000
20190826    Google - Generic    280      100       10,000

使用伪代码,它可能看起来像这样:

if Source == 'Facebook' and Medium == 'cpc' and Campaign == 'Brand':
    return 'Facebook - Brand'

elif Source == 'Facebook' and Medium == 'cpc' and Campaign == 'Generic':
    return 'Facebook - Generic'

elif Source == 'Google' and Medium == 'cpc' and Campaign == 'Brand':
    return 'Google - Brand'

else Source == 'Google' and Medium == 'cpc' and Campaign == 'Generic':
    return 'Google - Generic'

如果我能在这里得到任何帮助,我将不胜感激!

【问题讨论】:

  • 如果 Medium 不是“cpc”或 Source 不是“Facebook”也不是“Google”,你希望发生什么?不应该将相应的报告行插入到电子表格中吗?
  • 我们可以把它们放在一个叫做“other”的桶里

标签: google-apps-script google-sheets


【解决方案1】:

要进行这种数据操作,您需要先使用 JavaScript 循环和条件语句继续 report.rows,然后再将值设置到电子表格中。

示例:

  if (report.rows) {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName('Data'); 
  var bucket=[];
  //starting with the second row, since your first row are headers
  for(var i=1; i<report.rows.length;i++){
    var newRow=[];
    if((report.rows[i][1]=='Facebook'||report.rows[i][1]=='Google')&&report.rows[i][2]=='cpc'&&(report.rows[i][3]=='Brand'||report.rows[i][3]=='Generic')){
      newRow.push(report.rows[i][0]);
      newRow.push(report.rows[i][1]+' - '+report.rows[i][3]);
      for(var j=4; j<report.rows[0].length;j++){
        newRow.push(report.rows[i][j]);
      }
      var firstEmptyRow = sheet.getLastRow() + 1;      
      sheet.getRange(firstEmptyRow,1, 1, newRow.length).setValues([newRow]);
     }
    else{
      bucket.push(report.rows[i]);
     }
   }
}

【讨论】:

  • 感谢您的帮助,帮助我弄清楚到底需要什么!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多