From 4bfe3c6784e76095d8c113cac7ad3c75be90d992 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 31 Jul 2014 15:07:27 +0000 Subject: [PATCH] Fix bug #56502 - When shifting XSSF rows with formula cells, if the formula can't be parsed, log + leave it unchanged rather than failing git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1614916 13f79535-47bb-0310-9956-ffa450edef68 --- .../usermodel/helpers/XSSFRowShifter.java | 23 ++++++++++---- .../poi/xssf/usermodel/TestXSSFBugs.java | 28 ++++++++++++++++++ test-data/spreadsheet/56502.xlsx | Bin 0 -> 11902 bytes 3 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 test-data/spreadsheet/56502.xlsx diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java index da65972ed..1b5156886 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java @@ -20,6 +20,7 @@ package org.apache.poi.xssf.usermodel.helpers; import java.util.ArrayList; import java.util.List; +import org.apache.poi.ss.formula.FormulaParseException; import org.apache.poi.ss.formula.FormulaParser; import org.apache.poi.ss.formula.FormulaRenderer; import org.apache.poi.ss.formula.FormulaShifter; @@ -30,6 +31,8 @@ import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook; import org.apache.poi.xssf.usermodel.XSSFName; @@ -43,9 +46,10 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTConditionalFormatti import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellFormulaType; /** - * @author Yegor Kozlov + * Helper for shifting rows up or down */ public final class XSSFRowShifter { + private static POILogger logger = POILogFactory.getLogger(XSSFRowShifter.class); private final XSSFSheet sheet; public XSSFRowShifter(XSSFSheet sh) { @@ -194,12 +198,19 @@ public final class XSSFRowShifter { XSSFWorkbook wb = sheet.getWorkbook(); int sheetIndex = wb.getSheetIndex(sheet); XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb); - Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.CELL, sheetIndex); - String shiftedFmla = null; - if (shifter.adjustFormula(ptgs, sheetIndex)) { - shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs); + + try { + Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.CELL, sheetIndex); + String shiftedFmla = null; + if (shifter.adjustFormula(ptgs, sheetIndex)) { + shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs); + } + return shiftedFmla; + } catch (FormulaParseException fpe) { + // Log, but don't change, rather than breaking + logger.log(POILogger.WARN, "Error shifting formula on row ", row.getRowNum(), fpe); + return formula; } - return shiftedFmla; } public void updateConditionalFormatting(FormulaShifter shifter) { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 162ee4aad..f33b34591 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -1787,6 +1787,10 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { checkValue(excel, eval.getStringValue() + ".0"); } + /** + * New hyperlink with no initial cell reference, still need + * to be able to change it + */ @Test public void testBug56527() { XSSFWorkbook wb = new XSSFWorkbook(); @@ -1816,6 +1820,30 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues { assertEquals(5, hyperlink.getLastRow()); assertEquals(3, hyperlink.getLastColumn()); } + + /** + * Shifting rows with a formula that references a + * function in another file + */ + @Test + public void bug56502() throws Exception { + Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56502.xlsx"); + Sheet sheet = wb.getSheetAt(0); + + Cell cFunc = sheet.getRow(3).getCell(0); + assertEquals("[1]!LUCANET(\"Ist\")", cFunc.getCellFormula()); + Cell cRef = sheet.getRow(3).createCell(1); + cRef.setCellFormula("A3"); + + // Shift it down one row + sheet.shiftRows(1, sheet.getLastRowNum(), 1); + + // Check the new formulas: Function won't change, Reference will + cFunc = sheet.getRow(4).getCell(0); + assertEquals("[1]!LUCANET(\"Ist\")", cFunc.getCellFormula()); + cRef = sheet.getRow(4).getCell(1); + assertEquals("A4", cRef.getCellFormula()); + } private void checkValue(XSSFWorkbook excel, String expect) { XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(excel); diff --git a/test-data/spreadsheet/56502.xlsx b/test-data/spreadsheet/56502.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..72bd173337c69b34d77954c7ebabfefbf10bc3a6 GIT binary patch literal 11902 zcmeHtbyQUQ9xvS;(%sS_-3>~2NDLs|ozf^>N(|Bs(hMB~NJ@8yNQZ!cl)TY%@AVvx z_pSHuTleg>_UyG6^ZhX2`h8RNDJ&cw6ao|y6ciK{RCi5K$UA5#C?-TGC|oEc7y~Is z2R91`H)Bn2Ckxl-Y+m+ul(}#)jM-2yknjI<{SQmvqt=jeHwRWL%?V~gWwA38j$|4;cBgdwN{QO9yT)DYf@j!7oMNy*N1am<`HS@zyuq@$5Cjl z7`@llR4cuhZOjQ&OpeK@Xx~~!$jh?=44plTIr}kP&3)N)W zut*;){`yQw0^|DbX-Npfm#I8rspxP|4mDS~`MY4T%H&o9_L{}%ru0&*$Fi37)Q=vE zwO&a~peDVZHVMwuL3?NREF0}DlxCAK{ZRBS{wsb?JQoH_n21C9j@m(i8tm|b7_Z)v zW7A^K3(Iq|%a4chGvXJyI3gvHt|_PtVp8dC5gbF9A$6=u8o90NGnMWXYmOLsc1GJ| z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