Rename a sheet in Google Sheets using Apps Script
In this tutorial, I will show you how to rename a sheet in a Google Sheets spreadsheet using Apps Script. This is actually very easy to do and the Apps Script code is just a couple of lines long.
Prerequisites
This tutorial assumes that you're familiar with the following concepts:
Note
To rename a sheet in a Google Sheets spreadsheet, we will use the setName()
method of the Sheet object.
The function renameSheet()
accepts two parameters: (1) the current name of the sheet and (2) its new name.
First, we get a reference to the sheet by using the getSheetByName(currentName)
method of the Sheet class and then we rename it using the setName(newName)
method.
function renameSheet(currentName, newName) {
// Get a reference to the sheet using its existing name
// and then rename it using the setName() method.
SpreadsheetApp.getActive().getSheetByName(currentName).setName(newName);
}
To use the function, just call it with the sheet's current and new name.
renameSheet("hr", "Human Resources");
Conclusion
In this tutorial, I showed you how to rename a sheet in a Google Sheets spreadsheet using Apps Script.
Thanks for reading!