Phil Penny
Phil Penny

Reputation: 123

Google script create new spreadsheet and set as active

I can create a new spreadsheet using:

var newWorksheet = SpreadsheetApp.create("newWorksheetName");

However, how do I immediately set it as active so I can edit it?

As far as I know, I need to get the spreadsheet ID, to open it, but I don't know how to find this out automatically.

Upvotes: 0

Views: 2312

Answers (1)

Marios
Marios

Reputation: 27350

Try this:

var crNew = SpreadsheetApp.create("newWorksheetName");
var ssNew = SpreadsheetApp.openByUrl(crNew.getUrl());

where ssNew is the new Spreadsheet file.

Then you can use ssNew to directly access the new Spreadsheet file. For example, the following will give you the name of the first sheet of the newly generated newWorksheetName:

Logger.log(ssNew.getSheets()[0].getName());

Output: Sheet1

Another example would be to change the name of the sheet:

ssNew.getSheets()[0].setName("newname");

Upvotes: 3

Related Questions