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

Léo Davesne picture Léo Davesne · Apr 6, 2014 · Viewed 65.5k times · Source

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.

Answer

Léo Davesne picture Léo Davesne · Apr 6, 2014

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.