Skip to content

Commit

Permalink
NIFI-13922 Fixed SplitExcel to use the evaluated formula value for ce…
Browse files Browse the repository at this point in the history
…ll (#9499)

Corrected the copy configuration in SplitExcel to ensure the evaluated value of formulas are copied and not the actual formula. 

Signed-off-by: David Handermann <[email protected]>
  • Loading branch information
dan-s1 authored Nov 7, 2024
1 parent ca10d7a commit 5a98b35
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public String getDescription() {
private static final List<PropertyDescriptor> DESCRIPTORS;
private static final Set<Relationship> RELATIONSHIPS;
private static final CellCopyPolicy CELL_COPY_POLICY = new CellCopyPolicy.Builder()
.cellFormula(CellCopyPolicy.DEFAULT_COPY_CELL_FORMULA_POLICY)
.cellFormula(false) // NOTE: setting to false allows for copying the evaluated formula value.
.cellStyle(false) // NOTE: setting to false avoids exceeding the maximum number of cell styles (64000) in a .xlsx Workbook.
.cellValue(CellCopyPolicy.DEFAULT_COPY_CELL_VALUE_POLICY)
.condenseRows(CellCopyPolicy.DEFAULT_CONDENSE_ROWS_POLICY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -27,13 +32,17 @@
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.apache.nifi.flowfile.attributes.FragmentAttributes.FRAGMENT_COUNT;
import static org.apache.nifi.flowfile.attributes.FragmentAttributes.FRAGMENT_ID;
import static org.apache.nifi.flowfile.attributes.FragmentAttributes.FRAGMENT_INDEX;
import static org.apache.nifi.flowfile.attributes.FragmentAttributes.SEGMENT_ORIGINAL_FILENAME;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestSplitExcel {
private TestRunner runner;
Expand Down Expand Up @@ -132,5 +141,31 @@ void testDataWithSharedFormula() throws IOException {
runner.assertTransferCount(SplitExcel.REL_SPLIT, 2);
runner.assertTransferCount(SplitExcel.REL_ORIGINAL, 1);
runner.assertTransferCount(SplitExcel.REL_FAILURE, 0);

for (MockFlowFile flowFile : runner.getFlowFilesForRelationship(SplitExcel.REL_SPLIT)) {
try (XSSFWorkbook workbook = new XSSFWorkbook(flowFile.getContentStream())) {
Sheet firstSheet = workbook.sheetIterator().next();

// Start from the second row as the first row has column header names
List<Cell> formulaCells = Stream.iterate(firstSheet.getFirstRowNum() + 1, rowIndex -> rowIndex + 1)
.limit(firstSheet.getLastRowNum())
.map(firstSheet::getRow)
.filter(Objects::nonNull)
.map(row -> row.getCell(7)) // NOTE: The argument is 0 based although the formula column when viewed in Excel is in the 8th column.
.filter(Objects::nonNull)
.collect(Collectors.toList());

for (Cell formulaCell : formulaCells) {
Row row = formulaCell.getRow();
Sheet sheet = row.getSheet();
String messagePrefix = String.format("Cell %s in row %s in sheet %s",
formulaCell.getColumnIndex(), row.getRowNum(), sheet.getSheetName());

// If copy cell formula is set to true the cell types would be FORMULA and the numeric value would be 0.0.
assertEquals(CellType.NUMERIC, formulaCell.getCellType(), String.format("%s did not have the expected NUMERIC cell type", messagePrefix));
assertTrue(formulaCell.getNumericCellValue() > 0.0, String.format("%s did not have expected numeric value greater than 0.0", messagePrefix));
}
}
}
}
}
Binary file not shown.

0 comments on commit 5a98b35

Please sign in to comment.