Buttons

Buttons can be added to any of the cells in your spreadsheet, and can be configured to call a Python function when the user clicks on them. That function could export spreadsheet contents, send values from the sheet to a third-party library, or communicate with an external system like a database. Note that it is not generally appropriate to use a button to try and set or update values in cells, as discussed below under 'Updating cells with a button'

How to use buttons in Resolver One

Here's an example of creating a button and displaying it in a cell. This code can be added into any usercode section:

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

Recalculating will now show the button we have created in cell B2, labelled 'Greet':

images/button-in-b2.png

In order to make this button do something, we wire up its Click event handler. New code is marked with '|':

| def onClick():
|    print "hello"

  b = Button("Greet")
| b.Click += onClick
  sheet = workbook["Sheet1"]
  sheet.B2 = b

After a recalculate, clicking on the button will call the function ''onClick()'', which prints a greeting in the output pane:

images/buttons-greeting-in-output-pane.png

Notice that subsequent clicks do not trigger any further recalculations, they simply call ''onClick()'' once each time the button is clicked.

images/button-subsequent-clicks.png

The function ''OnClick()'' can contain more than simple print statements. Generally, buttons are useful for exporting the results from a spreadsheet into another system, such as a database, a file export, producing charts, or passing results into third-party libraries.

Updating cells with a button

It is not recommended to use buttons to update cells in the spreadsheet, since any changes made to cell values will be overwritten at the next recalculate. This is a consequence of the distinction Resolver One makes between the spreadsheet ''model'', and ''results''.

The ''model'' comprises all the constants and formulae typed into cells, together with the usercode displayed in the codebox. The ''results'' are the values that are displayed in cells on the grid, and are derived from the model by evaluating the model formulae and executing the usercode.

Changing a cell's value from within a button click handler will merely overwrite the values in the results, which will be transiently displayed in the grid, but then will get overwritten at the next recalculate, by the values derived from the model.

The general issue of overwriting cell values from usercode is described in more detail at Model vs Results.

Comments