]> source.dussan.org Git - poi.git/commitdiff
Bug 61648: Test Sheet.setArrayFormula for SXSSF
authorDominik Stadler <centic@apache.org>
Mon, 1 Jan 2018 14:39:15 +0000 (14:39 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 1 Jan 2018 14:39:15 +0000 (14:39 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1819770 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFSheet.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestSXSSFBugs.java

index f8a9de5844107ffe3c1e616a286d8ae93c91487c..4442c87515979e2362289fe6d162e74bfe2a7e3c 100644 (file)
@@ -970,8 +970,7 @@ public class SXSSFSheet implements Sheet
      */
     @NotImplemented
     @Override
-    public void shiftRows(int startRow, int endRow, int n)
-    {
+    public void shiftRows(int startRow, int endRow, int n) {
         throw new RuntimeException("NotImplemented");
     }
 
@@ -1730,9 +1729,12 @@ public class SXSSFSheet implements Sheet
      * @return the {@link CellRange} of cells affected by this change
      */
     @Override
-    public CellRange<? extends Cell> setArrayFormula(String formula, CellRangeAddress range)
-    {
-        return _sh.setArrayFormula(formula, range);
+    public CellRange<? extends Cell> setArrayFormula(String formula, CellRangeAddress range) {
+        // the simple approach via _sh does not work as it creates rows in the XSSFSheet and thus causes
+        // corrupted .xlsx files as rows appear multiple times in the resulting sheetX.xml files
+        // return _sh.setArrayFormula(formula, range);
+
+        throw new RuntimeException("NotImplemented");
     }
 
     /**
@@ -1742,9 +1744,12 @@ public class SXSSFSheet implements Sheet
      * @return the {@link CellRange} of cells affected by this change
      */
     @Override
-    public CellRange<? extends Cell> removeArrayFormula(Cell cell)
-    {
-        return _sh.removeArrayFormula(cell);
+    public CellRange<? extends Cell> removeArrayFormula(Cell cell) {
+        // the simple approach via _sh does not work as it creates rows in the XSSFSheet and thus causes
+        // corrupted .xlsx files as rows appear multiple times in the resulting sheetX.xml files
+        // return _sh.removeArrayFormula(cell);
+
+        throw new RuntimeException("NotImplemented");
     }
     
     @Override
index 5cb6afa77c194181051fc11d7385098334c30469..2d0aec1671d75f984134dd53532209710cc66c8c 100644 (file)
 package org.apache.poi.xssf.usermodel;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
 
 import java.io.IOException;
 
+import org.apache.poi.ss.ITestDataProvider;
 import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.PrintSetup;
+import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.xssf.SXSSFITestDataProvider;
+import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -99,4 +106,64 @@ public final class TestSXSSFBugs extends BaseTestBugzillaIssues {
             }
         }
     }
+
+    @Test
+    public void bug61648() throws Exception {
+        // works as expected
+        writeWorkbook(new XSSFWorkbook(), "/tmp/61648.xlsx", XSSFITestDataProvider.instance);
+
+        // does not work
+        try {
+            writeWorkbook(new SXSSFWorkbook(), "/tmp/61648s.xlsx", SXSSFITestDataProvider.instance);
+            fail("Should catch exception here");
+        } catch (RuntimeException e) {
+            // this is not implemented yet
+        }
+    }
+
+    void writeWorkbook(Workbook wb, String filename, ITestDataProvider testDataProvider) throws IOException {
+        Sheet sheet = wb.createSheet("array formula test");
+
+        int rowIndex = 0;
+        int colIndex = 0;
+        Row row = sheet.createRow(rowIndex++);
+
+        Cell cell = row.createCell(colIndex++);
+        cell.setCellType(CellType.STRING);
+        cell.setCellValue("multiple");
+        cell = row.createCell(colIndex++);
+        cell.setCellType(CellType.STRING);
+        cell.setCellValue("unique");
+
+        writeRow(sheet, rowIndex++, 80d, "INDEX(A2:A7, MATCH(FALSE, ISBLANK(A2:A7), 0))");
+        writeRow(sheet, rowIndex++, 30d, "IFERROR(INDEX(A2:A7, MATCH(1, (COUNTIF(B2:B2, A2:A7) = 0) * (NOT(ISBLANK(A2:A7))), 0)), \"\")");
+        writeRow(sheet, rowIndex++, 30d, "IFERROR(INDEX(A2:A7, MATCH(1, (COUNTIF(B2:B3, A2:A7) = 0) * (NOT(ISBLANK(A2:A7))), 0)), \"\")");
+        writeRow(sheet, rowIndex++, 2d,  "IFERROR(INDEX(A2:A7, MATCH(1, (COUNTIF(B2:B4, A2:A7) = 0) * (NOT(ISBLANK(A2:A7))), 0)), \"\")");
+        writeRow(sheet, rowIndex++, 30d, "IFERROR(INDEX(A2:A7, MATCH(1, (COUNTIF(B2:B5, A2:A7) = 0) * (NOT(ISBLANK(A2:A7))), 0)), \"\")");
+        writeRow(sheet, rowIndex++, 2d,  "IFERROR(INDEX(A2:A7, MATCH(1, (COUNTIF(B2:B6, A2:A7) = 0) * (NOT(ISBLANK(A2:A7))), 0)), \"\")");
+
+        /*FileOutputStream fileOut = new FileOutputStream(filename);
+        wb.write(fileOut);
+        fileOut.close();*/
+
+        Workbook wbBack = testDataProvider.writeOutAndReadBack(wb);
+        assertNotNull(wbBack);
+        wbBack.close();
+
+        wb.close();
+    }
+
+    void writeRow(Sheet sheet, int rowIndex, Double col0Value, String col1Value) {
+        int colIndex = 0;
+        Row row = sheet.createRow(rowIndex);
+
+        // numeric value cell
+        Cell cell = row.createCell(colIndex++);
+        cell.setCellType(CellType.NUMERIC);
+        cell.setCellValue(col0Value);
+
+        // formula value cell
+        CellRangeAddress range = new CellRangeAddress(rowIndex, rowIndex, colIndex, colIndex);
+        sheet.setArrayFormula(col1Value, range);
+    }
 }