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

Google Apps-ScriptGoogle Sheets

Google Apps-Script Problem Overview


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.

Google Apps-Script Solutions


Solution 1 - Google Apps-Script

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.

Solution 2 - Google Apps-Script

Here is a simple example:

var name = (new Date()).toLocaleDateString();
SpreadsheetApp.getActiveSpreadsheet().insertSheet(name);

Solution 3 - Google Apps-Script

I'd recommend this method.

var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet('My New Sheet');

Where 'My New Sheet' is the name you want it to be.

Solution 4 - Google Apps-Script

Here's the way I did it...

var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var newSheet = activeSpreadsheet.insertSheet();
newSheet.setName("whatever");

Solution 5 - Google Apps-Script

I have been looking at the answers and although they answer the question of how to create a sheet and give it a name, none shows how to make that name variable by passing it as a parameter

This is an example of a function passing a variable to the createNewSheet function:

(The function that has to be executed is setNameAndCreateSheet)

function createNewSheet(sheetName){
  // The sheetName parameter has to be passed to the function when it's called
  let activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  let newSheet = activeSpreadsheet.insertSheet();
  newSheet.setName(sheetName); // We sheet will be called as the string of the parameter
}

function setNameAndCreateSheet(){
  // This will get the email of the user executing the script
  const userEmail = Session.getEffectiveUser().getEmail();

  // This will get the current date
  const date = Utilities.formatDate(new Date(), "GMT-3", "dd/MM/yyyy");

  // We are making the string that will be passed to the createNewSheet function
  const sheetName = date + " - " + userEmail;
  // sheetName will return something like: "04/11/2021 - [email protected]"

  // This will execute the function that creates the sheet, and the name will be settled to the parameter passed (sheetName)
  createNewSheet(sheetName);
}

In this example, the parameter passed to the createNewSheet function is the concatenation of these two strings (date - useremail), but it could be anything.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLéo DavesneView Question on Stackoverflow
Solution 1 - Google Apps-ScriptLéo DavesneView Answer on Stackoverflow
Solution 2 - Google Apps-ScriptBenoitView Answer on Stackoverflow
Solution 3 - Google Apps-ScriptBadSushiView Answer on Stackoverflow
Solution 4 - Google Apps-ScriptJaystewView Answer on Stackoverflow
Solution 5 - Google Apps-ScriptNicolas DomergueView Answer on Stackoverflow