diff options
Diffstat (limited to 'src/java/org/apache')
4 files changed, 71 insertions, 17 deletions
diff --git a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java index 45c1612461..1e88e94b07 100644 --- a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java +++ b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java @@ -1871,17 +1871,31 @@ public final class InternalWorkbook { return linkTable.getLastInternalSheetIndexForExtIndex(externSheetNumber); } - /** returns the extern sheet number for specific sheet number , - * if this sheet doesn't exist in extern sheet , add it - * @param sheetNumber sheet number + /** + * Returns the extern sheet number for specific sheet number. + * If this sheet doesn't exist in extern sheet, add it + * @param sheetNumber local sheet number * @return index to extern sheet */ public short checkExternSheet(int sheetNumber){ return (short)getOrCreateLinkTable().checkExternSheet(sheetNumber); } + /** + * Returns the extern sheet number for specific range of sheets. + * If this sheet range doesn't exist in extern sheet, add it + * @param firstSheetNumber first local sheet number + * @param lastSheetNumber last local sheet number + * @return index to extern sheet + */ + public short checkExternSheet(int firstSheetNumber, int lastSheetNumber){ + return (short)getOrCreateLinkTable().checkExternSheet(firstSheetNumber, lastSheetNumber); + } public int getExternalSheetIndex(String workbookName, String sheetName) { - return getOrCreateLinkTable().getExternalSheetIndex(workbookName, sheetName); + return getOrCreateLinkTable().getExternalSheetIndex(workbookName, sheetName, sheetName); + } + public int getExternalSheetIndex(String workbookName, String firstSheetName, String lastSheetName) { + return getOrCreateLinkTable().getExternalSheetIndex(workbookName, firstSheetName, lastSheetName); } diff --git a/src/java/org/apache/poi/hssf/model/LinkTable.java b/src/java/org/apache/poi/hssf/model/LinkTable.java index a01b287161..7277da2e6c 100644 --- a/src/java/org/apache/poi/hssf/model/LinkTable.java +++ b/src/java/org/apache/poi/hssf/model/LinkTable.java @@ -383,7 +383,7 @@ final class LinkTable { } } - public int getExternalSheetIndex(String workbookName, String sheetName) { + public int getExternalSheetIndex(String workbookName, String firstSheetName, String lastSheetName) { SupBookRecord ebrTarget = null; int externalBookIndex = -1; for (int i=0; i<_externalBookBlocks.length; i++) { @@ -400,12 +400,13 @@ final class LinkTable { if (ebrTarget == null) { throw new RuntimeException("No external workbook with name '" + workbookName + "'"); } - int sheetIndex = getSheetIndex(ebrTarget.getSheetNames(), sheetName); + int firstSheetIndex = getSheetIndex(ebrTarget.getSheetNames(), firstSheetName); + int lastSheetIndex = getSheetIndex(ebrTarget.getSheetNames(), lastSheetName); - int result = _externSheetRecord.getRefIxForSheet(externalBookIndex, sheetIndex); + int result = _externSheetRecord.getRefIxForSheet(externalBookIndex, firstSheetIndex, lastSheetIndex); if (result < 0) { throw new RuntimeException("ExternSheetRecord does not contain combination (" - + externalBookIndex + ", " + sheetIndex + ")"); + + externalBookIndex + ", " + firstSheetIndex + ", " + lastSheetIndex + ")"); } return result; } @@ -454,6 +455,9 @@ final class LinkTable { } public int checkExternSheet(int sheetIndex) { + return checkExternSheet(sheetIndex, sheetIndex); + } + public int checkExternSheet(int firstSheetIndex, int lastSheetIndex) { int thisWbIndex = -1; // this is probably always zero for (int i=0; i<_externalBookBlocks.length; i++) { SupBookRecord ebr = _externalBookBlocks[i].getExternalBookRecord(); @@ -467,12 +471,12 @@ final class LinkTable { } //Trying to find reference to this sheet - int i = _externSheetRecord.getRefIxForSheet(thisWbIndex, sheetIndex); + int i = _externSheetRecord.getRefIxForSheet(thisWbIndex, firstSheetIndex, lastSheetIndex); if (i>=0) { return i; } //We haven't found reference to this sheet - return _externSheetRecord.addRef(thisWbIndex, sheetIndex, sheetIndex); + return _externSheetRecord.addRef(thisWbIndex, firstSheetIndex, lastSheetIndex); } /** @@ -612,7 +616,8 @@ final class LinkTable { int numberOfNames = extBlock.getNumberOfNames(); // a new name is inserted in the end of the SupBookRecord, after the last name _workbookRecordList.add(supLinkIndex + numberOfNames, extNameRecord); - int ix = _externSheetRecord.getRefIxForSheet(extBlockIndex, -2 /* the scope is workbook*/); + int fakeSheetIdx = -2; /* the scope is workbook*/ + int ix = _externSheetRecord.getRefIxForSheet(extBlockIndex, fakeSheetIdx, fakeSheetIdx); return new NameXPtg(ix, nameIndex); } diff --git a/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java b/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java index 846c9c2a6b..636b6b0da0 100644 --- a/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java +++ b/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java @@ -276,14 +276,15 @@ public class ExternSheetRecord extends StandardRecord { return _list.size() - 1; } - public int getRefIxForSheet(int externalBookIndex, int sheetIndex) { + public int getRefIxForSheet(int externalBookIndex, int firstSheetIndex, int lastSheetIndex) { int nItems = _list.size(); for (int i = 0; i < nItems; i++) { RefSubRecord ref = getRef(i); if (ref.getExtBookIndex() != externalBookIndex) { continue; } - if (ref.getFirstSheetIndex() == sheetIndex && ref.getLastSheetIndex() == sheetIndex) { + if (ref.getFirstSheetIndex() == firstSheetIndex && + ref.getLastSheetIndex() == lastSheetIndex) { return i; } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java index 84eb528d55..5136babe2d 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationWorkbook.java @@ -30,7 +30,9 @@ import org.apache.poi.ss.formula.FormulaParseException; import org.apache.poi.ss.formula.FormulaParsingWorkbook; import org.apache.poi.ss.formula.FormulaRenderingWorkbook; import org.apache.poi.ss.formula.FormulaType; +import org.apache.poi.ss.formula.NameIdentifier; import org.apache.poi.ss.formula.SheetIdentifier; +import org.apache.poi.ss.formula.SheetRangeIdentifier; import org.apache.poi.ss.formula.ptg.Area3DPtg; import org.apache.poi.ss.formula.ptg.NamePtg; import org.apache.poi.ss.formula.ptg.NameXPtg; @@ -69,7 +71,25 @@ public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, E public int getExternalSheetIndex(String workbookName, String sheetName) { return _iBook.getExternalSheetIndex(workbookName, sheetName); } - + public int getExternalSheetIndex(String workbookName, String firstSheetName, String lastSheetName) { + return _iBook.getExternalSheetIndex(workbookName, firstSheetName, lastSheetName); + } + + public int getExternalSheetIndex(String workbookName, NameIdentifier sheetName) { + return getExternalSheetIndex(workbookName, sheetName.getName()); + } + public int getExternalSheetIndex(String workbookName, NameIdentifier firstSheetName, NameIdentifier lastSheetName) { + return getExternalSheetIndex(workbookName, firstSheetName.getName(), lastSheetName.getName()); + } + public int getExternalSheetIndex(NameIdentifier sheetName) { + return getExternalSheetIndex(sheetName.getName()); + } + public int getExternalSheetIndex(NameIdentifier firstSheetName, NameIdentifier lastSheetName) { + int firstSheetIndex = _uBook.getSheetIndex(firstSheetName.getName()); + int lastSheetIndex = _uBook.getSheetIndex(lastSheetName.getName()); + return _iBook.checkExternSheet(firstSheetIndex, lastSheetIndex); + } + public Ptg get3DReferencePtg(CellReference cr, SheetIdentifier sheet) { int extIx = getSheetExtIx(sheet); return new Ref3DPtg(cr, extIx); @@ -235,11 +255,25 @@ public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, E if (sheetIden == null) { extIx = -1; } else { - String sName = sheetIden.getSheetIdentifier().getName(); + NameIdentifier sheetName = sheetIden.getSheetIdentifier(); + NameIdentifier lastSheetName = null; + + if (sheetIden instanceof SheetRangeIdentifier) { + lastSheetName = ((SheetRangeIdentifier)sheetIden).getLastSheetIdentifier(); + } + if (sheetIden.getBookName() == null) { - extIx = getExternalSheetIndex(sName); + if (lastSheetName == null) { + extIx = getExternalSheetIndex(sheetName); + } else { + extIx = getExternalSheetIndex(sheetName, lastSheetName); + } } else { - extIx = getExternalSheetIndex(sheetIden.getBookName(), sName); + if (lastSheetName == null) { + extIx = getExternalSheetIndex(sheetIden.getBookName(), sheetName); + } else { + extIx = getExternalSheetIndex(sheetIden.getBookName(), sheetName, lastSheetName); + } } } return extIx; |