1

I've been trying to update this code to change the custom menu's name to the current Sheet name.

function onOpen() {
  const prop = PropertiesService.getScriptProperties();
  const sheetName = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName(); 
  SpreadsheetApp.getUi().createMenu(`${sheetName}`).addItem('Run Script', 'main').addToUi();
  prop.setProperty("previousSheet", sheetName); 
}

function onSelectionChange(e) {
  const prop = PropertiesService.getScriptProperties();
  const previousSheet = prop.getProperty("previousSheet");
  const range = e.range;
  const randomValue = Math.floor(Math.random() * 100);
  const sheetName = range.getSheet().getSheetName();
  if (sheetName != previousSheet) {
    range.setValue(`Changed tab from ${previousSheet} to ${sheetName}. ${randomValue}`);
    SpreadsheetApp.getActiveSpreadsheet().removeMenu(`${previousSheet}`);
    onOpen();
  } else return;
  
  prop.setProperty("previousSheet", sheetName);

}

I could never get .removeMenu to work when I use it in onSelectionChange, neither onOpen to add the updated menu again. Not sure if this is even possible to do, maybe someone can help. Thanks.

1 Answer 1

1

Explanation:

Menus created from Class Ui are different from menus created from Class Spreadsheet, so your removeMenu() is failing since it tries to remove a non-existent menu. Thus you need to create your menu in Class Spreadsheet.

Sample Code:

function onOpen() {
  const prop = PropertiesService.getScriptProperties();
  const sheetName = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName(); 
  var menuEntries = [];
  menuEntries.push({name: sheetName, functionName: "main"});
  SpreadsheetApp.getActiveSpreadsheet().addMenu("Sheet Menu",menuEntries);
  prop.setProperty("previousSheet", sheetName); 
}

function onSelectionChange(e) {
  const prop = PropertiesService.getScriptProperties();
  const previousSheet = prop.getProperty("previousSheet");
  const range = e.range;
  const randomValue = Math.floor(Math.random() * 100);
  const sheetName = range.getSheet().getSheetName();
  if (sheetName != previousSheet) {
    range.setValue(`Changed tab from ${previousSheet} to ${sheetName}. ${randomValue}`);
    SpreadsheetApp.getActiveSpreadsheet().removeMenu("Sheet Menu");
    onOpen();
  } else return;
  
  prop.setProperty("previousSheet", sheetName);

}

Sample Output:

enter image description here

References:

Class Spreadsheet

Sign up to request clarification or add additional context in comments.

4 Comments

Very useful! Thanks a lot. This still doesn't update on Sheet switching even though I've used your own code. Any ideas why it might not work? PrintScreen: prnt.sc/1j4x62v
It helps to check your script executions in script.google.com. Maybe somewhere it failed. It always works on my setup.
Using your code again but creating a new Spreadsheet from Incognito solved it. I think it was a bug somewhere. Works now, would've been cool if there was a way to change the menu name as well. Thank you a lot!
The problem with menus created this way is that you can't add submenus to them. The "subMenu" parameter to updateMenu is actually the flat list of menu items; nothing hierarchical.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.