简体   繁体   English

Excel Office.js - 在 Workbook_Open() 环境中添加按钮

[英]Excel Office.js -add button on Workbook_Open() envent

I'm new on Office.js On my VBA add-in I use Workbook_Open() event that add add dynamically buttons over a cell location that perform function on worksheet.我是 Office.js 的新手在我的 VBA 加载项上,我使用 Workbook_Open() 事件,在单元格位置上动态添加按钮,在工作表上执行功能。 Did google search and get no tangible results for office.js.谷歌搜索并没有得到 office.js 的具体结果。 Question is it possible to add button over a cells and attach function with Office.js.问题是否可以在单元格上添加按钮并使用 Office.js 附加功能。

With Office.js it is not possible to add buttons on the spreadsheet surface itself.使用 Office.js,无法在电子表格表面本身上添加按钮。 However, you can add buttons dynamically onto a task pane with HTML.但是,您可以使用 HTML 将按钮动态添加到任务窗格中。 Or you can declare buttons statically on the ribbon and contextual (right-click) menus.或者,您可以在功能区和上下文(右键单击)菜单上静态声明按钮。

Both of these solutions are described in the documentation on Office Add-in UI Elements: http://dev.office.com/docs/add-ins/design/ui-elements/ui-elements这两种解决方案都在 Office 插件 UI 元素的文档中进行了描述: http : //dev.office.com/docs/add-ins/design/ui-elements/ui-elements

While you cannot add buttons to a worksheet, you can format cells to look like buttons and make them run and Office-JS function when they are clicked.虽然您无法向工作表添加按钮,但您可以将单元格格式化为按钮,并在单击它们时使它们运行和 Office-JS 功能。 Here's what they look like这是它们的样子

使用 Office-JS 格式化为按钮的单元格

Here's the code that builds them and makes them work这是构建它们并使它们工作的代码

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
  }
}

This code is available at the following Gist:此代码可在以下 Gist 中找到:

Link to Gist with code 使用代码链接到 Gist

You can run and modify this code using the JavaScript Automation Development Environment (JADE) add-in for Excel.您可以使用 Excel 的 JavaScript 自动化开发环境 (JADE) 加载项运行和修改此代码。 Just search for JADE in the add-store.只需在 add-store 中搜索 JADE。 Once installed, click "Import a Code Module" and paste this Gist ID: 055f3811ab7d0240a92df54523d493a9安装后,单击“导入代码模块”并粘贴此 Gist ID:055f3811ab7d0240a92df54523d493a9

Disclaimer: I wrote the JADE add-in免责声明:我编写了 JADE 插件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM