From 236710df4e606aeff0f4370907f8b4bcb6fdc85c Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 17 Oct 2023 08:36:07 +0000 Subject: [PATCH] [bug-67778] try to make formula eval involving external workbooks more robust git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1913045 13f79535-47bb-0310-9956-ffa450edef68 --- .../usermodel/BaseXSSFFormulaEvaluator.java | 58 +++++++++++-------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java index fe4e41330a..8f8e00087d 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/BaseXSSFFormulaEvaluator.java @@ -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); } } - } } } -- 2.39.5