简体   繁体   中英

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

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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