Drop-down cells
This sample, which is saved as Drop-down cells in the Samples subfolder of the Resolver One folder on your start menu, shows how to constrain the allowed input values for a given cell. It's made up of several simple parts:
- The 'cities' worksheet contains a small list of cities and their timezones relative to GMT. The cities column is the sheet's header column, and the row with the labels 'city' and 'time' is the header row.
- The interface worksheet ('Sheet1') has 2 important cells: B2 and B3. B3's formula is '=timezone(B2)'; and, to ensure that we don't pass bad values to timezone, we have set a validation range on B2. In this case, B2's validation range is set to 'cities!$A$2:$A$6'.
- The 'timezone' function, defined in the pre-constants user code. When this function is called (passing in a city name), it uses the city name to look up and return the appropriate value from the 'time' column on the 'cities' worksheet.
def timezone(city):
cities = workbook['cities']
return cities['time', city]
You can edit a cell's validation range from 'Validation...' under the 'Data' menu. While validation ranges will always be stored as absolute references (with dollar signs), it is not necessary to include them yourself: entering 'cities!a2:a6' will have the same effect. It is also possible to validate on the contents of a named range -- just enter that range's name instead of a reference to an explicit cell range.
Drop-downs in User Code
Drop-downs can also be created and manipulated in user code. There are several properties of cells that affect the behaviour of drop-downs: DropdownItems, DropdownIndex and Value. The items that can appear in a drop-down on a cell are specified in DropdownItems:
workbook['cities'].Cells.C4.DropdownItems = ['1', '2', '3']
DropdownItems can be a Python iterable or a CellRange and can contain any kind of object. The text that appears in the drop-down list on the cell is simply the string representation of the value in DropdownItems. After a recalculation, the entries will appear in a drop-down on the cell.
DropdownIndex and Value can be used to set the drop-down selection. Setting Value to an entry that exists in DropdownItems will set the DropdownIndex to the (zero-based) index of that item in the list. Similarly, setting the DropdownIndex will update the Value of the cell to the item at the specified index.
Sheet3 of the sample workbook shows how a simple drop-down can be created from and used in user code.
Back to Sample Spreadsheets.
Comments
If you have comments, questions or suggestions about any of the Resolver One documentation, please post them to the Documentation Suggestions Forum.
