1

First off, let me say that I am not a developer, nor do I really code beyond basic HTML. So I appreciate your patience. :)

I'm working with a script that is for AdWords, but I believe it's more or less written in Javascript. (I've included the script below.)

Basically, I'm receiving the error message 'Parsing Error: Please check your selector. (line XX)' when I preview the script.

I've searched all around for hours and have yet to find a solution.

I think it may be that a query being returned contains either a single or double quote, and may be messing up the code? Though I can't actually prove that.

Also, yes, I was sure to update lines 17-21 with the correct details.

Any help would be much appreciated!

Thanks! John

/* 
// AdWords Script: Put Data From AdWords Report In Google Sheets
// --------------------------------------------------------------
// Copyright 2017 Optmyzr Inc., All Rights Reserved
// 
// This script takes a Google spreadsheet as input. Based on the column headers, data filters, and date range specified
// on this sheet, it will generate different reports.
//
// The goal is to let users create custom automatic reports with AdWords data that they can then include in an automated reporting
// tool like the one offered by Optmyzr.
//
//
// For more PPC management tools, visit www.optmyzr.com
//
*/

var DEBUG = 0; // set to 1 to get more details about what the script does while it runs; default = 0
var REPORT_SHEET_NAME = "report"; // the name of the tab where the report data should go
var SETTINGS_SHEET_NAME = "settings"; // the name of the tab where the filters and date range are specified
var SPREADSHEET_URL = "https://docs.google.com/spreadsheets/d/1dttJTb547L81XYKdTQ56LcfO9hHhbb9wm06ZY5mKhEo/edit#gid=0"; // The URL to the Google spreadsheet with your report template
var EMAIL_ADDRESSES = "example@example.com"; // Get notified by email at this address when a new report is ready

function main() {
  var currentSetting = new Object();
  currentSetting.ss = SPREADSHEET_URL;

  // Read Settings Sheet
  var settingsSheet = SpreadsheetApp.openByUrl(currentSetting.ss).getSheetByName(SETTINGS_SHEET_NAME);
  var rows = settingsSheet.getDataRange();
  var numRows = rows.getNumRows();
  var numCols = rows.getNumColumns();
  var values = rows.getValues();
  var numSettingsRows = numRows - 1;

  var sortString = "";
  var filters = new Array();
  for(var i = 0; i < numRows; i++) {
    var row = values[i];
    var settingName = row[0];
    var settingOperator = row[1];
    var settingValue = row[2];
    var dataType = row[3];
    debug(settingName + " " + settingOperator + " " + settingValue);

    if(settingName.toLowerCase().indexOf("report type") != -1) {
      var reportType = settingValue;
    } else if(settingName.toLowerCase().indexOf("date range") != -1) {
      var dateRange = settingValue;
    } else if(settingName.toLowerCase().indexOf("sort order") != -1) {
      var sortDirection = dataType || "DESC";
      if(settingValue) var sortString = "ORDER BY " + settingValue + " " + sortDirection;
      var sortColumnIndex = 1;
    }else {
      if(settingOperator && settingValue) {
        if(dataType.toLowerCase().indexOf("long") != -1 || dataType.toLowerCase().indexOf("double") != -1 || dataType.toLowerCase().indexOf("money") != -1 || dataType.toLowerCase().indexOf("integer") != -1) {
          var filter =  settingName + " " + settingOperator + " " + settingValue;
        } else {
          if(settingValue.indexOf("'") != -1) {
            var filter =  settingName + " " + settingOperator + ' "' + settingValue + '"';
          } else if(settingValue.indexOf("'") != -1) {
            var filter =  settingName + " " + settingOperator + " '" + settingValue + "'";
          } else {
            var filter =  settingName + " " + settingOperator + " '" + settingValue + "'";
          }
        }
        debug("filter: " + filter)
        filters.push(filter);
      }
    }
  }


  // Process the report sheet and fill in the data
  var reportSheet = SpreadsheetApp.openByUrl(currentSetting.ss).getSheetByName(REPORT_SHEET_NAME);
  var rows = reportSheet.getDataRange();
  var numRows = rows.getNumRows();
  var numCols = rows.getNumColumns();
  var values = rows.getValues();
  var numSettingsRows = numRows - 1;

  // Read Header Row and match names to settings
  var headerNames = new Array();
  var row = values[0];
  for(var i = 0; i < numCols; i++) {
    var value = row[i];
    headerNames.push(value);
    //debug(value);
  } 



  if(reportType.toLowerCase().indexOf("performance") != -1) {
    var dateString = ' DURING ' + dateRange;
  } else {
    var dateString = "";
  }
  if(filters.length) {
    var query = 'SELECT ' + headerNames.join(",") + ' FROM ' + reportType + ' WHERE ' + filters.join(" AND ") + dateString + " " + sortString;
  } else {
    var query = 'SELECT ' + headerNames.join(",") + ' FROM ' + reportType + dateString + " " + sortString;
  }
  debug(query);
  var report = AdWordsApp.report(query); //THIS IS LINE 103 WITH THE ERROR
  try {
    report.exportToSheet(reportSheet);
    var subject = "Your " + reportType + " for " + dateRange + " for " + AdWordsApp.currentAccount().getName() + " is ready";
    var body = "currentSetting.ss<br>You can now add this data to <a href='https://www.optmyzr.com'>Optmyzr</a> or another reporting system.";
    MailApp.sendEmail(EMAIL_ADDRESSES, subject, body);
    Logger.log("Your report is ready at " + currentSetting.ss);
    Logger.log("You can include this in your scheduled Optmyzr reports or another reporting tool.");
  } catch (e) {
    debug("error: " + e);
  }

}

function debug(text) {
  if(DEBUG) Logger.log(text);
}
  • What line number is your error on? Please include that, and also mark it in the code with a comment so it is easy to find, since line numbers are not displayed in this interface. – sorak Mar 13 '18 at 19:15
  • Ah, yes! Thanks for taking a look! The error is line 103 var report = AdWordsApp.report(query); which is now marked with a comment. Thanks again! – user2056189 Mar 14 '18 at 19:57
  • Okay, so the error is not in your Javascript code. That message is returned by the AdWords server in response to your query. Can you show us the query that it is running? – sorak Mar 14 '18 at 20:16
  • Thanks, sorak! I'm not entirely sure what it means to show you the query it's running, but what it's try do, in this particular case, is show the Account Performance for an AdWords campaign. I'm wondering if it's pulling in a header that contains an apostrophe and is messing up the code? I've seen some ideas that involve using some code to switch all apostrophes to double quotes? Again, I'm not a coder... But if you let me know exactly what you need, I'll let you know! – user2056189 Mar 14 '18 at 23:30
  • print out the query variable and let's see the string that's being generated and sent to the server – sorak Mar 14 '18 at 23:55
  • Hi sorak, sorry again for my ignorance... How do I do that exactly? Thanks again for jumping in! – user2056189 Mar 15 '18 at 12:32
  • Before line 103 insert: `console.log('Query: '+query);` ... then when it is triggered by your browser the query should appear in your browser console. You can also put in `debugger;` to inspect the variable in the debugger, if you're using Chrome and comfortable with that method. – sorak Mar 15 '18 at 14:24
  • Hm, I placed the `console.log('Query: '+query);` code right before the line with the issue and received this error: ReferenceError: "console" is not defined. (On the line where I entered the new code.) – user2056189 Mar 15 '18 at 14:39
  • Try Logger.log('Query: '+query) ... and then examine the logger to see the output – sorak Mar 15 '18 at 14:44
  • That seems to work to show the query. Here's the message I saw `SELECT FROM ACCOUNT_PERFORMANCE_REPORT DURING LAST_30_DAYS Query: SELECT FROM ACCOUNT_PERFORMANCE_REPORT DURING LAST_30_DAYS Parsing error. Please check your selector. (line 104)' (I guess my theory of AdWords trying to import an apostrophe is bust...) – user2056189 Mar 15 '18 at 15:02
  • Makes sense. The area between SELECT and FROM is the selector. You're not selecting any fields with that query. That's happening because the headerNames array is empty. Verify the value of REPORT_SHEET_NAME – sorak Mar 15 '18 at 15:08
  • sorak! You've fixed it! It seems that the Google Sheet to which I was importing wasn't setup correctly... I feel so stupid now. Haha. But thank you so much for your help on this! As I start to get more and more into coding, this website will be a great resource! Thanks again for your time, sorak! – user2056189 Mar 15 '18 at 15:16

1 Answers1

2

The area between SELECT and FROM is the selector. You're not selecting any fields with that query. That's happening because the headerNames array is empty. Verify the value of REPORT_SHEET_NAME

sorak
  • 2,607
  • 2
  • 16
  • 24
  • Would you accept this answer? I didn't mean to conduct the entire correspondence in the question comments, but at least we got it fixed. – sorak Mar 15 '18 at 15:22