Search code examples
google-apps-scriptgoogle-sheetsgoogle-ads-api

Exporting Google Ads Data To Specific Cell Range


I have an ads script which exports data to a sheet based on rules.

However, I only want to replace data within a certain cell range such as 'A2:J' within a Google sheet.

I know how to do this within spreadsheetApp script but unsure/struggling on Ads script/AWQL.

I have pasted the basics of the script below but really could do with insight into whether and how it is possible to push to a specific range.

function main(){

// Put your Google data sheet here
var sheetURL = '';

// Enter the tab name here
var tabName = '';

var QUERIES = [{'query' : 'SELECT Date, DayOfWeek, HourOfDay, Device, CampaignName, Impressions, Clicks, Cost, Conversions, ConversionValue ' +
                        'FROM CAMPAIGN_PERFORMANCE_REPORT ' +
            'WHERE Impressions > 0 ' +
                      'DURING TODAY',
                      //'DURING ' + dateRanges, 
            'spreadsheetUrl' : sheetURL,
            'tabName' : tabName,
            'reportVersion' : 'v201809'
           }            
          ];

//This is to gather the above query and push it to the spreadsheet stated above
  for(var i in QUERIES) {
    var queryObject = QUERIES[i];
    var query = queryObject.query;
    var spreadsheetUrl = queryObject.spreadsheetUrl;
    var tabName = queryObject.tabName;
    var reportVersion = queryObject.reportVersion;
    //Logger.log(spreadsheetUrl + " " + query);
    var spreadsheet = SpreadsheetApp.openByUrl(spreadsheetUrl);
    var sheet = spreadsheet.getSheetByName(tabName);
    var report = AdWordsApp.report(query, {apiVersion: reportVersion});
    report.exportToSheet(sheet);
  }
}

Solution

  • Answer:

    Basically, it is not possible to export a report only to a certain range of the sheet directly. As you can see in the official docs, exportToSheet(sheet) only accepts a sheet as an argument, and there are no additional optional arguments to accomplish your purpose.

    Workaround:

    As Cooper and yourself commented, you can export the report to another sheet, and from there copy the desired range to your final sheet.

    If you don't want to have this additional sheet around all the time, I'd propose creating a new sheet every time you use exportTosheet, exporting the report to this newly created sheet, and finally deleting this sheet after it has been copied to the final sheet.

    Also, since last November 2018, you should be using AdsApp instead of AdWordsApp, as explained here.