AdWords
4K members online now
4K members online now
Use AdWords conversion tracking and reporting to measure your results. Have a question about Google Analytics? Post it here, on the Google Analytics Community!
Guide Me
star_border
Reply

AdWords data to Google Sheets with scripts

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

I'm new to scripts.  Have scripts writing to Google Sheets but always goes to the 1st tab (Sheet1).  When I go to the publish option in the Sheet I can get URLs for each of the tabs but no matter what URL I use in the script the info still goes to Sheet1 (or whatever I've called it).

 

Is there a way to use scripts to send AdWords data to sheet2, sheet3, etc?

1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Ed G
March 2017

AdWords data to Google Sheets with scripts

Explorer ✭ ✭ ✭
# 6
Explorer ✭ ✭ ✭

 

Change this:

report.exportToSheet(spreadsheet.getActiveSheet());

 

for this: 

report.exportToSheet(sheet);

 

Now it should work perfectly.

 

Regards,

 

Hana

View solution in original post

AdWords data to Google Sheets with scripts

Explorer ✭ ✭ ✭
# 2
Explorer ✭ ✭ ✭

Hello @Ed G

 

You can use this code to switch to different sheet or create a new one in case the sheet with the specified name does not exist.

 

var sheet = spreadsheet.getSheetByName("Sheet 2");
if (sheet === null) {
sheet = spreadsheet.insertSheet("Sheet 2");
}

 

Regards

 

Hana

 

AdWords data to Google Sheets with scripts

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

Thanks Hanna K

 

I'm sure I'm leaving something out.  Here's the entire script

 

function main() {
var SPREADSHEET_URL = 'https://docs.google.com/spreadsheets/d/1MU6K-y0LBqXr_kIbYtbGwEP0Ja2eY5K65uIkimXMC3o/pubhtml';
var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var sheet = spreadsheet.getSheetByName("Sheet 2");
var report = AdWordsApp.report("SELECT CampaignName, AdGroupName, Conversions " +
"FROM ADGROUP_PERFORMANCE_REPORT " +
"DURING LAST_30_DAYS");
report.exportToSheet(spreadsheet.getActiveSheet());
Logger.log("Report available at " + spreadsheet.getUrl());
}

 

I'm still writing to Sheet1

AdWords data to Google Sheets with scripts

Explorer ✭ ✭ ✭
# 4
Explorer ✭ ✭ ✭

I opened your spreadsheet and I see, that the sheet name is "Sheet2" but on the line 4 you are trying to open a sheet called "Sheet 2" and it is not the same. Remove the space.

 

Did it help?

 

AdWords data to Google Sheets with scripts

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

Good point.  I got rid of the space but still writing to Sheet1.

 

Looking at the script again without the space

 

function main() {
var SPREADSHEET_URL = 'https://docs.google.com/spreadsheets/d/1MU6K-y0LBqXr_kIbYtbGwEP0Ja2eY5K65uIkimXMC3o/pubhtml';
var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var sheet = spreadsheet.getSheetByName("Sheet2");
var report = AdWordsApp.report("SELECT CampaignName, AdGroupName, Conversions " +
"FROM ADGROUP_PERFORMANCE_REPORT " +
"DURING LAST_30_DAYS");
report.exportToSheet(spreadsheet.getActiveSheet());
Logger.log("Report available at " + spreadsheet.getUrl());
}

 

In the line 

eport.exportToSheet(spreadsheet.getActiveSheet());

 

How does the script know which sheet I'm referring to when I use - getActiveSheet

 

I really appreciate your help. 

 

 

Marked as Best Answer.
Solution
Accepted by topic author Ed G
March 2017

AdWords data to Google Sheets with scripts

Explorer ✭ ✭ ✭
# 6
Explorer ✭ ✭ ✭

 

Change this:

report.exportToSheet(spreadsheet.getActiveSheet());

 

for this: 

report.exportToSheet(sheet);

 

Now it should work perfectly.

 

Regards,

 

Hana

AdWords data to Google Sheets with scripts

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭

That's it!

 

Thank you very much!

AdWords data to Google Sheets with scripts

Visitor ✭ ✭ ✭
# 8
Visitor ✭ ✭ ✭

Hi Hana,

 

I have written the below code to get data from google adwords to google sheets

 

function main() {
var SPREADSHEET_URL = 'https://docs.google.com/spreadsheets/d/1zaCUIuNVdkSkpTEHb1ETHnktJfOSLBBcb0bTdyDk13w/edit#gid=3891937...';
var spreadsheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var sheet = spreadsheet.getSheetByName("report");
var report = AdWordsApp.report("SELECT CampaignName, AdGroupName, Conversions " +
"FROM ADGROUP_PERFORMANCE_REPORT " +
"DURING LAST_30_DAYS");
report.exportToSheet(sheet);
Logger.log("Report available at " + spreadsheet.getUrl());
}

 

But it is giving the error "ReferenceError: "AdWordsApp" is not defined. (line 122, file "Code")

 

Please could you help?

 

Thanks & Regards,

Aasim