Skip to content

Commit

Permalink
jxlsteam#148 optional run var prefix for EachCommand.groupBy and .ord…
Browse files Browse the repository at this point in the history
…erBy

PR jxlsteam#200
  • Loading branch information
SoltauFintel authored Jun 8, 2022
1 parent 9a28968 commit 6b5417a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 17 deletions.
29 changes: 23 additions & 6 deletions jxls-poi/src/test/java/org/jxls/TestWorkbook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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;
}
}

/**
Expand All @@ -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;
}
}

/**
Expand All @@ -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;
}
}

/**
Expand All @@ -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;
}
}

/**
Expand Down
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions jxls-site/src/site/markdown/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)<br/>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)

Expand Down
11 changes: 6 additions & 5 deletions jxls-site/src/site/markdown/reference/each_command.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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}

Expand Down
28 changes: 22 additions & 6 deletions jxls/src/main/java/org/jxls/command/EachCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,10 +24,10 @@
* <li>'varIndex' is name of variable in context that holds current iteration index, 0 based. Use ${varIndex+1} for 1 based.</li>
* <li>'direction' defines expansion by rows (DOWN) or by columns (RIGHT). Default is DOWN.</li>
* <li>'select' holds an expression for filtering collection.</li>
* <li>'groupBy' is the name for grouping.</li>
* <li>'groupBy' is the name for grouping (prepend var+".").</li>
* <li>'groupOrder' defines the grouping order. Case does not matter.
* "ASC" for ascending, "DESC" for descending sort order. Other values or null: no sorting.</li>
* <li>'orderBy' contains the names separated with comma and each with an optional postfix " ASC" (default) or " DESC" for the sort order.</li>
* <li>'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.</li>
* <li>'multisheet' is the name of the sheet names container.</li>
* <li>'cellRefGenerator' defines custom strategy for target cell references.</li>
* </ul>
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> orderByProps = Arrays.asList(orderBy.split(","));
List<String> orderByProps = Arrays.asList(orderBy.split(","))
.stream().map(f -> removeVarPrefix(f.trim())).collect(Collectors.toList());
OrderByComparator<Object> comp = new OrderByComparator<>(orderByProps, util);
Collections.sort((List<Object>) itemsCollection, comp);
}
Expand Down Expand Up @@ -400,4 +405,15 @@ private List<String> extractSheetNameList(Context context) {
}
throw new JxlsException("The sheet names var '" + multisheet + "' must be of type List<String>.");
}

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;
}
}

0 comments on commit 6b5417a

Please sign in to comment.