如何禁用 SpreadsheetApp.addMenu 项

How to disable SpreadsheetApp.addMenu item

SpreadsheetApp.AddMenu(name, array of entries) 在传播的顶部栏中生成了一个新菜单sheet。或者,getUI.createMenu 会生成一个项目数组。两者都很好,不知道哪个更好。

是否可以根据活动 sheet 的内容禁用和启用某些菜单项?

有没有办法判断特定 sheet 何时激活?如果没有,我可以使用 onChangeonEdit 事件,因为只有当用户更改为特定 sheet.[=15 时才需要启用我想要禁用的菜单项=]

脚本可以通过调用 getActiveSheet 找到当前处于活动状态的 sheet。但是,当用户从一个 sheet 切换到另一个时,不会触发触发器:这不被视为编辑或更改。执行问题中所述的计划 B:在 edit/change.

时检测活动 sheet

至于两种方法:.addMenu 语法是旧版 Google 表格中唯一可用的语法。添加 getUI().createMenu() 时,保留旧方法以保持兼容性。旧方法的语法更简单,但新方法有更多选项,例如添加分隔符和子菜单。参见 What is the difference between creating a menu with .getUI().createMenu() and .addMenu()?

无法使自定义菜单项显示为禁用。相反,当用户尝试调用当前上下文中不可用的选项时,您可能希望显示一条不太烦人的消息,例如 toast 来通知他们。

感谢您的提问,就我而言,尝试做的是 动态菜单电子表格,它将在视觉上发生变化(用户界面),显示一些菜单项,用户需要在同一个活动电子表格上执行操作。

我是怎么做到的?

  • 我使用这 2 个资源:

Custom Menus in Google Workspace

A document, spreadsheet, presentation, or form can only contain one menu with a given name. If the same script or another script adds a menu with the same name, the new menu replaces the old. Menus cannot be removed while the file is open, although you can write your onOpen() function to skip the menu in the future if a certain property is set.

Properties Service

The Properties service lets you store simple data in key-value pairs scoped to one script, one user of a script, or one document in which an add-on is used. It is typically used to store developer configuration or user preferences

示例代码


/*1.- Create the menu and define its name*/
  
const uiMenu = SpreadsheetApp.getUi().createMenu('MyMenu');
  
/*2.- Define a variable and store in propertyService*/
 
  const properties = PropertiesService.getDocumentProperties();
  const isLoad = properties.getProperty('load');

  console.log(typeof Boolean(isLoad));
  console.log('global', isLoad);

/* 3.- At the moment the spreadsheet is open this function is going to execute */

function onOpen(){
  console.log('onOpen', isLoad);
  if(Boolean(isLoad)){
       uiMenu
      .addItem('Send', 'send')
      .addItem('End', 'end')
  } else {
      uiMenu
      .addItem('Start', 'start')
      .addItem('Load', 'load')
  }
  uiMenu.addToUi();   
}

function start(){
  SpreadsheetApp.getUi().alert('You clicked the first menu item!');
  SpreadsheetApp.getActiveSpreadsheet().toast('Started');
}

function load(){
  SpreadsheetApp.getUi().alert('You clicked the Second menu item!');

  //set the variable 'load'
  properties.setProperty('load', true);

  //set the menu with the new items
  uiMenu
    .addItem('Send', 'send')
    .addItem('End', 'end')

  uiMenu.addToUi();

  SpreadsheetApp.getActiveSpreadsheet().toast(`Loading data`);
}


function send() {
  SpreadsheetApp.getUi().alert('You clicked the Third menu item!');

  if(Boolean(isLoad)){
     SpreadsheetApp.getActiveSpreadsheet().toast(`Sending data`)
  }
}

function end(){
  SpreadsheetApp.getActiveSpreadsheet().toast('End the process');
  
  //You can delete or set the variable to start with the process again.
 
  properties.deleteProperty('load');
  
//set the menu with the new items
  uiMenu
      .addItem('Start', 'start')
      .addItem('Load', 'load')
      .addToUi()
}