Resolver Hacks contains information and code for doing fun and useful things with Resolver One. This site is created and maintained by Michael Foord, not by Resolver Systems. Please read the disclaimer before using any of the code on this site.
A Button on the Grid
Adding buttons to a spreadsheet is a useful way of building functionality into them. For example, a button could be used to write back to a database or save a file.
Buttons are easy to use, you create them in a similar way to the 'Windows Forms' button, and then assign them to a cell's value. All you need is a function to be called when the button is pressed (the click handler) and the text to display in the button.
sheet = workbook['Sheet1']
sheet.A1.Value = random()
def Recalculate():
try:
import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import SendKeys
SendKeys.Send('{F9}')
except Exception,e:
print e
button = Button(Text="Recalc")
button.Click += Recalculate
sheet.B1 = button
If you enter the code above as user code (any user code section) then pressing the button ought to cause a recalculation. The value in A1 is a random number, so it will change with every recalculation. If the A1 doesn't change, check the output view to see if an error message is being printed.
Buttons don't have the ability to enter new data into the grid. They do have access to the workbook object, but changes made to the workbook from user code aren't permanent, they will be overwritten by the next recalculation.
This code causes a recalculate by using SendKeys to send an 'F9'. This of course is a hack (and not completely reliable [1]), but still fun.
| [1] | In the Resolver test framework we have discovered that SendKeys is about 99% reliable at the most. Unfortunately we use it hundreds (possibly thousands) of times in the course of a full test run - guaranteeing that it will fail several times. We have a workaround in place to check that it does its job, but it isn't pretty. |
Last edited Mon Dec 10 18:40:02 2007.

IronPython in Action