Posts tagged with google-apps-script

I have created a script in GoogleAds to fetch this report: https://developers.google.com/adwords/api/docs/appendix/reports/product-partition-report from GoogleAds and insert the result as a table in BigQuery using the AdvanceAPI in GoogeAds.

The script I am running is built according to this sample code: https://developers.google.com/adwords/api/docs/appendix/reports/product-partition-report, provided by the Google Ads Script Team and they have also tried to help me in this matter.

The issue I am facing is that the first run of the script runs perfectly and creates a table in BQ and inserts the data as intended. However, when the script runs again (as sheduled 3 PM everyday) to get updated data from GoogleAds I get the following error message: API call to bigquery.tables.insert failed with error: Already Exists

The error message also provides the information that the issue is somewhere in this code:

table.tableReference = BigQuery.newTableReference(); table.tableReference.datasetId = CONFIG.BIGQUERY_DATASET_ID; table.tableReference.projectId = CONFIG.BIGQUERY_PROJECT_ID; table.tableReference.tableId = reportConfig.NAME; table = BigQuery.Tables.insert(table, CONFIG.BIGQUERY_PROJECT_ID,     CONFIG.BIGQUERY_DATASET_ID); 

But all this chunk of code is provided from the Google Ads Script Team so there should not be any problem with this?

So happy if someone could give me any guidance how to solve this!

Thanks in advance.

I'm getting to Google Ads scripts. I managed to execute this script that pauses a specific campaign but I couldn't apply this to multiple campaigns. Here is what I tried, but it only pauses the first campaign in ("Name IN ['test1', 'test2']"). Could someone help me to achieve this?

function main () {   var campaignIterator = AdsApp.campaigns()     .withCondition("Name IN ['test1', 'test2']").get();   if (campaignIterator.hasNext()) {     var campaign = campaignIterator.next();     campaign.pause();   } }

When I run an ads-script to call the app-script::spreadsheet API, I get the following error:

var sheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL); var rangeValues = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues(); ==> Cannot find method getRange(number,number,number,number) 

How can it be? Only a subset of the app-script sheet api is available from ads-script?

I'm trying to pull a report from the Google Ads API into Google sheets and I can't get the API to recognize my query as a query

Here's the code and error I'm getting:

    function basicReport() {   var query = {     "query" : "SELECT campaign.name, campaign.status FROM campaign ORDER BY campaign.id"   };      var body = JSON.stringify(query);      var head = {     'Developer-token' : "<Dev token>",     'login-customer-id' : <Manager ID>,     'Authorization' : "Bearer <Auth token>",   }; var options = {   'method' : 'POST',   'content-type': 'application/json',   'headers' : head,   'payload' : body,   'muteHttpExceptions' : true };    var response = UrlFetchApp.fetch('https://googleads.googleapis.com/v4/customers/<Customer ID>/googleAds:searchStream', options);   var json = response.getContentText();   var data = JSON.parse(json); 

But I constantly get the error:

"error": {     "code": 400,     "message": "Invalid JSON payload received. Unknown name \"{\"query\":\"SELECT campaign.name, campaign.status FROM campaign ORDER BY campaign.id\"}\": Cannot bind query parameter. Field '{\"query\":\"SELECT campaign' could not be found in request message.",     "status": "INVALID_ARGUMENT",     "details": [       {         "@type": "type.googleapis.com/google.rpc.BadRequest",         "fieldViolations": [           {             "description": "Invalid JSON payload received. Unknown name \"{\"query\":\"SELECT campaign.name, campaign.status FROM campaign ORDER BY campaign.id\"}\": Cannot bind query parameter. Field '{\"query\":\"SELECT campaign' could not be found in request message." 

I've run the query in OAuth playground (https://developers.google.com/oauthplayground) and it worked there, so I know the query is ok.

I've tried passing the body as an object not a string, but then I get a 500 error.

I need to export campaign statistics to .xlsx, which is stored in Google Drive. I substitute the link to this document in the script, and I get this error:

"Service Spreadsheets failed while accessing document with id 1S_rjqsgbjyTKXEo14j_m2xtoIwgHCFi1. (file Code.gs, line 18)".

In the privacy parameters of the document, "editing for everyone who has the link" is configured. How do I solve the problem? I tested these two links: https://docs.google.com/spreadsheets/d/1S_rjqsgbjyTKXEo14j_m2xtoIwgHCFi1/edit#gid=1649084583/ https://drive.google.com/file/d/1S_rjqsgbjyTKXEo14j_m2xtoIwgHCFi1/view?usp=sharing

Here is the script itself.

function main() {  //  var url = 'https://drive.google.com/file/d/1S_rjqsgbjyTKXEo14j_m2xtoIwgHCFi1/view?usp=sharing/';  var datasheet = 'Ali_web_all_acc_export.xlsx';  var columns = 'CampaignName, AdvertisingChannelType, Cost, Date';  var report = 'CAMPAIGN_PERFORMANCE_REPORT';  var selector = 'CampaignName DOES_NOT_CONTAIN "Шашлык15loooool"';  var during = 'YESTERDAY';  // Функция выгрузки  exportReportToSpreadsheet(url, datasheet, columns, report, selector, during); } function exportReportToSpreadsheet(url, datasheet, columns, report, selector, date) {  var spreadsheet = SpreadsheetApp.openByUrl(url);  var sheet = spreadsheet.getSheetByName(datasheet);  var report = AdWordsApp.report(    'SELECT ' + columns + ' ' +    'FROM ' + report + ' ' +    'WHERE ' + selector +' ' +    'DURING '+ date + ' '    );  report.exportToSheet(sheet); }