2

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

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

代码

      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);

结果

失败

我需要类似的东西(隐藏 A1、B1、B2、B3:当焦点 A|B 时显示):

    ____
    A
        A1
    ____
    B
        B1
        B2
        B3

这是我的传播:

失败2

请帮忙!!

4

3 回答 3

1

我做到了,但是由于参数“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;
    };
    

菜单电子表格

菜单1

submenu电子表格

在此处输入图像描述

于 2012-11-15T08:50:42.023 回答
1

嗯,正如 Serge 指出的那样。答案是错误的。同时我发现,真正的原因是:具有子菜单的 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);
于 2012-11-14T21:54:40.783 回答
0

对不起,你不能拥有它(还):http ://code.google.com/p/google-apps-script-issues/issues/detail?id=317

于 2012-11-14T20:59:52.803 回答