【问题标题】:Excel Office.js -add button on Workbook_Open() enventExcel Office.js - Workbook_Open() 事件上的添加按钮
【发布时间】:2016-08-15 21:57:47
【问题描述】:

我是 Office.js 的新手 在我的 VBA 加载项中,我使用 Workbook_Open() 事件,该事件在单元格位置上添加动态添加按钮,这些按钮在工作表上执行功能。谷歌搜索并没有得到 office.js 的切实结果。 问题 是否可以在单元格上添加按钮并使用 Office.js 附加功能。

【问题讨论】:

    标签: excel office-js


    【解决方案1】:

    使用 Office.js,无法在电子表格表面本身上添加按钮。但是,您可以使用 HTML 将按钮动态添加到任务窗格中。或者,您可以在功能区和上下文(右键单击)菜单上静态声明按钮。

    Office 插件 UI 元素的文档中描述了这两种解决方案:http://dev.office.com/docs/add-ins/design/ui-elements/ui-elements

    【讨论】:

      【解决方案2】:

      虽然您无法向工作表添加按钮,但您可以将单元格设置为看起来像按钮的格式,并使它们在单击时运行和 Office-JS 功能。这是他们的样子

      这是构建它们并使它们工作的代码

      function build_buttons(){
        // format cells on the active sheet to look and act like buttons
        Excel.run(async function (context) {    
          const sheet = context.workbook.worksheets.getActiveWorksheet()
      
          // set column sizes for cells to look like buttons
          sheet.getRange("A1").format.columnWidth=10
          sheet.getRange("B1").format.columnWidth=50
          sheet.getRange("C1").format.columnWidth=10
          sheet.getRange("D1").format.columnWidth=50
          sheet.getRange("E1").format.columnWidth=10
          
          // set background color
           sheet.getRange("A1:E3").format.fill.color="khaki"
          
          // make cells look and act like buttons
          format_cell_as_button(sheet, "B2", "Button 1")
          format_cell_as_button(sheet, "D2", "Button 2")
          
          // set up sheet to respont to clicks
          sheet.onSingleClicked.add(click_handler)
          context.sync()
            
        })
      }
      
      function click_handler(event){
        // This function gets called every time a click happens 
        // on the sheet.  It decides which function to call based
        // on which cell received the click event
        
        switch(event.address){
          case "B2":
            button_1_click()
            break
          case "D2":
            button_2_click()
            break
          default:    
        }
      }
      
      function button_1_click(){
        // function that gets called when "B2" gets the click event
        Excel.run(async function (context) {    
          const sheet = context.workbook.worksheets.getActiveWorksheet()
          sheet.getRange("b4").values="Button 1 clicked"
          context.sync()
        })
      }
      
      function button_2_click(){
        // function that gets called when "D2" gets the click event
        Excel.run(async function (context) {    
          const sheet = context.workbook.worksheets.getActiveWorksheet()
          sheet.getRange("b4").values="Button 2 clicked"
          context.sync()
        })
      }
      
      
      
      function format_cell_as_button(sheet, cell_address, text){
        // configure a cell to look like a button
        // "sheet" must be a reference to a worksheet that has
        // context.sync() called on it after this function is
        // run.
        
        sheet.getRange(cell_address).format.horizontalAlignment = "Center"
        sheet.getRange(cell_address).values=text
        sheet.getRange(cell_address).format.fill.color="lightgrey"
        format_border(sheet.getRange(cell_address).format.borders, 
                     ["EdgeBottom","EdgeRight"],"Continuous","darkGrey","thick")
        format_border(sheet.getRange(cell_address).format.borders, 
                     ["EdgeTop","EdgeLeft"],"Continuous","whiteSmoke","thick")
      }
      
      function format_border(border_object, border_names, style, color, weight ){
        // border_object must have context.sync() called on it after
        // this function is run.
        for(const border of border_names){
          border_object.getItem(border).style=style
          border_object.getItem(border).color=color
          border_object.getItem(border).weight=weight
        }
      }
      

      此代码可在以下 Gist 中找到:

      Link to Gist with code

      您可以使用 Excel 的 JavaScript 自动化开发环境 (JADE) 插件运行和修改此代码。只需在添加商店中搜索 JADE。安装后,单击“导入代码模块”并粘贴此 Gist ID:055f3811ab7d0240a92df54523d493a9

      免责声明:我编写了 JADE 插件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-09
        相关资源
        最近更新 更多