簡體   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