How to populate adwords data in specific cells of a google docs spreadsheet using AdWords Scripts?

a 夏天 提交于 2019-12-23 04:44:03

问题


I don’t have a programming background and just started learning JavaScript basics so I can become fluent at creating custom Google AdWords scripts to help me automate client reporting. I’ve spent many hours with trial and error and haven’t figured out to this problem (which is probably super easy for programmers):

I’m trying to populate a spreadsheet that I have made (here’s the shortened link: http://goo.gl/wLAAx) with account level data so I can calculate the run rate of an account. All I’m trying to do is run a script that will run every morning that will append a new row in the spreadsheet with yesterday’s date and populate its respective data, like clicks, impressions, cost, and conversions.

Thanks in advance for any help and information!


回答1:


Here's a script that'd do what you want (it uses a slightly different spreadsheet, check it out). Also, the script is only reading in the first 365 rows in the spreadsheet, so it'll break in a year.

In the future, you may want to check with https://developers.google.com/adwords/scripts for resources and community oriented towards scripts

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;
    }
  }
}


来源:https://stackoverflow.com/questions/16069480/how-to-populate-adwords-data-in-specific-cells-of-a-google-docs-spreadsheet-usin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!