diff --git a/jxls-poi/src/test/java/org/jxls/TestWorkbook.java b/jxls-poi/src/test/java/org/jxls/TestWorkbook.java index ac9ab91f..2082a719 100644 --- a/jxls-poi/src/test/java/org/jxls/TestWorkbook.java +++ b/jxls-poi/src/test/java/org/jxls/TestWorkbook.java @@ -51,10 +51,11 @@ public void selectSheet(int index) { * @return String */ public String getCellValueAsString(int row, int column) { - if (sheet.getRow(row - 1).getCell(column - 1) == null) { + try { + return sheet.getRow(row - 1).getCell(column - 1).getStringCellValue(); + } catch (NullPointerException e) { return null; } - return sheet.getRow(row - 1).getCell(column - 1).getStringCellValue(); } /** @@ -64,7 +65,11 @@ public String getCellValueAsString(int row, int column) { * @return RichTextString.toString() */ public String getCellValueAsRichString(int row, int column) { - return sheet.getRow(row - 1).getCell(column - 1).getRichStringCellValue().toString(); + try { + return sheet.getRow(row - 1).getCell(column - 1).getRichStringCellValue().toString(); + } catch (NullPointerException e) { + return null; + } } /** @@ -84,7 +89,11 @@ public int getCellValueAsRichStringNumFormattingRuns(int row, int column) { * @return String */ public String getFormulaString(int row, int column) { - return sheet.getRow(row - 1).getCell(column - 1).getCellFormula(); + try { + return sheet.getRow(row - 1).getCell(column - 1).getCellFormula(); + } catch (NullPointerException e) { + return null; + } } /** @@ -94,7 +103,11 @@ public String getFormulaString(int row, int column) { * @return Double */ public Double getCellValueAsDouble(int row, int column) { - return sheet.getRow(row - 1).getCell(column - 1).getNumericCellValue(); + try { + return sheet.getRow(row - 1).getCell(column - 1).getNumericCellValue(); + } catch (NullPointerException e) { + return null; + } } /** @@ -104,7 +117,11 @@ public Double getCellValueAsDouble(int row, int column) { * @return LocalDateTime */ public LocalDateTime getCellValueAsLocalDateTime(int row, int column) { - return sheet.getRow(row - 1).getCell(column - 1).getLocalDateTimeCellValue(); + try { + return sheet.getRow(row - 1).getCell(column - 1).getLocalDateTimeCellValue(); + } catch (NullPointerException e) { + return null; + } } /** diff --git a/jxls-poi/src/test/resources/org/jxls/templatebasedtests/GroupSumTest_filterCondition.xlsx b/jxls-poi/src/test/resources/org/jxls/templatebasedtests/GroupSumTest_filterCondition.xlsx index d7d67ad1..9f7da4df 100644 Binary files a/jxls-poi/src/test/resources/org/jxls/templatebasedtests/GroupSumTest_filterCondition.xlsx and b/jxls-poi/src/test/resources/org/jxls/templatebasedtests/GroupSumTest_filterCondition.xlsx differ diff --git a/jxls-poi/src/test/resources/org/jxls/templatebasedtests/OrderByTest.xlsx b/jxls-poi/src/test/resources/org/jxls/templatebasedtests/OrderByTest.xlsx index 95265299..b7345047 100644 Binary files a/jxls-poi/src/test/resources/org/jxls/templatebasedtests/OrderByTest.xlsx and b/jxls-poi/src/test/resources/org/jxls/templatebasedtests/OrderByTest.xlsx differ diff --git a/jxls-site/src/site/markdown/changes.md b/jxls-site/src/site/markdown/changes.md index 6bd6b7a1..693657f4 100644 --- a/jxls-site/src/site/markdown/changes.md +++ b/jxls-site/src/site/markdown/changes.md @@ -6,6 +6,7 @@ v2.12.0 * [#164 Update commons-compress](https://github.com/jxlsteam/jxls/issues/164) * [#147 Row height bugfix](https://github.com/jxlsteam/jxls/issues/147), contribution by [jools-uk](https://github.com/jools-uk) * [#153 Issue in Excel Output while using SXSSF Transformer](https://github.com/jxlsteam/jxls/issues/153) +* [#148 Inconsistent use of var prefix in EachCommand](https://github.com/jxlsteam/jxls/issues/148)
You should write var+"." before each property name in orderBy and groupBy. * [#129 GroupSum with condition](https://github.com/jxlsteam/jxls/issues/129) see example: GroupSumTest * [#73 Exceptions not propagated in EachCommand](https://github.com/jxlsteam/jxls/issues/73) diff --git a/jxls-site/src/site/markdown/reference/each_command.md b/jxls-site/src/site/markdown/reference/each_command.md index 90270890..ff82d2e2 100644 --- a/jxls-site/src/site/markdown/reference/each_command.md +++ b/jxls-site/src/site/markdown/reference/each_command.md @@ -22,11 +22,11 @@ Command Attributes * `select` is an expression selector to filter out collection items during the iteration -* `groupBy` is a property to do the grouping +* `groupBy` is a property to do the grouping (prepend the var name + ".") * `groupOrder` indicates ordering for groups ('desc' or 'asc') -* `orderBy` contains the names separated with comma and each with an optional postfix " ASC" (default) or " DESC" for the sort order +* `orderBy` contains the property names separated with comma and each with an optional postfix " ASC" (default) or " DESC" for the sort order. You should prepend the var name + "." before each property name. * `multisheet` is a name of a context variable containing a list of sheet names to output the collection @@ -95,15 +95,16 @@ With Java API you do this like Grouping the data ------------------ *Each-Command* supports grouping via its `groupBy` property. The `groupOrder` property sets the ordering and can be `desc` or `asc`. -If you write `groupBy` without `groupOrder` no sorting will be done. +If you write `groupBy` without `groupOrder` no sorting will be done. Normally one defines `groupBy` and `groupOrder`. In the excel markup it can look like this - jx:each(items="employees" var="myGroup" groupBy="name" groupOrder="asc" lastCell="D6") + jx:each(items="employees" var="myGroup" groupBy="myGroup.name" groupOrder="asc" lastCell="D6") In this example each group can be referred using _myGroup_ variable which will be accessible in the context. +For consistent use you should write the var name + "." before the groupBy property name. -The current group item can be referred using `myGroup.item`. So to refer to employee name use +The current group item can be referred using `myGroup.item`. So to refer to employee name use ${myGroup.item.name} diff --git a/jxls/src/main/java/org/jxls/command/EachCommand.java b/jxls/src/main/java/org/jxls/command/EachCommand.java index c710e263..df068a26 100644 --- a/jxls/src/main/java/org/jxls/command/EachCommand.java +++ b/jxls/src/main/java/org/jxls/command/EachCommand.java @@ -4,6 +4,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import org.jxls.area.Area; import org.jxls.common.CellRef; @@ -23,10 +24,10 @@ *
  • 'varIndex' is name of variable in context that holds current iteration index, 0 based. Use ${varIndex+1} for 1 based.
  • *
  • 'direction' defines expansion by rows (DOWN) or by columns (RIGHT). Default is DOWN.
  • *
  • 'select' holds an expression for filtering collection.
  • - *
  • 'groupBy' is the name for grouping.
  • + *
  • 'groupBy' is the name for grouping (prepend var+".").
  • *
  • 'groupOrder' defines the grouping order. Case does not matter. * "ASC" for ascending, "DESC" for descending sort order. Other values or null: no sorting.
  • - *
  • 'orderBy' contains the names separated with comma and each with an optional postfix " ASC" (default) or " DESC" for the sort order.
  • + *
  • 'orderBy' contains the names separated with comma and each with an optional postfix " ASC" (default) or " DESC" for the sort order. Prepend var+'.' before each name.
  • *
  • 'multisheet' is the name of the sheet names container.
  • *
  • 'cellRefGenerator' defines custom strategy for target cell references.
  • * @@ -205,7 +206,8 @@ public String getGroupBy() { } /** - * @param groupBy property name for grouping the collection + * @param groupBy property name for grouping the collection. + * You should write the run var name + "." before the property name. */ public void setGroupBy(String groupBy) { this.groupBy = groupBy; @@ -226,14 +228,16 @@ public void setGroupOrder(String groupOrder) { } /** - * @param orderBy property name for ordering the list + * @param orderBy property names for ordering the list. + * You should write the run var name + "." before each property name. + * You can write " ASC" or " DESC" after each property name for ascending/descending sorting order. ASC is the default. */ public void setOrderBy(String orderBy) { this.orderBy = orderBy; } /** - * @return property name for ordering the list + * @return property names for ordering the list */ public String getOrderBy() { return orderBy; @@ -307,7 +311,8 @@ public Size applyAt(CellRef cellRef, Context context) { @SuppressWarnings("unchecked") private void orderCollection(Iterable itemsCollection) { if (itemsCollection instanceof List && orderBy != null && !orderBy.trim().isEmpty()) { - List orderByProps = Arrays.asList(orderBy.split(",")); + List orderByProps = Arrays.asList(orderBy.split(",")) + .stream().map(f -> removeVarPrefix(f.trim())).collect(Collectors.toList()); OrderByComparator comp = new OrderByComparator<>(orderByProps, util); Collections.sort((List) itemsCollection, comp); } @@ -400,4 +405,15 @@ private List extractSheetNameList(Context context) { } throw new JxlsException("The sheet names var '" + multisheet + "' must be of type List."); } + + private String removeVarPrefix(String pVariable) { + int o = pVariable.indexOf("."); + if (o >= 0) { + String pre = pVariable.substring(0, o).trim(); + if (pre.equals(var)) { + return pVariable.substring(o + 1).trim(); + } + } + return pVariable; + } }