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);
}
}
}
- 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++) {
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;
}
}
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();
}
//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);
}
/**
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);
}
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;
}
}
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;
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);
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;
// Check things parse as expected:
- // Note - Ptgs will only show one sheet, the formula
- // parser stuff looks up the second later
+ // TODO Fix it so that the Pxgs get the second sheet too!
// SUM to one cell over 3 workbooks, relative reference
ptgs = parse(fpb, "SUM(Sheet1:Sheet3!A1)");
assertEquals(2, ptgs.length);
if (wb instanceof HSSFWorkbook) {
- assertEquals(Ref3DPtg.class, ptgs[0].getClass());
- assertEquals("Sheet1!A1", toFormulaString(ptgs[0], fpb));
+ assertEquals(Ref3DPtg.class, ptgs[0].getClass());
+ assertEquals("Sheet1:Sheet3!A1", toFormulaString(ptgs[0], fpb));
} else {
assertEquals(Ref3DPxg.class, ptgs[0].getClass());
assertEquals("Sheet1!A1", toFormulaString(ptgs[0], fpb));
ptgs = parse(fpb, "MAX(Sheet1:Sheet3!A$1)");
assertEquals(2, ptgs.length);
if (wb instanceof HSSFWorkbook) {
- assertEquals(Ref3DPtg.class, ptgs[0].getClass());
- assertEquals("Sheet1!A$1", toFormulaString(ptgs[0], fpb));
+ assertEquals(Ref3DPtg.class, ptgs[0].getClass());
+ assertEquals("Sheet1:Sheet3!A$1", toFormulaString(ptgs[0], fpb));
} else {
assertEquals(Ref3DPxg.class, ptgs[0].getClass());
assertEquals("Sheet1!A$1", toFormulaString(ptgs[0], fpb));
ptgs = parse(fpb, "MIN(Sheet1:Sheet3!$A$1)");
assertEquals(2, ptgs.length);
if (wb instanceof HSSFWorkbook) {
- assertEquals(Ref3DPtg.class, ptgs[0].getClass());
- assertEquals("Sheet1!$A$1", toFormulaString(ptgs[0], fpb));
+ assertEquals(Ref3DPtg.class, ptgs[0].getClass());
+ assertEquals("Sheet1:Sheet3!$A$1", toFormulaString(ptgs[0], fpb));
} else {
assertEquals(Ref3DPxg.class, ptgs[0].getClass());
assertEquals("Sheet1!$A$1", toFormulaString(ptgs[0], fpb));
}
assertEquals(FuncVarPtg.class, ptgs[1].getClass());
assertEquals("MIN", toFormulaString(ptgs[1], fpb));
+
+
+ // Check we can round-trip - try to set a new one to a new cell
+ Cell newF = s1.getRow(0).createCell(10, Cell.CELL_TYPE_FORMULA);
+ newF.setCellFormula("SUM(Sheet2:Sheet3!A1)");
+ assertEquals("SUM(Sheet2:Sheet3!A1)", newF.getCellFormula());
}
}
private static String toFormulaString(Ptg ptg, FormulaParsingWorkbook wb) {
assertEquals("\'Test Sheet\'!A1", formula);
// Now both
- // TODO Implement remaining logic for #55906
cell.setCellFormula("Cash_Flow:\'Test Sheet\'!A1");
formula = cell.getCellFormula();
-// assertEquals("Cash_Flow:\'Test Sheet\'!A1", formula);
+ assertEquals("Cash_Flow:\'Test Sheet\'!A1", formula);
}
/**