Hey everyone, I’m trying to figure out how to use Apps Script to change my Google Sheets filename based on what’s in a specific cell. Right now my spreadsheet is called ‘KIT MAKING MACRO’ but I want it to automatically update to whatever is in cell O1. For example, if O1 contains ‘A90241 - Batch 3’, that should become the new filename.
I’m not sure how to write the script for this. Can anyone help me out? I’ve tried looking through the documentation but I’m a bit lost. Any tips or code examples would be super helpful. Thanks in advance!
I’ve actually implemented something similar in my work recently. Here’s a script that should do what you’re looking for:
function updateFileName() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var newName = sheet.getRange('O1').getValue();
if (newName) {
sheet.rename(newName);
}
}
You can set this to run automatically whenever the sheet is opened or edited by adding a trigger in the Apps Script editor. Remember to grant the necessary permissions when the script first runs.
One caveat: if cell O1 is empty, the script won’t change the filename, so consider adding a fallback name or retaining the original.
I faced a similar challenge recently and found a neat solution. Here’s what worked for me:
function autoRenameSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var newName = ss.getRange(‘O1’).getValue();
if (newName && newName.trim() !== ‘’) {
ss.rename(newName);
} else {
Logger.log(‘Cell O1 is empty. Filename not updated.’);
}
}
You can set this to run automatically by creating a trigger. Go to Edit > Current project’s triggers, then add a new trigger to run this function on edit or on open.
One thing to watch out for: if you’re sharing this sheet, make sure everyone has the right permissions, or they might get errors when the script tries to rename the file. Also, keep in mind that frequent renaming might confuse collaborators, so you might want to add some sort of notification system to alert users when the name changes.
hey samuel, I’ve done smthing like this before. try this code:
function renameSheet() {
var ss = SpreadsheetApp.getActive();
var newName = ss.getRange(‘O1’).getValue();
ss.rename(newName);
}
you can set it to run whenever the sheet opens. hope this helps!