You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by ex...@apache.org on 2024/11/07 14:44:23 UTC

(nifi) branch support/nifi-1.x updated: NIFI-13922 Fixed SplitExcel to use the evaluated formula value for cell (#9499)

This is an automated email from the ASF dual-hosted git repository.

exceptionfactory pushed a commit to branch support/nifi-1.x
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/support/nifi-1.x by this push:
     new 5a98b352eb NIFI-13922 Fixed SplitExcel to use the evaluated formula value for cell (#9499)
5a98b352eb is described below

commit 5a98b352eb0e40a1a0896fb3aa4480452a82f7da
Author: dan-s1 <ds...@gmail.com>
AuthorDate: Thu Nov 7 09:44:17 2024 -0500

    NIFI-13922 Fixed SplitExcel to use the evaluated formula value for cell (#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 <ex...@apache.org>
---
 .../org/apache/nifi/processors/poi/SplitExcel.java |   2 +-
 .../apache/nifi/processors/poi/TestSplitExcel.java |  35 +++++++++++++++++++++
 .../src/test/resources/dataWithSharedFormula.xlsx  | Bin 16865 -> 14138 bytes
 3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/main/java/org/apache/nifi/processors/poi/SplitExcel.java b/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/main/java/org/apache/nifi/processors/poi/SplitExcel.java
index 602a1a486c..f3def15120 100644
--- a/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/main/java/org/apache/nifi/processors/poi/SplitExcel.java
+++ b/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/main/java/org/apache/nifi/processors/poi/SplitExcel.java
@@ -142,7 +142,7 @@ public class SplitExcel extends AbstractProcessor {
     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)
diff --git a/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/test/java/org/apache/nifi/processors/poi/TestSplitExcel.java b/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/test/java/org/apache/nifi/processors/poi/TestSplitExcel.java
index 3bf5b8e800..f0d71528f3 100644
--- a/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/test/java/org/apache/nifi/processors/poi/TestSplitExcel.java
+++ b/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/test/java/org/apache/nifi/processors/poi/TestSplitExcel.java
@@ -19,6 +19,11 @@ package org.apache.nifi.processors.poi;
 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;
 
@@ -27,6 +32,9 @@ import java.nio.file.Path;
 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;
@@ -34,6 +42,7 @@ import static org.apache.nifi.flowfile.attributes.FragmentAttributes.FRAGMENT_IN
 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;
@@ -132,5 +141,31 @@ public class TestSplitExcel {
         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));
+                }
+            }
+        }
     }
 }
diff --git a/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/test/resources/dataWithSharedFormula.xlsx b/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/test/resources/dataWithSharedFormula.xlsx
index c399b640cb..056a072374 100644
Binary files a/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/test/resources/dataWithSharedFormula.xlsx and b/nifi-nar-bundles/nifi-poi-bundle/nifi-poi-processors/src/test/resources/dataWithSharedFormula.xlsx differ