Skip to content

Commit

Permalink
JxlsHelper removal
Browse files Browse the repository at this point in the history
Use JxlsPoiTemplateFillerBuilder instead! See migration guide.
  • Loading branch information
SoltauFintel committed Dec 27, 2023
1 parent f43aa9c commit b30143c
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.jxls.command.EachCommand;
import org.jxls.common.CellData;
import org.jxls.common.CellRef;
import org.jxls.common.Context;
Expand Down Expand Up @@ -149,8 +150,9 @@ public void writeToCell(Cell cell, Context context, PoiTransformer transformer)
updateCellGeneralInfo(cell);
updateCellContents(cell);
CellStyle targetCellStyle = cellStyle;
if (Boolean.TRUE.equals(context.getRunVar(CellData.IGNORE_SOURCE_CELL_STYLE) )) {
CellStyle dataFormatCellStyle = findCellStyle(evaluationResult, (Map<String, String>) context.getRunVar(CellData.CELL_STYLE_MAP), transformer);
if (Boolean.TRUE.equals(EachCommand.getRunVar(context, CellData.IGNORE_SOURCE_CELL_STYLE) )) {
Map<String, String> cellStyleMap = (Map<String, String>) EachCommand.getRunVar(context, CellData.CELL_STYLE_MAP);
CellStyle dataFormatCellStyle = findCellStyle(evaluationResult, cellStyleMap, transformer);
if (dataFormatCellStyle != null) {
targetCellStyle = dataFormatCellStyle;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public MyMap() {

@Override
public Object get(Object key) {
if ("jx:isFormulaProcessingRequired".equals(key)) return null; // TODO
if (!containsKey(key)) {
throw new IllegalArgumentException("Map does not contain key: " + key);
}
Expand Down
5 changes: 3 additions & 2 deletions jxls/src/main/java/org/jxls/area/XlsArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Set;

import org.jxls.command.Command;
import org.jxls.command.EachCommand;
import org.jxls.common.AreaListener;
import org.jxls.common.AreaRef;
import org.jxls.common.CellData;
Expand Down Expand Up @@ -232,7 +233,7 @@ public Size applyAt(CellRef cellRef, Context context) {
transformStaticCells(cellRef, context, commandsArea);
fireAfterApplyEvent(cellRef, context);
Size finalSize = new Size(cellRange.calculateWidth(), cellRange.calculateHeight());
if (!Boolean.FALSE.equals(context.getRunVar(IS_FORMULA_PROCESSING_REQUIRED))) {
if (!Boolean.FALSE.equals(EachCommand.getRunVar(context, IS_FORMULA_PROCESSING_REQUIRED))) {
AreaRef newAreaRef = new AreaRef(cellRef, finalSize);
updateCellDataFinalAreaForFormulaCells(newAreaRef);
}
Expand Down Expand Up @@ -525,7 +526,7 @@ private void transformStaticCells(CellRef cellRef, Context context,
}

private void updateCellDataArea(CellRef srcCell, CellRef targetCell, Context context) {
if (Boolean.FALSE.equals(context.getRunVar(IS_FORMULA_PROCESSING_REQUIRED))) {
if (Boolean.FALSE.equals(EachCommand.getRunVar(context, IS_FORMULA_PROCESSING_REQUIRED))) {
return;
}
CellData cellData = transformer.getCellData(srcCell);
Expand Down
4 changes: 2 additions & 2 deletions jxls/src/main/java/org/jxls/command/CollectionProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public abstract class CollectionProcessor {

public void traverse(Context context, Iterable<?> itemsCollection, String varName, String varIndex,
ExpressionEvaluator selectEvaluator) {
Object currentVarObject = varName == null ? null : context.getRunVar(varName);
Object currentVarIndexObject = varIndex == null ? null : context.getRunVar(varIndex);
Object currentVarObject = EachCommand.getRunVar(context, varName);
Object currentVarIndexObject = EachCommand.getRunVar(context, varIndex);
int currentIndex = 0;
for (Object obj : itemsCollection) {
context.putVar(varName, obj);
Expand Down
17 changes: 13 additions & 4 deletions jxls/src/main/java/org/jxls/command/EachCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,8 @@ private void orderCollection(Iterable<?> itemsCollection) {
private Iterable<?> filter(Context context, Iterable<?> itemsCollection, String selectExpression) {
List<Object> filteredList = new ArrayList<>();
ExpressionEvaluator selectEvaluator = getTransformationConfig().getExpressionEvaluator(selectExpression);
Object currentVarObject = var == null ? null : context.getRunVar(var);
Object currentVarIndexObject = varIndex == null ? null : context.getRunVar(varIndex);
Object currentVarObject = getRunVar(context, var);
Object currentVarIndexObject = getRunVar(context, varIndex);
int currentIndex = 0;
for (Object obj : itemsCollection) {
context.putVar(var, obj);
Expand Down Expand Up @@ -373,8 +373,8 @@ private Size processCollection(Context context, Iterable<?> itemsCollection, Cel
}

CellRef currentCell = cellRef;
Object currentVarObject = varName == null ? null : context.getRunVar(varName);
Object currentVarIndexObject = varIndex == null ? null : context.getRunVar(varIndex);
Object currentVarObject = getRunVar(context, varName);
Object currentVarIndexObject = getRunVar(context, varIndex);
int currentIndex = 0;
for (Object obj : itemsCollection) {
context.putVar(varName, obj);
Expand Down Expand Up @@ -414,6 +414,15 @@ private Size processCollection(Context context, Iterable<?> itemsCollection, Cel
restoreVarObject(context, varName, currentVarObject);
return new Size(newWidth, newHeight);
}

public static Object getRunVar(Context context, String varName) {
if (varName != null) {
if (context.toMap().containsKey(varName)) {
return context.getRunVar(varName);
}
}
return null;
}

private void restoreVarObject(Context context, String varName, Object varObject) {
if (varName == null) {
Expand Down
4 changes: 2 additions & 2 deletions jxls/src/main/java/org/jxls/command/GridCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ private Size processBody(final CellRef cellRef, Context context) {
CellRef currentCell = cellRef;
int totalWidth = 0;
int totalHeight = 0;
Boolean oldIgnoreSourceCellStyle = (Boolean) context.getRunVar(CellData.IGNORE_SOURCE_CELL_STYLE);
Boolean oldIgnoreSourceCellStyle = (Boolean) EachCommand.getRunVar(context, CellData.IGNORE_SOURCE_CELL_STYLE);
context.putVar(CellData.IGNORE_SOURCE_CELL_STYLE, Boolean.TRUE);
Map<String, String> oldStyleCellMap = (Map<String, String>) context.getRunVar(CellData.CELL_STYLE_MAP);
Map<String, String> oldStyleCellMap = (Map<String, String>) EachCommand.getRunVar(context, CellData.CELL_STYLE_MAP);
context.putVar(CellData.CELL_STYLE_MAP, this.cellStyleMap);
// TODO possible error: content of DATA_VAR is not saved & restored
for (Object rowObject : dataCollection) {
Expand Down
3 changes: 2 additions & 1 deletion jxls/src/main/java/org/jxls/functions/GroupSum.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.jxls.command.EachCommand;
import org.jxls.common.Context;
import org.jxls.common.JxlsException;
import org.jxls.expression.ExpressionEvaluator;
Expand Down Expand Up @@ -86,7 +87,7 @@ public T sum(String fieldName, Iterable<Object> collection, String filter) {
}
ExpressionEvaluator expressionEvaluator = transformationConfig.getExpressionEvaluator();
Summarizer<T> sum = sumBuilder.build();
Object oldValue = context.getRunVar(objectVarName);
Object oldValue = EachCommand.getRunVar(context, objectVarName);
for (Object i : collection) {
Object value = getValue(i, fieldName);
context.putVar(objectVarName, i);
Expand Down
12 changes: 0 additions & 12 deletions jxls/src/test/java/org/jxls/common/ContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
Expand Down Expand Up @@ -45,15 +44,4 @@ public void testToString() {
assertTrue(context.toString().contains("y=Abc"));
assertTrue(context.toString().contains("x=1"));
}

@Test
public void testCreateFromMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("x", "Abc");
map.put("y", Integer.valueOf(10));
Context context = new Context(map);
map.clear();
assertEquals("Abc", context.getVar("x"));
assertEquals(Integer.valueOf(10), context.getVar("y"));
}
}

0 comments on commit b30143c

Please sign in to comment.