Excel Office.js - 在 Workbook_Open() 事件上添加按钮
Excel Office.js -add button on Workbook_Open() envent
我是 Office.js 的新手
在我的 VBA 加载项中,我使用 Workbook_Open() 事件在单元格位置上动态添加按钮,在工作表上执行功能。 google 是否搜索过 office.js,但没有得到任何具体结果。
题
是否可以在单元格上添加按钮并使用 Office.js.
附加功能
使用 Office.js 无法在电子表格表面本身添加按钮。但是,您可以使用 HTML 将按钮动态添加到任务窗格中。或者您可以在功能区和上下文(右键单击)菜单上静态声明按钮。
Office 加载项文档中描述了这两种解决方案 UI 元素:http://dev.office.com/docs/add-ins/design/ui-elements/ui-elements
虽然您不能向工作表添加按钮,但您可以将单元格设置为看起来像按钮的格式,并使它们 运行 和 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 获得:
您可以 运行 并使用 Excel 的 JavaScript 自动化开发环境 (JADE) 插件修改此代码。只需在添加商店中搜索 JADE。安装后,单击“导入代码模块”并粘贴此 Gist ID:055f3811ab7d0240a92df54523d493a9
免责声明:我编写了 JADE 插件
我是 Office.js 的新手 在我的 VBA 加载项中,我使用 Workbook_Open() 事件在单元格位置上动态添加按钮,在工作表上执行功能。 google 是否搜索过 office.js,但没有得到任何具体结果。 题 是否可以在单元格上添加按钮并使用 Office.js.
附加功能使用 Office.js 无法在电子表格表面本身添加按钮。但是,您可以使用 HTML 将按钮动态添加到任务窗格中。或者您可以在功能区和上下文(右键单击)菜单上静态声明按钮。
Office 加载项文档中描述了这两种解决方案 UI 元素:http://dev.office.com/docs/add-ins/design/ui-elements/ui-elements
虽然您不能向工作表添加按钮,但您可以将单元格设置为看起来像按钮的格式,并使它们 运行 和 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 获得:
您可以 运行 并使用 Excel 的 JavaScript 自动化开发环境 (JADE) 插件修改此代码。只需在添加商店中搜索 JADE。安装后,单击“导入代码模块”并粘贴此 Gist ID:055f3811ab7d0240a92df54523d493a9
免责声明:我编写了 JADE 插件