繁体   English   中英

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

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

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

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

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

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

使用 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 中找到:

使用代码链接到 Gist

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

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

暂无
暂无

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

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