Buttons

Add buttons to your worksheets to call user code functions.

The function could export spreadsheet contents, send values from the sheet to a third-party library, communicate with an external system like a database, or even update the contents of the spreadsheet itself.

How to use buttons in Resolver One

It's easiest to demonstrate this feature with example code:

  1. Add the following code to any usercode section:

    b = Button("Greet")
    sheet = workbook["Sheet1"]
    sheet.B2 = b
    
  2. Recalculate.

    A button labelled "Greet" appears in cell B2:

    images/button-in-b2.png
  3. Add an event handler to make the button perform an action when clicked (new code is marked with '|'):

    | def onClick():
    |    print "hello"
    
      b = Button("Greet")
    | b.Click += onClick
      sheet = workbook["Sheet1"]
      sheet.B2 = b
    
  4. Recalculate then click the button.

    Clicking calls the function ''onClick()'', which prints a greeting in the output pane:

    images/buttons-greeting-in-output-pane.png
  5. Click the button a couple of times more.

    Subsequent clicks do not trigger recalculations; they simply call ''onClick()'':

    images/button-subsequent-clicks.png

More about button handler functions

Button handler functions like OnClick() can contain more than simple print statements. Typical uses include...

  • Exporting results from a spreadsheet into another system, such as a database.
  • A file export.
  • Producing charts.
  • Passing results into third-party libraries.
  • Updating the contents of other cells by setting their Formula property:
def onClick():
   workbook["Sheet1"].Cells.D3.Formula = 23
   workbook["Sheet1"].Cells.D4.Formula = "=D4 * 2"

Comments