使用基本广告系列数据创建和更新电子表格

时间:2016-07-22 16:35:54

标签: google-apps-script google-adwords

我刚开始尝试使用Google Adwords Scripts和Google Spreadsheet。我正在尝试从我们的经理帐户中提取基本广告系列数据,并将该数据发送到工作表。

我编写了以下内容,但每次运行脚本时,都会创建一个包含标题/列名但不包含实际数据的电子表格。

我哪里出错了?

function main() {
  var spreadsheet = SpreadsheetApp.create('Adwords Testing');
  var report = AdWordsApp.report(
      'SELECT CampaignName, Clicks, Conversions, Cost, Ctr, AverageCpc, Impressions ' +
      'FROM   CAMPAIGN_PERFORMANCE_REPORT ' +
      'WHERE  CampaignStatus = ENABLED ' +
      'DURING LAST_7_DAYS');

  var rows = report.rows();
  while (rows.hasNext()){
    var row = rows.next();
    var campaignName = ['CampaignName'];
    var clicks = ['Clicks'];
    var conversions = ['conversions'];
    var cost = ['Cost'];
    var avgcpc = ['AverageCpc'];
    var ctr = ['Ctr'];
    var impressions = ['Impressions'];
  }
  report.exportToSheet(spreadsheet.getActiveSheet());
  Logger.log('your sheet is available at ' + spreadsheet);
  
}

1 个答案:

答案 0 :(得分:0)

AdWords Scripts

  

如果您有兴趣进一步编写AdWords帐户脚本,请查看AdWords脚本,这是 Apps脚本嵌入AdWords界面的版本。

我认为这是你的错误所在:

report.exportToSheet(spreadsheet.getActiveSheet());

要为行添加值,您将使用getRange()getValuessetValues()使用Spreadsheet Service的类。

getRange()

  

获取此命名范围引用的范围。

的GetValues()

  

返回此范围的矩形网格值。返回一个二维数组值,按行索引,然后按列索引。值可以是NumberBooleanDateString类型,具体取决于单元格的值。空单元格将由数组中的空字符串表示。请记住,当范围索引从1, 1开始时,JavaScript数组将从[0][0]编入索引。

// The code below will get the values for the range C2:G8
// in the active spreadsheet. Note that this will be a javascript array.
var values = SpreadsheetApp.getActiveSheet().getRange(2, 3, 6, 4).getValues();
Logger.log(values[0][0]);

setValues方法()

  

设置矩形网格值(必须与此范围的尺寸匹配)。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// The size of the two-dimensional array must match the size of the range.
var values = [
[ "2.000", "1,000,000", "$2.99" ]
];

var range = sheet.getRange("B2:D2");
range.setValues(values);

以下是iluxa的摘要:

function main() {
var sheet = SpreadsheetApp.openByUrl(
"https://docs.google.com/spreadsheet/ccc?key=0Aj_Bw31w9s7LdEdLMllQMnZfbW52c3BvcTh0ekVBQ3c#gid=0").getActiveSheet();
var emptyRow = findEmptyRow(sheet);

var yesterday = new Date(new Date().getTime() - (24 * 3600 * 1000));

var range = sheet.getRange(emptyRow + 1, 1, 1, 10);

var row = range.getValues();
row[0][0] = yesterday;
row[0][1] = MONTHS[yesterday.getMonth()];
row[0][2] = DAYS_OF_WEEK[yesterday.getDay()];

var stats = AdWordsApp.report("SELECT Clicks, Impressions, Cost, Conversions " +
"FROM ACCOUNT_PERFORMANCE_REPORT DURING YESTERDAY")
.rows()
.next();
row[0][3] = stats["Clicks"];
row[0][4] = stats["Impressions"];
row[0][7] = stats["Cost"];
row[0][8] = stats["Conversions"];
range.setValues(row);
}

function findEmptyRow(sheet) {
var dates = sheet.getRange(1, 1, 365, 1).getValues();
for (var emptyDate = 0; emptyDate < dates.length; emptyDate ++) {
if (dates[emptyDate][0].length == 0) {
return emptyDate;
}
}
}

希望它有所帮助!