Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new sheet in a Google Sheets with Google Apps Script

How to create a new sheet in a Google Sheets with Google Apps Script?

I know it seems obvious but I just want to create a new sheet with a specific name.

like image 689
Léo Davesne Avatar asked Apr 06 '14 18:04

Léo Davesne


People also ask

How do you get a sheet in an app script?

To get started, visit sheets.google.com and create a new sheet. Under the "Tools" menu in your new sheet, click the option for "Script editor…" That'll open a new tab with a blank script file: the Script Editor. It's an easy way to create both standalone and bound scripts without leaving Google Apps.


2 Answers

Surprisingly I didn't find any clear and quick answer.

So here is my code:

function onOpen() {     var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();     var yourNewSheet = activeSpreadsheet.getSheetByName("Name of your new sheet");      if (yourNewSheet != null) {         activeSpreadsheet.deleteSheet(yourNewSheet);     }      yourNewSheet = activeSpreadsheet.insertSheet();     yourNewSheet.setName("Name of your new sheet"); } 

Finally, note that this new sheet will be automatically the active one.

like image 101
Léo Davesne Avatar answered Sep 18 '22 17:09

Léo Davesne


Here is a simple example:

var name = (new Date()).toLocaleDateString(); SpreadsheetApp.getActiveSpreadsheet().insertSheet(name); 
like image 32
Benoit Avatar answered Sep 18 '22 17:09

Benoit