Skip to content

Instantly share code, notes, and snippets.

@theGove
Created January 1, 2022 11:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save theGove/055f3811ab7d0240a92df54523d493a9 to your computer and use it in GitHub Desktop.
Save theGove/055f3811ab7d0240a92df54523d493a9 to your computer and use it in GitHub Desktop.
Format Cells to behave like Buttons in Excel uising Office-js
function build_buttons(){
/*Jade.listing:{"name":"Build Buttons","description":"Format cells on the active sheet to appear as 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
}
}
function auto_exec(){
Jade.open_editor() // shows the code editor
}
@theGove
Copy link
Author

theGove commented Jan 1, 2022

This code is designed to work with the JavaScript Automation Development Environment (JADE) add-in for Microsoft Excel Install the add-in from the add-in store then select "Import a Code Module" and enter the id of this Gist: 055f3811ab7d0240a92df54523d493a9 Then click the "Run" button. More information at https://support.jsvba.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment