bug 61474, github #81: implement updateNamedRanges for ColumnShifter

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1814258 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2017-11-04 07:42:33 +00:00
parent 607ca594b1
commit 7c88f32408
3 changed files with 22 additions and 15 deletions

View File

@ -43,10 +43,9 @@ public final class XSSFColumnShifter extends ColumnShifter {
/** /**
* Updated named ranges * Updated named ranges
*/ */
@NotImplemented
@Override @Override
public void updateNamedRanges(FormulaShifter formulaShifter) { public void updateNamedRanges(FormulaShifter formulaShifter) {
throw new NotImplementedException("updateNamedRanges"); XSSFRowColShifter.updateNamedRanges(sheet, formulaShifter);
} }
/** /**

View File

@ -18,6 +18,7 @@
package org.apache.poi.xssf.usermodel.helpers; package org.apache.poi.xssf.usermodel.helpers;
import org.apache.poi.ss.formula.*; import org.apache.poi.ss.formula.*;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.helpers.BaseRowColShifter; import org.apache.poi.ss.usermodel.helpers.BaseRowColShifter;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
@ -37,6 +38,25 @@ import java.util.List;
private XSSFRowColShifter() { /*no instances for static classes*/} private XSSFRowColShifter() { /*no instances for static classes*/}
/**
* Updated named ranges
*/
/*package*/ static void updateNamedRanges(Sheet sheet, FormulaShifter formulaShifter) {
Workbook wb = sheet.getWorkbook();
XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create((XSSFWorkbook) wb);
for (Name name : wb.getAllNames()) {
String formula = name.getRefersToFormula();
int sheetIndex = name.getSheetIndex();
final int rowIndex = -1; //don't care, named ranges are not allowed to include structured references
Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.NAMEDRANGE, sheetIndex, rowIndex);
if (formulaShifter.adjustFormula(ptgs, sheetIndex)) {
String shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
name.setRefersToFormula(shiftedFmla);
}
}
}
/*package*/ static void updateHyperlinks(Sheet sheet, FormulaShifter formulaShifter) { /*package*/ static void updateHyperlinks(Sheet sheet, FormulaShifter formulaShifter) {
int sheetIndex = sheet.getWorkbook().getSheetIndex(sheet); int sheetIndex = sheet.getWorkbook().getSheetIndex(sheet);
List<? extends Hyperlink> hyperlinkList = sheet.getHyperlinkList(); List<? extends Hyperlink> hyperlinkList = sheet.getHyperlinkList();

View File

@ -65,19 +65,7 @@ public final class XSSFRowShifter extends RowShifter {
*/ */
@Override @Override
public void updateNamedRanges(FormulaShifter formulaShifter) { public void updateNamedRanges(FormulaShifter formulaShifter) {
Workbook wb = sheet.getWorkbook(); XSSFRowColShifter.updateNamedRanges(sheet, formulaShifter);
XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create((XSSFWorkbook) wb);
for (Name name : wb.getAllNames()) {
String formula = name.getRefersToFormula();
int sheetIndex = name.getSheetIndex();
final int rowIndex = -1; //don't care, named ranges are not allowed to include structured references
Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.NAMEDRANGE, sheetIndex, rowIndex);
if (formulaShifter.adjustFormula(ptgs, sheetIndex)) {
String shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
name.setRefersToFormula(shiftedFmla);
}
}
} }
/** /**