Export data to Google sheet to a specific sheet

675 views
Skip to first unread message

Søren Jespersen

unread,
Jul 11, 2022, 12:37:11 PM7/11/22
to Google Ads Scripts Forum
Hi

I am very new to scripts, and don't really know how to code, but can copy and paste different script together, but not much more than that. 

I have created a script that is suppose to export data to a Google sheet, to a specific sheet. I can make it work and export but only to the first sheet in my Google sheet, but when i add a line to export it to a specific sheet the script fail.

I have tried some different solutions but have not been able to make it work.

This is what the script looks like now, it always gives this error message: 
ReferenceError: ss is not defined at main (Code:9:14)
So i know that is is line 9 where i try to specify what sheet i what it to export to. Have tried to set it under var SPREADSHEET_URL but it didn't work either. and i have also tried a different line that didn't work either 
var sheet = spreadsheet.getSheetByName('Sheet1');

***Script starts here***

var SPREADSHEET_URL = 'SHEET-URL';

function main() {
 var sheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL).getActiveSheet();
 var sheet = ss.getSheetByName('Sheet2');
 var range = sheet.getRangeList(['A1:A', 'B1:B'])
 range.clearContents();
 sheet.getRange("A1").setValue("Campaign");
 sheet.getRange("B1").setValue("Ad groups");

Google Ads Scripts Forum Advisor

unread,
Jul 11, 2022, 1:36:02 PM7/11/22
to adwords...@googlegroups.com
Hello,

Thanks for reaching out. 

The openByUrl method is expecting a URL, rather than a sheet name.

Here is an example that first gets the spreadsheet using a URL, and then gets a specific sheet, by name, within that spreadsheet:

  const SPREADSHEET_URL = 'INSERT_SPREADSHEET_URL_HERE';
  // Name of the specific sheet in the spreadsheet.
  const SHEET_NAME = 'INSERT_SHEET_NAME_HERE';

  const ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
  const sheet = ss.getSheetByName(SHEET_NAME);

More spreadsheet examples can be found here.

Regards,

Google Logo
Matt
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2cUqk4:ref

Dave Roeser

unread,
Jul 11, 2022, 3:25:43 PM7/11/22
to Google Ads Scripts Forum
Hi,

Did you mean this line
var sheet = SpreadsheetApp.openByUrl(SPREADSHEET_URL).getActiveSheet();
to be
var ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL).getActiveSheet();
instead?
Take care,
Dave

--
-- You received this message because you are subscribed to the Google Groups AdWords Scripts Forum group. Please do not reply to this email. To post to this group or unsubscribe please visit https://developers.google.com/adwords/scripts/community.
---
You received this message because you are subscribed to the Google Groups "Google Ads Scripts Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adwords-scrip...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/adwords-scripts/25106c8f-9b31-4314-8b72-7450db98a1b5n%40googlegroups.com.

Google Ads Scripts Forum Advisor

unread,
Jul 12, 2022, 3:18:14 AM7/12/22
to adwords...@googlegroups.com

Hello Dave,

I’m James from the Google Ads scripts support team as well. Allow me to assist you further. I believe that based on my colleagues previous response, your code should be written according to the given code below.

function main() {
const SPREADSHEET_URL = 'INSERT_SPREADSHEET_URL_HERE';

// Name of the specific sheet in the spreadsheet.
const SHEET_NAME = 'INSERT_SHEET_NAME_HERE';

const ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
const sheet = ss.getSheetByName(SHEET_NAME);

var range = sheet.getRangeList(['A1:A', 'B1:B'])
  range.clearContents();
​ ​​​​​​ sheet.getRange("A1").setValue("Campaign");
  sheet.getRange("B1").setValue("Ad groups"); 
}

Let me know if you have any questions.

Regards,

Google Logo
James Howell
Google Ads Scripts Team
 


ref:_00D1U1174p._5004Q2cUqk4:ref
Reply all
Reply to author
Forward
0 new messages