From 1bb695e5e3504f97ab0985b38369222704045f44 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Thu, 26 Feb 2009 09:10:35 +0000 Subject: [PATCH] improved shifting of sheet-level names, also fixed an incorrect Bugzilla number in status.xml git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@748064 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 2 +- src/documentation/content/xdocs/status.xml | 2 +- .../org/apache/poi/hssf/model/Workbook.java | 2 +- .../usermodel/helpers/XSSFRowShifter.java | 2 +- .../ss/usermodel/BaseTestSheetShiftRows.java | 33 +++++++++++++++---- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 7e6538a435..ac90a3b436 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -55,7 +55,7 @@ 46643 - Fixed formula parser to encode range operator with tMemFunc 46647 - Fixed COUNTIF NE operator and other special cases involving type conversion 46635 - Added a method to remove slides - 46520 - Fixed HSSFFont.applyFont() to properly apply font to overlapping regions + 40520 - Fixed HSSFFont.applyFont() to properly apply font to overlapping regions 46545 - Fixed ObjRecord to ignore excessive padding written by previous POI versions 46613 - Fixed evaluator to perform case insensitive string comparisons 46544 - command line interface for hssf ExcelExtractor diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index f47785ddfd..4a5dd69249 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -52,7 +52,7 @@ 46643 - Fixed formula parser to encode range operator with tMemFunc 46647 - Fixed COUNTIF NE operator and other special cases involving type conversion 46635 - Added a method to remove slides - 46520 - Fixed HSSFFont.applyFont() to properly apply font to overlapping regions + 40520 - Fixed HSSFFont.applyFont() to properly apply font to overlapping regions 46545 - Fixed ObjRecord to ignore excessive padding written by previous POI versions 46613 - Fixed evaluator to perform case insensitive string comparisons 46544 - command line interface for hssf ExcelExtractor diff --git a/src/java/org/apache/poi/hssf/model/Workbook.java b/src/java/org/apache/poi/hssf/model/Workbook.java index 5feaa76893..60e33c2f8e 100644 --- a/src/java/org/apache/poi/hssf/model/Workbook.java +++ b/src/java/org/apache/poi/hssf/model/Workbook.java @@ -2320,7 +2320,7 @@ public final class Workbook implements Model { for (int i = 0 ; i < getNumNames() ; ++i){ NameRecord nr = getNameRecord(i); Ptg[] ptgs = nr.getNameDefinition(); - if (shifter.adjustFormula(ptgs, nr.getExternSheetNumber())) { + if (shifter.adjustFormula(ptgs, nr.getSheetNumber())) { nr.setNameDefinition(ptgs); } } 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 95ef50777a..26fdad839c 100755 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java @@ -102,11 +102,11 @@ public class XSSFRowShifter { */ public void updateNamedRanges(FormulaShifter shifter) { XSSFWorkbook wb = sheet.getWorkbook(); - int sheetIndex = wb.getSheetIndex(sheet); XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb); for (int i = 0; i < wb.getNumberOfNames(); i++) { XSSFName name = wb.getNameAt(i); String formula = name.getRefersToFormula(); + int sheetIndex = name.getSheetIndex(); Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.NAMEDRANGE, sheetIndex); if (shifter.adjustFormula(ptgs, sheetIndex)) { diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java index 083779c0b5..f43568771d 100755 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetShiftRows.java @@ -194,25 +194,44 @@ public abstract class BaseTestSheetShiftRows extends TestCase { public final void baseTestShiftWithNames() { Workbook wb = getTestDataProvider().createWorkbook(); - Sheet sheet = wb.createSheet(); - Row row = sheet.createRow(0); + Sheet sheet1 = wb.createSheet("Sheet1"); + Sheet sheet2 = wb.createSheet("Sheet2"); + Row row = sheet1.createRow(0); row.createCell(0).setCellValue(1.1); row.createCell(1).setCellValue(2.2); Name name1 = wb.createName(); name1.setNameName("name1"); - name1.setRefersToFormula("A1+B1"); + name1.setRefersToFormula("Sheet1!$A$1+Sheet1!$B$1"); Name name2 = wb.createName(); name2.setNameName("name2"); - name2.setRefersToFormula("A1"); + name2.setRefersToFormula("Sheet1!$A$1"); - sheet.shiftRows(0, 1, 2); + //refers to A1 but on Sheet2. Should stay unaffected. + Name name3 = wb.createName(); + name3.setNameName("name3"); + name3.setRefersToFormula("Sheet2!$A$1"); + + //The scope of this one is Sheet2. Should stay unaffected. + Name name4 = wb.createName(); + name4.setNameName("name4"); + name4.setRefersToFormula("A1"); + name4.setSheetIndex(1); + + sheet1.shiftRows(0, 1, 2); //shift down the top row on Sheet1. name1 = wb.getNameAt(0); - assertEquals("A3+B3", name1.getRefersToFormula()); + assertEquals("Sheet1!$A$3+Sheet1!$B$3", name1.getRefersToFormula()); name2 = wb.getNameAt(1); - assertEquals("A3", name2.getRefersToFormula()); + assertEquals("Sheet1!$A$3", name2.getRefersToFormula()); + + //name3 and name4 refer to Sheet2 and should not be affected + name3 = wb.getNameAt(2); + assertEquals("Sheet2!$A$1", name3.getRefersToFormula()); + + name4 = wb.getNameAt(3); + assertEquals("A1", name4.getRefersToFormula()); } public final void baseTestShiftWithMergedRegions() { -- 2.39.5