我如何使用谷歌脚本创建SubMenu

时间:2012-11-14 16:56:37

标签: javascript google-apps-script

我需要知道如何创建一个包含2个菜单项的简单菜单( A B ):第一个(A)包含1个项目( A1 ),第二个(B),里面包含3个项目( B1,B2,B3

我可以创建一个示例但总是失败:

CODE

      var handler = app.createServerHandler();
     //Create menu bar
      var menuBar = app.createMenuBar(true);

      var data =  ss.getSheets()[LOG_SHEET_INDEX].getDataRange().getValues();

      var lastRow = ss.getLastRow();
      for(var row = 1; row < lastRow; row++){
        var id_menu =1;
        if(data[row][0]==0){
          var nivel = data[row][1];
          var menuItem = app.createMenuItem(data[row][2], handler);

          var separator = app.createMenuItemSeparator();
          menuBar.addSeparator(separator);
          var subMenu = app.createMenuBar(true).setId("subMenu"+nivel);
          for(var i = 1; i< lastRow; i++){
            if(data[i][0]==nivel){
              var sm = app.getElementById("subMenu"+nivel)
              this[sm.addItem(data[row][2], handler)];
            }

          }menuItem.setSubMenu(sm);

        }menuBar.addItem(data[row][2], app.createMenuBar(true));

      }


      app.add(menuBar);

RESULT

fail

我需要一些东西(隐藏A1,B1,B2,B3:显示聚焦A | B时):

    ____
    A
        A1
    ____
    B
        B1
        B2
        B3

这是我的传播:

fail2

请帮助!!

3 个答案:

答案 0 :(得分:1)

嗯,正如塞尔指出我的上一篇文章。答案是错的。同时我发现了,真正的原因是: 具有子菜单的MenuItem必须没有处理程序:

 var menuBar=app.createMenuBar();
 var subb1=app.createMenuBar(true);
 subb1.addItem (app.createMenuItem("S1", handler1));
 subb1.addItem (app.createMenuItem("S2", handler2));
 var mi1=app.createMenuItem("X1", null); // passing null and not a handler makes submenu work...
 mi1.setSubMenu(subb1);
 menuBar.addItem(mi1);
 app.add(menuBar);

答案 1 :(得分:1)

我完成了但是我遇到了无法使用的handers问题(serverhandle和clienthandler)因为参数'e'未定义或无效:(也是MenuItemID的问题)

  • 我需要知道哪个按钮被称为“e”并且不知道什么是错误的。

    do get(){
    var app = UiApp.createApplication()
    var menu = menuBar();
    var panel = app.createAbsolutePanel().setId("Panel");
    var horizontalPanel = app.createHorizontalPanel().setId("horizontalPanel");
    var verticalPanelDoc = app.createVerticalPanel().setId("verticalPanelDoc");
    var verticalPanelMenu = app.createVerticalPanel().setId("verticalPanelMenu");
    var scrollPanelDoc = app.createScrollPanel().setId("scrollPanelDoc").setSize('1000', '600');
    
    verticalPanelMenu.add(menu);
    verticalPanelDoc.add(scrollPanelDoc);
    horizontalPanel.add(verticalPanelMenu);
    horizontalPanel.add(verticalPanelDoc);
    
    panel.add(horizontalPanel)
    app.add(panel);
    
    return app;
    }
    
    
    function show(e){
    
      var ss =SpreadsheetApp.openById(TABLA_MENU);
      //**testing 'e' = undefined or void**
      var e_source = e.parameter.source;
      ss.getSheetByName("menu").getRange("B"+6).setValue(e_source);
      ss.getSheetByName("menu").getRange("C"+6).setValue(e);
      //**result in cells :   e_source = undefined   e = {parameter={}}**
    
      var app = UiApp.getActiveApplication();
      var panel = app.getElementById("Panel");
      var verticalPanelDoc = app.getElementById("verticalPanelDoc");
      var horizontalPanel = app.getElementById("horizontalPanel");
      var scrollPanelDoc = app.getElementById("scrollPanelDoc");
      verticalPanelDoc.clear();
      verticalPanelDoc.remove(scrollPanelDoc);
    
      //**this will depend of 'e' **
    
      var panelToShow = app.createCaptionPanel().setTitle("Show").setText("Show");
      //
      var label = app.createLabel().setText("Works!");
      panelToShow.add(label);
      verticalPanelDoc.add(panelToShow);
    
    
      return app;
    };
    
    function menuBar(){
      var ss = SpreadsheetApp.openById(TABLA_MENU);
      var app = UiApp.getActiveApplication();
      var absolutePanel = app.getElementById("absolutePanel");
      var verticalPanelMenu = app.getElementById("verticalPanelMenu");
      var horizontalPanel = app.getElementById("horizontalPanel");
    
      var option_menu = app.getElementById("option_menu");
    
      var arrayMenu = ss.getSheetByName("menu").getDataRange().getValues();
      var arraySubMenu =ss.getSheetByName("submenu").getDataRange().getValues();
      var arrayEnlacesSubMenu =ss.getSheetByName("enlaces").getDataRange().getValues();
      var test1 = arraySubMenu[1][1];//A1
      var test2 = arrayMenu[1];//A
      var test3 = arrayEnlacesSubMenu[1][1]
      // **create menu**
      var menuBar = app.createMenuBar(true).setAnimationEnabled(true);
    
      var handler = app.createServerHandler("show")
      .addCallbackElement(verticalPanelMenu)
      .addCallbackElement(horizontalPanel)
      .addCallbackElement(absolutePanel);
    
      // **add structure to menu**
      for(var k=1; k<arrayMenu.length; k++) {
    
        menuBar.addItem(arrayMenu[k][1], arrayMenu[k][1] = app.createMenuBar(true).setWidth("100%")).setWidth("100%");
    
        for(var j=1; j<arraySubMenu[k].length; j++){
          if(arraySubMenu[k][j]!=''){
            var enlace = arrayEnlacesSubMenu[j][1].toString();
    
            var a = arrayMenu[k][1];
            var b = arraySubMenu[k][j];
            //var c = this[arrayMenu[k][1]].addItem(arraySubMenu[k][j],handler).addSeparator(app.createMenuItemSeparator())
            arrayMenu[k][1].addItem(arraySubMenu[k][j],handler).setId("1"+arraySubMenu[k][j])).addSeparator(app.createMenuItemSeparator());
            //**THIS ID nither set the ID* show in navigator ID = ... class="gwt-MenuItem" id="gwt-uid-18" role="menuitem" aria-hasp....pan="1">Publicaciones 2012</td>
          }
        }
        menuBar.addSeparator(app.createMenuItemSeparator());
      } 
      return menuBar;
    };
    

<强> menuSpreadsheet

menu1

<强> submenuSpreadsheet

enter image description here

答案 2 :(得分:0)

抱歉,你还没有({3}}