From a91a13bb2a7cf3e822f5a883529c45d246d5b7b5 Mon Sep 17 00:00:00 2001
From: Yegor Kozlov
Date: Fri, 18 May 2012 09:09:10 +0000
Subject: [PATCH] Bugzilla 53025: Updatad documentation and example on data
validations
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1340042 13f79535-47bb-0310-9956-ffa450edef68
---
.../content/xdocs/spreadsheet/quick-guide.xml | 378 ++++++++++++++++--
src/documentation/content/xdocs/status.xml | 2 +
.../poi/ss/examples/LinkedDropDownLists.java | 228 +++++++++++
3 files changed, 583 insertions(+), 25 deletions(-)
create mode 100755 src/examples/src/org/apache/poi/ss/examples/LinkedDropDownLists.java
diff --git a/src/documentation/content/xdocs/spreadsheet/quick-guide.xml b/src/documentation/content/xdocs/spreadsheet/quick-guide.xml
index d467468097..2140cda6ea 100644
--- a/src/documentation/content/xdocs/spreadsheet/quick-guide.xml
+++ b/src/documentation/content/xdocs/spreadsheet/quick-guide.xml
@@ -68,10 +68,11 @@
How to set cell comments
How to adjust column width to fit the contents
Hyperlinks
-
Data Validation
+
Data Validations
Embedded Objects
Autofilters
Conditional Formatting
+
Hiding and Un-Hiding Rows
Features
@@ -1486,40 +1487,45 @@ Examples:
Data Validations
-
Check the value a user enters into a cell against one or more predefined value(s).
-
The following code will limit the value the user can enter into cell A1 to one of three integer values, 10, 20 or 30.
+
+ As of version 3.8, POI has slightly different syntax to work with data validations with .xls and .xlsx formats.
+
+
+ hssf.usermodel (binary .xls format)
+
Check the value a user enters into a cell against one or more predefined value(s).
+
The following code will limit the value the user can enter into cell A1 to one of three integer values, 10, 20 or 30.
Drop Down Lists:
This code will do the same but offer the user a drop down list to select a value from.
Messages On Error:
To create a message box that will be shown to the user if the value they enter is invalid.
+
Replace 'Box Title' with the text you wish to display in the message box's title bar
and 'Message Text' with the text of your error message.
Prompts:
@@ -1531,6 +1537,7 @@ Examples:
The text encapsulated in the first parameter passed to the createPromptBox() method will appear emboldened
and as a title to the prompt whilst the second will be displayed as the text of the message.
The createExplicitListConstraint() method can be passed and array of String(s) containing interger, floating point, dates or text values.
+
Further Data Validations:
To obtain a validation that would check the value entered was, for example, an integer between 10 and 100,
use the DVConstraint.createNumericConstraint(int, int, String, String) factory method.
@@ -1550,7 +1557,6 @@ Examples:
the setSuppressDropDownArrow(false) method call will simply be ignored.
Date and time constraints can be created by calling the createDateConstraint(int, String, String, String)
or the createTimeConstraint(int, String, String). Both are very similar to the above and are explained in the javadoc.
-
Creating Data Validations From Spreadsheet Cells.
The contents of specific cells can be used to provide the values for the data validation
and the DVConstraint.createFormulaListConstraint(String) method supports this.
@@ -1587,6 +1593,281 @@ Examples:
and nor will this:
+
+
+ xssf.usermodel (.xlsx format)
+
+Data validations work similarly when you are creating an xml based, SpreadsheetML,
+workbook file; but there are differences. Explicit casts are required, for example,
+in a few places as much of the support for data validations in the xssf stream was
+built into the unifying ss stream, of which more later. Other differences are
+noted with comments in the code.
+
+
+
Check the value the user enters into a cell against one or more predefined value(s).
+
+
+
Drop Down Lists:
+
This code will do the same but offer the user a drop down list to select a value from.
+
+
Note that the call to the setSuppressDropDowmArrow() method can either be simply excluded or replaced with:
+
+
+
Prompts and Error Messages:
+
+These both exactly mirror the hssf.usermodel so please refer to the 'Messages On Error:' and 'Prompts:' sections above.
+
+
+
Further Data Validations:
+
+To obtain a validation that would check the value entered was, for example,
+an integer between 10 and 100, use the XSSFDataValidationHelper(s) createNumericConstraint(int, int, String, String) factory method.
+
+
+
+The values passed to the final two String parameters can be formulas; the '=' symbol is used to denote a formula.
+Thus, the following would create a validation the allows values only if they fall between the results of summing two cell ranges
+
+
+
+It is not possible to create a drop down list if the createNumericConstraint() method is called,
+the setSuppressDropDownArrow(true) method call will simply be ignored.
+
+
+Please check the javadoc for other constraint types as examples for those will not be included here.
+There are, for example, methods defined on the XSSFDataValidationHelper class allowing you to create
+the following types of constraint; date, time, decimal, integer, numeric, formula, text length and custom constraints.
+
+
Creating Data Validations From Spread Sheet Cells:
+
+One other type of constraint not mentioned above is the formula list constraint.
+It allows you to create a validation that takes it value(s) from a range of cells. This code
+
+
+
+
+would create a validation that took it's values from cells in the range A1 to F1.
+
+
+The usefulness of this technique can be extended if you use named ranges like this;
+
+
+
+
+OpenOffice Calc has slightly different rules with regard to the scope of names.
+Excel supports both Workbook and Sheet scope for a name but Calc does not, it seems only to support Sheet scope for a name.
+Thus it is often best to fully qualify the name for the region or area something like this;
+
+
+
+This does open a further, interesting opportunity however and that is to place all of the data for the validation(s) into named ranges of cells on a hidden sheet within the workbook. These ranges can then be explicitly identified in the setRefersToFormula() method argument.
+
+
+ ss.usermodel
+
+The classes within the ss.usermodel package allow developers to create code that can be used
+to generate both binary (.xls) and SpreadsheetML (.xlsx) workbooks.
+
+
+The techniques used to create data validations share much in common with the xssf.usermodel examples above.
+As a result just one or two examples will be presented here.
+
+
Check the value the user enters into a cell against one or more predefined value(s).
+
+
+
Drop Down Lists:
+
+
This code will do the same but offer the user a drop down list to select a value from.
+
+
+
+
Prompts and Error Messages:
+
+These both exactly mirror the hssf.usermodel so please refer to the 'Messages On Error:' and 'Prompts:' sections above.
+
+
+As the differences between the ss.usermodel and xssf.usermodel examples are small -
+restricted largely to the way the DataValidationHelper is obtained, the lack of any
+need to explicitly cast data types and the small difference in behaviour between
+the hssf and xssf interpretation of the setSuppressDropDowmArrow() method,
+no further examples will be included in this section.
+
+
Advanced Data Validations.
+
Dependent Drop Down Lists.
+
+In some cases, it may be necessary to present to the user a sheet which contains more than one drop down list.
+Further, the choice the user makes in one drop down list may affect the options that are presented to them in
+the second or subsequent drop down lists. One technique that may be used to implement this behaviour will now be explained.
+
+
+There are two keys to the technique; one is to use named areas or regions of cells to hold the data for the drop down lists,
+the second is to use the INDIRECT() function to convert between the name and the actual addresses of the cells.
+In the example section there is a complete working example- called LinkedDropDownLists.java -
+that demonstrates how to create linked or dependent drop down lists. Only the more relevant points are explained here.
+
+
+To create two drop down lists where the options shown in the second depend upon the selection made in the first,
+begin by creating a named region of cells to hold all of the data for populating the first drop down list.
+Next, create a data validation that will look to this named area for its data, something like this;
+
+
+
+Note that the name of the area - in the example above it is 'CHOICES' -
+is simply passed to the createFormulaListConstraint() method. This is sufficient
+to cause Excel to populate the drop down list with data from that named region.
+
+
+Next, for each of the options the user could select in the first drop down list,
+create a matching named region of cells. The name of that region should match the
+text the user could select in the first drop down list. Note, in the example,
+all upper case letters are used in the names of the regions of cells.
+
+
+
+Now, very similar code can be used to create a second, linked, drop down list;
+
+
+
+
+
+The key here is in the following Excel function - INDIRECT(UPPER($A$1)) - which is used to populate the second,
+linked, drop down list. Working from the inner-most pair of brackets, it instructs Excel to look
+at the contents of cell A1, to convert what it reads there into upper case â as upper case letters are used
+in the names of each region - and then convert this name into the addresses of those cells that contain
+the data to populate another drop down list.
+
+ Using Excel, it is possible to hide a row on a worksheet by selecting that row (or rows),
+ right clicking once on the right hand mouse button and selecting 'Hide' from the pop=up menu that appears.
+
+
+ To emulate this using POI, simply call the setZeroHeight() method on an instance of either
+ XSSFRow or HSSFRow (the method is defined on the ss.usermodel.Row interface that both classes implement), like this:
+
+
+
+ If the file were saved away to disc now, then the first row on the first sheet would not be visible.
+
+
+ Using Excel, it is possible to unhide previously hidden rows by selecting the row above and the row below
+ the one that is hidden and then pressing and holding down the Ctrl key, the Shift and the pressing
+ the number 9 before releasing them all.
+
+
+ To emulate this behaviour using POI do something like this:
+
+
+
+ If the file were saved away to disc now, any previously hidden rows on the first sheet of the workbook would now be visible.
+
+
+ The example illustrates two features. Firstly, that it is possible to unhide a row simply by calling the setZeroHeight()
+ method and passing the boolean value 'false'. Secondly, it ilustrates how to test whther a row is hidden or not.
+ Simply call the getZeroHeight() method and it will return 'true' if the row is hidden, 'false' otherwise.
+