To create a new sheet within a Google Sheet using Apps Script, you can use the insertSheet()
method of the Spreadsheet
class. Here’s an example code snippet:
function createNewSheet() { var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var newSheetName = 'New Sheet'; // Replace with the desired name for the new sheet var newSheet = spreadsheet.insertSheet(); newSheet.setName(newSheetName); }
In this code, the createNewSheet
function first retrieves the active spreadsheet using SpreadsheetApp.getActiveSpreadsheet()
. You can modify this line to access a specific spreadsheet if needed.
Next, you can specify the desired name for the new sheet by assigning it to the newSheetName
variable. In this example, it’s set to 'New Sheet'
, but you can change it to any desired name.
The insertSheet()
method is then called on the spreadsheet
object to create a new sheet. By default, the new sheet is inserted as the last sheet in the spreadsheet.
Finally, the setName()
method is used on the newSheet
object to set the name of the new sheet to the specified newSheetName
.
You can call the createNewSheet
function from another function or set up a trigger to execute it based on your needs. When executed, it will create a new sheet within the Google Sheet with the specified name.
Feel free to modify the code to suit your specific requirements and perform additional operations on the newly created sheet, such as populating data or applying formatting.