通过google-app-script创建DCM报告

时间:2017-01-24 09:04:28

标签: google-apps-script

我是Google App Scripts的新手。我需要使用Google-app-script功能在Spreadsheet中构建报告。我知道如何从DCM / DFM报告摘要报告到电子表格。

但是,如果没有从DCM / DFM下载现有报告,我就无法创建新的内部电子表格。可能吗?可能有人有样品(我还没找到)?或者我错过了这个概念,将数据提取到电子表格的唯一方法是先在DCM / DCF报表生成器中创建报表?

以下是我使用的代码。



function generateReport() {                                              
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Reports');
  var profileId = 2623334
  var httpOptions = {
        'headers': {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()}
    }

  var resource = {
  
    'kind': 'dfareporting#report',
    'accountId': '34405',
    'type': 'STANDARD',
    'name': 'Simple Report',
    'criteria': {
    'dateRange': {
      'kind': 'dfareporting#dateRange',
      'startDate': '2016-09-01',
      'endDate': '2017-01-22',
    },
    'dimensions': [
      {
        'kind': 'dfareporting#sortedDimension',
        'name': 'dfa:date',
      }
    ],
   'metricNames': [
      'dfa:clicks', 'dfa:impressions'
    ],
  }
  }
  
  var url = DoubleClickCampaigns.Reports.insert(resource, profileId);
  var report =  UrlFetchApp.fetch(url.urls.apiUrl, httpOptions);  
  for (var i=0; i<report.length; i++) {
    var row = report[i];
    sheet.getRange('A' + String(i+2)).setValue(row[0]);
    sheet.getRange('B' + String(i+2)).setValue(row[1]);
    sheet.getRange('C' + String(i+2)).setValue(row[2]);
  }
}
&#13;
&#13;
&#13;

此处出现错误:

var report =  UrlFetchApp.fetch(url.urls.apiUrl, httpOptions); 

错误消息:

  

无法阅读财产&#34; apiUrl&#34;未定义的对象。

1 个答案:

答案 0 :(得分:0)

我的最终剧本是:

&#13;
&#13;
function generateReport() {                                                 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Reports');
  var startDate = Browser.inputBox("Enter a start date (format: 'yyyy-mm-dd')");
  var endDate = Browser.inputBox("Enter an end date (format: 'yyyy-mm-dd')");
  var ReportName = Browser.inputBox("Enter your future report name");
  var profileId = 2623334; 
  var resource = {
    'kind': 'dfareporting#report',
    'accountId': '34405',
    'type': 'STANDARD',
    'name': ReportName,
    'criteria': {
       'dateRange': {
           'kind': 'dfareporting#dateRange',
           'startDate': startDate,
           'endDate': endDate,
                   },
       'dimensions': [
                       {
                         'kind': 'fareporting#sortedDimension',
                         'name': 'dfa:date'
                       }
                    ],
        'metricNames': [
      'dfa:clicks', 'dfa:impressions'
                       ],
                },
              };
  
  var httpOptions = {
        'headers': {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()}
    }
  var url = DoubleClickCampaigns.Reports.insert(resource, profileId);                   //create Report inside DCM
  var newReportId = Number(url.id);  //get id of new Report  
  var additionalParameters = {
    'synchronous': 'true'
  };
  var newReportRun = DoubleClickCampaigns.Reports.run(profileId, newReportId, additionalParameters);          // run new Report in DCM
  var newReportFileId = Number(newReportRun.id);  //get id of New File
  var newReportFile = DoubleClickCampaigns.Files.get(newReportId, newReportFileId);     
    if(newReportFile.urls) {
      var httpOptions = {
        'headers': {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()}             
      }
      var contents = UrlFetchApp.fetch(newReportFile.urls.apiUrl, httpOptions);              //Makes a request to fetch (получать) a URL using optional advanced parameters
      if(newReportFile.format == 'CSV') {                                                 
   
        var rows = Utilities.parseCsv(contents.getContentText());                                   
        if(rows && rows.length) {
          var fileName = "DCM_test_work5";
          var spreadSheet = SpreadsheetApp.create(fileName);
          var sheet = spreadSheet.getActiveSheet();
          rows.map(function(r) { sheet.appendRow(r); });
        }
      } else {
      // Store the Excel file directly
        Logger.log('not CSV!');
        DocsList.createFile(contents.getBlob()).rename(newReportFile.fileName)
      }  
    }
}
&#13;
&#13;
&#13;