Create a new sheet for each row in a Google Sheets spreadsheet using Apps Script
In this tutorial, I will show you how to create new sheets in a Google Sheets spreadsheet based on the data in a given sheet. Let's say that you have a sheet called CreateSheets containing the names of sheets you want created programmatically. You want to create a new sheet in your spreadsheet for each row in this sheet. I will show you how to use Apps Script to automatically create new sheets based on the data that you've entered in Google Sheets.
Below is a screenshot of the sheet containing names of sheets you want created.
Prerequisites
This tutorial assumes that you're familiar with the following concepts:
The code below reads data from a sheet called CreateSheet" and inserts a new sheet for each row in this sheet.
Note
To insert a new sheet in a Google Sheets spreadsheet, we will use the insertSheet() method of the Spreadsheet object.
function createNewSheetPerRow() {
// Get the data from the sheet called CreateSheets
var sheetNames = SpreadsheetApp.getActive().getSheetByName("CreateSheets").getDataRange().getValues();
// For each row in the sheet, insert a new sheet and rename it.
sheetNames.forEach(function(row) {
var sheetName = row[0];
var sheet = SpreadsheetApp.getActive().insertSheet();
sheet.setName(sheetName);
});
}
When you run the createNewSheetPerRow()
function, you'll see three new sheets created because the CreateSheets sheet had three rows in it.
Conclusion
In this tutorial, I showed you how to create a new sheet for every row in a Google Sheet.
Thanks for reading!
Master Google Sheets Automation
I'd appreciate any feedback you can give me regarding this post.
Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!