To rename a sheet in a Google Sheet using Apps Script, you can use the setName()
method of the Sheet
class. Here’s an example code snippet:
function renameSheet() { var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); var sheetName = 'Sheet1'; // Replace with the name of the sheet you want to rename var newSheetName = 'New Name'; // Replace with the desired new name for the sheet var sheet = spreadsheet.getSheetByName(sheetName); sheet.setName(newSheetName); }
In this code, the renameSheet
function first retrieves the active spreadsheet using SpreadsheetApp.getActiveSpreadsheet()
. You can modify this line to access a specific spreadsheet if needed.
Next, you need to specify the name of the sheet you want to rename by assigning it to the sheetName
variable. In this example, it’s set to 'Sheet1'
, but you should change it to the actual name of the sheet you want to rename.
You can then assign the desired new name for the sheet to the newSheetName
variable. In this example, it’s set to 'New Name'
, but you can change it to any desired name.
The getSheetByName()
method is used on the spreadsheet
object to retrieve the sheet with the specified sheetName
.
Finally, the setName()
method is called on the sheet
object to set the new name to the specified newSheetName
.
You can call the renameSheet
function from another function or set up a trigger to execute it based on your needs. When executed, it will rename the specified sheet in the Google Sheet to the desired new name.
Feel free to modify the code to suit your specific requirements and work with different sheets in the spreadsheet.