]> source.dussan.org Git - poi.git/commitdiff
[bug-67778] try to make formula eval involving external workbooks more robust
authorPJ Fanning <fanningpj@apache.org>
Tue, 17 Oct 2023 08:36:07 +0000 (08:36 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 17 Oct 2023 08:36:07 +0000 (08:36 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913045 13f79535-47bb-0310-9956-ffa450edef68

poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java

index fe4e41330a07d6a29c255774da6a2661f8f20a25..8f8e00087dcbde118acdfb8da8a8b7c7aaa78ee7 100644 (file)
@@ -111,36 +111,46 @@ public abstract class BaseXSSFFormulaEvaluator extends BaseFormulaEvaluator {
                 Area3DPxg area3DPxg = (Area3DPxg) ptg;
                 if (area3DPxg.getExternalWorkbookNumber() > 0) {
                     EvaluationWorkbook.ExternalSheet externalSheet = getEvaluationWorkbook().getExternalSheet(area3DPxg.getSheetName(), area3DPxg.getLastSheetName(), area3DPxg.getExternalWorkbookNumber());
+                    if (externalSheet != null) {
+                        processEvalCell((XSSFEvaluationCell) evalCell, externalSheet, area3DPxg);
+                    }
+                }
 
-                    XSSFCell xssfCell = ((XSSFEvaluationCell) evalCell).getXSSFCell();
-                    XSSFWorkbook externalWorkbook = (XSSFWorkbook) xssfCell.getSheet().getWorkbook().getCreationHelper().getReferencedWorkbooks().get(externalSheet.getWorkbookName());
-                    ExternalLinksTable externalLinksTable = xssfCell.getSheet().getWorkbook().getExternalLinksTable().get(area3DPxg.getExternalWorkbookNumber() - 1);
+            }
+        }
+    }
 
-                    int firstSheet = externalWorkbook.getSheetIndex(area3DPxg.getSheetName());
-                    int lastSheet = firstSheet;
-                    if (area3DPxg.getLastSheetName() != null) {
-                        lastSheet = externalWorkbook.getSheetIndex(area3DPxg.getLastSheetName());
-                    }
+    private static void processEvalCell(XSSFEvaluationCell evalCell,
+                                        EvaluationWorkbook.ExternalSheet externalSheet, Area3DPxg area3DPxg) {
+        XSSFCell xssfCell = evalCell.getXSSFCell();
+
+        XSSFWorkbook xssfWorkbook = xssfCell.getSheet().getWorkbook();
+        XSSFWorkbook externalWorkbook = (XSSFWorkbook) xssfWorkbook.getCreationHelper()
+                .getReferencedWorkbooks().get(externalSheet.getWorkbookName());
+        ExternalLinksTable externalLinksTable = xssfWorkbook.getExternalLinksTable().get(area3DPxg.getExternalWorkbookNumber() - 1);
 
-                    for (int sheetIndex = firstSheet; sheetIndex <= lastSheet; sheetIndex++) {
-                        XSSFSheet sheet = externalWorkbook.getSheetAt(sheetIndex);
-                        int firstRow = area3DPxg.getFirstRow();
-                        int lastRow = area3DPxg.getLastRow();
-                        for (int rowIndex = firstRow; rowIndex <= lastRow; rowIndex++) {
-                            XSSFRow row = sheet.getRow(rowIndex);
-                            int firstColumn = area3DPxg.getFirstColumn();
-                            int lastColumn = area3DPxg.getLastColumn();
-                            for (int cellIndex = firstColumn; cellIndex <= lastColumn; cellIndex++) {
-                                XSSFCell cell = row.getCell(cellIndex);
-                                String cellValue = cell.getRawValue();
-                                String cellR = new CellReference(cell).formatAsString(false);
-                                externalLinksTable.cacheData(sheet.getSheetName(), (long)rowIndex + 1, cellR, cellValue);
-                            }
-                        }
+        if (externalWorkbook != null && externalLinksTable != null) {
+            int firstSheet = externalWorkbook.getSheetIndex(area3DPxg.getSheetName());
+            int lastSheet = firstSheet;
+            if (area3DPxg.getLastSheetName() != null) {
+                lastSheet = externalWorkbook.getSheetIndex(area3DPxg.getLastSheetName());
+            }
 
+            for (int sheetIndex = firstSheet; sheetIndex <= lastSheet; sheetIndex++) {
+                XSSFSheet sheet = externalWorkbook.getSheetAt(sheetIndex);
+                int firstRow = area3DPxg.getFirstRow();
+                int lastRow = area3DPxg.getLastRow();
+                for (int rowIndex = firstRow; rowIndex <= lastRow; rowIndex++) {
+                    XSSFRow row = sheet.getRow(rowIndex);
+                    int firstColumn = area3DPxg.getFirstColumn();
+                    int lastColumn = area3DPxg.getLastColumn();
+                    for (int cellIndex = firstColumn; cellIndex <= lastColumn; cellIndex++) {
+                        XSSFCell cell = row.getCell(cellIndex);
+                        String cellValue = cell.getRawValue();
+                        String cellR = new CellReference(cell).formatAsString(false);
+                        externalLinksTable.cacheData(sheet.getSheetName(), (long)rowIndex + 1, cellR, cellValue);
                     }
                 }
-
             }
         }
     }