Allow to specify SpreadsheetVersion when instantiating blank HSSFWorkbook

This commit is contained in:
Travis Burtrum 2017-05-30 14:54:22 -04:00
parent 9e63166ac3
commit c2cd1b48ca
17 changed files with 82 additions and 57 deletions

View File

@ -70,6 +70,7 @@ import org.apache.poi.hssf.record.aggregates.RecordAggregate.PositionTrackingVis
import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor; import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate; import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate;
import org.apache.poi.hssf.record.aggregates.WorksheetProtectionBlock; import org.apache.poi.hssf.record.aggregates.WorksheetProtectionBlock;
import org.apache.poi.ss.SpreadsheetVersion;
import org.apache.poi.ss.formula.FormulaShifter; import org.apache.poi.ss.formula.FormulaShifter;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.PaneInformation; import org.apache.poi.ss.util.PaneInformation;
@ -444,9 +445,12 @@ public final class InternalSheet {
* @return Sheet object with all values set to defaults * @return Sheet object with all values set to defaults
*/ */
public static InternalSheet createSheet() { public static InternalSheet createSheet() {
return new InternalSheet(); return new InternalSheet(SpreadsheetVersion.EXCEL97);
} }
private InternalSheet() { public static InternalSheet createSheet(final SpreadsheetVersion spreadsheetVersion) {
return new InternalSheet(spreadsheetVersion);
}
private InternalSheet(final SpreadsheetVersion spreadsheetVersion) {
_mergedCellsTable = new MergedCellsTable(); _mergedCellsTable = new MergedCellsTable();
List<RecordBase> records = new ArrayList<RecordBase>(32); List<RecordBase> records = new ArrayList<RecordBase>(32);
@ -487,7 +491,7 @@ public final class InternalSheet {
_columnInfos = columns; _columnInfos = columns;
_dimensions = createDimensions(); _dimensions = createDimensions();
records.add(_dimensions); records.add(_dimensions);
_rowsAggregate = new RowRecordsAggregate(); _rowsAggregate = new RowRecordsAggregate(spreadsheetVersion);
records.add(_rowsAggregate); records.add(_rowsAggregate);
// 'Sheet View Settings' // 'Sheet View Settings'
records.add(windowTwo = createWindowTwo()); records.add(windowTwo = createWindowTwo());

View File

@ -109,7 +109,7 @@ public final class SharedFormulaRecord extends SharedValueRecordBase {
throw new RuntimeException("Shared Formula Conversion: Coding Error"); throw new RuntimeException("Shared Formula Conversion: Coding Error");
} }
SharedFormula sf = new SharedFormula(org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion); SharedFormula sf = new SharedFormula(SpreadsheetVersion.EXCEL97);
return sf.convertSharedFormulas(field_7_parsed_expr.getTokens(), formulaRow, formulaColumn); return sf.convertSharedFormulas(field_7_parsed_expr.getTokens(), formulaRow, formulaColumn);
} }

View File

@ -40,6 +40,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
private final ValueRecordsAggregate _valuesAgg; private final ValueRecordsAggregate _valuesAgg;
private final List<Record> _unknownRecords; private final List<Record> _unknownRecords;
private final SharedValueManager _sharedValueManager; private final SharedValueManager _sharedValueManager;
private final SpreadsheetVersion _spreadsheetVersion;
// Cache values to speed up performance of // Cache values to speed up performance of
// getStartRowNumberForBlock / getEndRowNumberForBlock, see Bugzilla 47405 // getStartRowNumberForBlock / getEndRowNumberForBlock, see Bugzilla 47405
@ -47,9 +48,15 @@ public final class RowRecordsAggregate extends RecordAggregate {
/** Creates a new instance of ValueRecordsAggregate */ /** Creates a new instance of ValueRecordsAggregate */
public RowRecordsAggregate() { public RowRecordsAggregate() {
this(SharedValueManager.createEmpty()); this(SharedValueManager.createEmpty(), SpreadsheetVersion.EXCEL97);
} }
private RowRecordsAggregate(SharedValueManager svm) { public RowRecordsAggregate(final SpreadsheetVersion spreadsheetVersion) {
this(SharedValueManager.createEmpty(), spreadsheetVersion);
if (spreadsheetVersion == null) {
throw new IllegalArgumentException("SpreadsheetVersion must be provided.");
}
}
private RowRecordsAggregate(SharedValueManager svm, final SpreadsheetVersion spreadsheetVersion) {
if (svm == null) { if (svm == null) {
throw new IllegalArgumentException("SharedValueManager must be provided."); throw new IllegalArgumentException("SharedValueManager must be provided.");
} }
@ -57,6 +64,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
_valuesAgg = new ValueRecordsAggregate(); _valuesAgg = new ValueRecordsAggregate();
_unknownRecords = new ArrayList<Record>(); _unknownRecords = new ArrayList<Record>();
_sharedValueManager = svm; _sharedValueManager = svm;
_spreadsheetVersion = spreadsheetVersion;
} }
/** /**
@ -66,7 +74,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
* and table records of the current sheet). Never <code>null</code>. * and table records of the current sheet). Never <code>null</code>.
*/ */
public RowRecordsAggregate(RecordStream rs, SharedValueManager svm) { public RowRecordsAggregate(RecordStream rs, SharedValueManager svm) {
this(svm); this(svm, SpreadsheetVersion.EXCEL97);
while(rs.hasNext()) { while(rs.hasNext()) {
Record rec = rs.getNext(); Record rec = rs.getNext();
switch (rec.getSid()) { switch (rec.getSid()) {
@ -143,7 +151,7 @@ public final class RowRecordsAggregate extends RecordAggregate {
} }
public RowRecord getRow(int rowIndex) { public RowRecord getRow(int rowIndex) {
int maxrow = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastRowIndex(); int maxrow = _spreadsheetVersion.getLastRowIndex();
if (rowIndex < 0 || rowIndex > maxrow) { if (rowIndex < 0 || rowIndex > maxrow) {
throw new IllegalArgumentException("The row number must be between 0 and " + maxrow + ", but had: " + rowIndex); throw new IllegalArgumentException("The row number must be between 0 and " + maxrow + ", but had: " + rowIndex);
} }

View File

@ -42,13 +42,7 @@ import org.apache.poi.ss.formula.FormulaType;
import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.ptg.ExpPtg; import org.apache.poi.ss.formula.ptg.ExpPtg;
import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.FormulaError;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.util.CellAddress; import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
@ -70,11 +64,6 @@ import org.apache.poi.util.LocaleUtil;
*/ */
public class HSSFCell implements Cell { public class HSSFCell implements Cell {
private static final String FILE_FORMAT_NAME = "BIFF8"; private static final String FILE_FORMAT_NAME = "BIFF8";
/**
* The maximum number of columns in BIFF8
*/
public static final int LAST_COLUMN_NUMBER = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastColumnIndex(); // 2^8 - 1
private static final String LAST_COLUMN_NAME = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastColumnName();
public final static short ENCODING_UNCHANGED = -1; public final static short ENCODING_UNCHANGED = -1;
public final static short ENCODING_COMPRESSED_UNICODE = 0; public final static short ENCODING_COMPRESSED_UNICODE = 0;
@ -105,7 +94,7 @@ public class HSSFCell implements Cell {
*/ */
protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col) protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col)
{ {
checkBounds(col); checkBounds(col, book);
_stringValue = null; _stringValue = null;
_book = book; _book = book;
_sheet = sheet; _sheet = sheet;
@ -150,7 +139,7 @@ public class HSSFCell implements Cell {
protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col, protected HSSFCell(HSSFWorkbook book, HSSFSheet sheet, int row, short col,
CellType type) CellType type)
{ {
checkBounds(col); checkBounds(col, book);
_cellType = CellType._NONE; // Force 'setCellType' to create a first Record _cellType = CellType._NONE; // Force 'setCellType' to create a first Record
_stringValue = null; _stringValue = null;
_book = book; _book = book;
@ -570,8 +559,8 @@ public class HSSFCell implements Cell {
return; return;
} }
if(value.length() > org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getMaxTextLength()){ if(value.length() > _book.getSpreadsheetVersion().getMaxTextLength()){
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters"); throw new IllegalArgumentException("The maximum length of cell contents (text) is "+_book.getSpreadsheetVersion().getMaxTextLength()+" characters");
} }
if (_cellType == CellType.FORMULA) { if (_cellType == CellType.FORMULA) {
@ -977,8 +966,10 @@ public class HSSFCell implements Cell {
/** /**
* @throws RuntimeException if the bounds are exceeded. * @throws RuntimeException if the bounds are exceeded.
*/ */
private static void checkBounds(int cellIndex) { private static void checkBounds(int cellIndex, final HSSFWorkbook book) {
final int LAST_COLUMN_NUMBER = book.getSpreadsheetVersion().getLastColumnIndex(); // 2^8 - 1
if (cellIndex < 0 || cellIndex > LAST_COLUMN_NUMBER) { if (cellIndex < 0 || cellIndex > LAST_COLUMN_NUMBER) {
final String LAST_COLUMN_NAME = book.getSpreadsheetVersion().getLastColumnName();
throw new IllegalArgumentException("Invalid column index (" + cellIndex throw new IllegalArgumentException("Invalid column index (" + cellIndex
+ "). Allowable column range for " + FILE_FORMAT_NAME + " is (0.." + "). Allowable column range for " + FILE_FORMAT_NAME + " is (0.."
+ LAST_COLUMN_NUMBER + ") or ('A'..'" + LAST_COLUMN_NAME + "')"); + LAST_COLUMN_NUMBER + ") or ('A'..'" + LAST_COLUMN_NAME + "')");

View File

@ -29,8 +29,8 @@ import org.apache.poi.util.Removal;
*/ */
public final class HSSFClientAnchor extends HSSFAnchor implements ClientAnchor { public final class HSSFClientAnchor extends HSSFAnchor implements ClientAnchor {
public static final int MAX_COL = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastColumnIndex(); public static final int MAX_COL = SpreadsheetVersion.EXCEL97.getLastColumnIndex();
public static final int MAX_ROW = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastRowIndex(); public static final int MAX_ROW = SpreadsheetVersion.EXCEL97.getLastRowIndex();
private EscherClientAnchorRecord _escherClientAnchor; private EscherClientAnchorRecord _escherClientAnchor;

View File

@ -296,7 +296,7 @@ public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, E
@Override @Override
public SpreadsheetVersion getSpreadsheetVersion(){ public SpreadsheetVersion getSpreadsheetVersion(){
return org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion; return _uBook.getSpreadsheetVersion();
} }
/** /**

View File

@ -129,7 +129,7 @@ public final class HSSFName implements Name {
* @throws IllegalArgumentException if the name is invalid or the name already exists (case-insensitive) * @throws IllegalArgumentException if the name is invalid or the name already exists (case-insensitive)
*/ */
public void setNameName(String nameName){ public void setNameName(String nameName){
validateName(nameName); validateName(nameName, _book.getSpreadsheetVersion());
InternalWorkbook wb = _book.getWorkbook(); InternalWorkbook wb = _book.getWorkbook();
_definedNameRec.setNameText(nameName); _definedNameRec.setNameText(nameName);
@ -178,7 +178,7 @@ public final class HSSFName implements Name {
* *
* @param name * @param name
*/ */
private static void validateName(String name) { private static void validateName(String name, SpreadsheetVersion spreadsheetVersion) {
if (name.length() == 0) { if (name.length() == 0) {
throw new IllegalArgumentException("Name cannot be blank"); throw new IllegalArgumentException("Name cannot be blank");
@ -212,7 +212,7 @@ public final class HSSFName implements Name {
if (name.matches("[A-Za-z]+\\d+")) { if (name.matches("[A-Za-z]+\\d+")) {
String col = name.replaceAll("\\d", ""); String col = name.replaceAll("\\d", "");
String row = name.replaceAll("[A-Za-z]", ""); String row = name.replaceAll("[A-Za-z]", "");
if (CellReference.cellReferenceIsWithinRange(col, row, org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion)) { if (CellReference.cellReferenceIsWithinRange(col, row, spreadsheetVersion)) {
throw new IllegalArgumentException("Invalid name: '"+name+"': cannot be $A$1-style cell reference"); throw new IllegalArgumentException("Invalid name: '"+name+"': cannot be $A$1-style cell reference");
} }
} }

View File

@ -247,7 +247,7 @@ public final class HSSFRow implements Row, Comparable<HSSFRow> {
*/ */
@Override @Override
public void setRowNum(int rowIndex) { public void setRowNum(int rowIndex) {
int maxrow = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastRowIndex(); int maxrow = book.getSpreadsheetVersion().getLastRowIndex();
if ((rowIndex < 0) || (rowIndex > maxrow)) { if ((rowIndex < 0) || (rowIndex > maxrow)) {
throw new IllegalArgumentException("Invalid row number (" + rowIndex throw new IllegalArgumentException("Invalid row number (" + rowIndex
+ ") outside allowable range (0.." + maxrow + ")"); + ") outside allowable range (0.." + maxrow + ")");

View File

@ -123,7 +123,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createSheet() * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createSheet()
*/ */
protected HSSFSheet(HSSFWorkbook workbook) { protected HSSFSheet(HSSFWorkbook workbook) {
_sheet = InternalSheet.createSheet(); _sheet = InternalSheet.createSheet(workbook.getSpreadsheetVersion());
_rows = new TreeMap<Integer, HSSFRow>(); _rows = new TreeMap<Integer, HSSFRow>();
this._workbook = workbook; this._workbook = workbook;
this._book = workbook.getWorkbook(); this._book = workbook.getWorkbook();
@ -715,7 +715,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
if (region.getNumberOfCells() < 2) { if (region.getNumberOfCells() < 2) {
throw new IllegalArgumentException("Merged region " + region.formatAsString() + " must contain 2 or more cells"); throw new IllegalArgumentException("Merged region " + region.formatAsString() + " must contain 2 or more cells");
} }
region.validate(org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion); region.validate(_workbook.getSpreadsheetVersion());
if (validate) { if (validate) {
// throw IllegalStateException if the argument CellRangeAddress intersects with // throw IllegalStateException if the argument CellRangeAddress intersects with
@ -1453,7 +1453,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
*/ */
@Override @Override
public void showInPane(int toprow, int leftcol) { public void showInPane(int toprow, int leftcol) {
int maxrow = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastRowIndex(); final int maxrow = _workbook.getSpreadsheetVersion().getLastRowIndex();
if (toprow > maxrow) throw new IllegalArgumentException("Maximum row number is " + maxrow); if (toprow > maxrow) throw new IllegalArgumentException("Maximum row number is " + maxrow);
showInPane((short)toprow, (short)leftcol); showInPane((short)toprow, (short)leftcol);
@ -1530,10 +1530,10 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* *
* @param row the row number * @param row the row number
*/ */
private static int clip(int row) { private int clip(int row) {
return Math.min( return Math.min(
Math.max(0, row), Math.max(0, row),
org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastRowIndex()); _workbook.getSpreadsheetVersion().getLastRowIndex());
} }
/** /**
@ -1677,7 +1677,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
} }
} }
if (endRow + n > _lastrow) { if (endRow + n > _lastrow) {
_lastrow = Math.min(endRow + n, org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastRowIndex()); _lastrow = Math.min(endRow + n, _workbook.getSpreadsheetVersion().getLastRowIndex());
} }
} else { } else {
// Rows are moving up // Rows are moving up
@ -1687,7 +1687,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
if (endRow == _lastrow) { if (endRow == _lastrow) {
// Need to walk backward to find the last non-blank row // Need to walk backward to find the last non-blank row
// NOTE: n is always negative here // NOTE: n is always negative here
_lastrow = Math.min(endRow + n, org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastRowIndex()); _lastrow = Math.min(endRow + n, _workbook.getSpreadsheetVersion().getLastRowIndex());
for (int i = endRow - 1; i > endRow + n; i--) { for (int i = endRow - 1; i > endRow + n; i--) {
if (getRow(i) != null) { if (getRow(i) != null) {
_lastrow = i; _lastrow = i;
@ -1703,7 +1703,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
String sheetName = _workbook.getSheetName(sheetIndex); String sheetName = _workbook.getSheetName(sheetIndex);
short externSheetIndex = _book.checkExternSheet(sheetIndex); short externSheetIndex = _book.checkExternSheet(sheetIndex);
FormulaShifter shifter = FormulaShifter.createForRowShift( FormulaShifter shifter = FormulaShifter.createForRowShift(
externSheetIndex, sheetName, startRow, endRow, n, org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion); externSheetIndex, sheetName, startRow, endRow, n, _workbook.getSpreadsheetVersion());
_sheet.updateFormulasAfterCellShift(shifter, externSheetIndex); _sheet.updateFormulasAfterCellShift(shifter, externSheetIndex);
int nSheets = _workbook.getNumberOfSheets(); int nSheets = _workbook.getNumberOfSheets();
@ -1923,7 +1923,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
@Override @Override
public void setColumnBreak(int column) { public void setColumnBreak(int column) {
validateColumn((short) column); validateColumn((short) column);
_sheet.getPageSettings().setColumnBreak((short) column, (short) 0, (short) org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastRowIndex()); _sheet.getPageSettings().setColumnBreak((short) column, (short) 0, (short) _workbook.getSpreadsheetVersion().getLastRowIndex());
} }
/** /**
@ -1953,7 +1953,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* @param row the index of the row to validate, zero-based * @param row the index of the row to validate, zero-based
*/ */
protected void validateRow(int row) { protected void validateRow(int row) {
int maxrow = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastRowIndex(); final int maxrow = _workbook.getSpreadsheetVersion().getLastRowIndex();
if (row > maxrow) throw new IllegalArgumentException("Maximum row number is " + maxrow); if (row > maxrow) throw new IllegalArgumentException("Maximum row number is " + maxrow);
if (row < 0) throw new IllegalArgumentException("Minumum row number is 0"); if (row < 0) throw new IllegalArgumentException("Minumum row number is 0");
} }
@ -1964,7 +1964,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
* @param column the index of the column to validate, zero-based * @param column the index of the column to validate, zero-based
*/ */
protected void validateColumn(int column) { protected void validateColumn(int column) {
int maxcol = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastColumnIndex(); final int maxcol = _workbook.getSpreadsheetVersion().getLastColumnIndex();
if (column > maxcol) throw new IllegalArgumentException("Maximum column number is " + maxcol); if (column > maxcol) throw new IllegalArgumentException("Maximum column number is " + maxcol);
if (column < 0) throw new IllegalArgumentException("Minimum column number is 0"); if (column < 0) throw new IllegalArgumentException("Minimum column number is 0");
} }
@ -2496,8 +2496,8 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
private void setRepeatingRowsAndColumns( private void setRepeatingRowsAndColumns(
CellRangeAddress rowDef, CellRangeAddress colDef) { CellRangeAddress rowDef, CellRangeAddress colDef) {
int sheetIndex = _workbook.getSheetIndex(this); int sheetIndex = _workbook.getSheetIndex(this);
int maxRowIndex = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastRowIndex(); final int maxRowIndex = _workbook.getSpreadsheetVersion().getLastRowIndex();
int maxColIndex = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastColumnIndex(); final int maxColIndex = _workbook.getSpreadsheetVersion().getLastColumnIndex();
int col1 = -1; int col1 = -1;
int col2 = -1; int col2 = -1;
@ -2582,8 +2582,8 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
return null; return null;
} }
int maxRowIndex = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastRowIndex(); final int maxRowIndex = _workbook.getSpreadsheetVersion().getLastRowIndex();
int maxColIndex = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastColumnIndex(); final int maxColIndex = _workbook.getSpreadsheetVersion().getLastColumnIndex();
for (Ptg ptg : nameDefinition) { for (Ptg ptg : nameDefinition) {

View File

@ -174,7 +174,7 @@ public final class HSSFSheetConditionalFormatting implements SheetConditionalFor
if (regions == null) { if (regions == null) {
throw new IllegalArgumentException("regions must not be null"); throw new IllegalArgumentException("regions must not be null");
} }
for(CellRangeAddress range : regions) range.validate(org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion); for(CellRangeAddress range : regions) range.validate(_sheet._workbook.getSpreadsheetVersion());
if (cfRules == null) { if (cfRules == null) {
throw new IllegalArgumentException("cfRules must not be null"); throw new IllegalArgumentException("cfRules must not be null");

View File

@ -124,7 +124,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
private static final Pattern COMMA_PATTERN = Pattern.compile(","); private static final Pattern COMMA_PATTERN = Pattern.compile(",");
// org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion // org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion
public static SpreadsheetVersion spreadsheetVersion = SpreadsheetVersion. EXCEL97; public final SpreadsheetVersion spreadsheetVersion;
/** /**
* The maximum number of cell styles in a .xls workbook. * The maximum number of cell styles in a .xls workbook.
@ -210,8 +210,19 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
this(InternalWorkbook.createWorkbook()); this(InternalWorkbook.createWorkbook());
} }
public HSSFWorkbook(final SpreadsheetVersion spreadsheetVersion) {
this(InternalWorkbook.createWorkbook(), spreadsheetVersion);
if(spreadsheetVersion == null)
throw new IllegalArgumentException("SpreadsheetVersion must be non-null");
}
private HSSFWorkbook(InternalWorkbook book) { private HSSFWorkbook(InternalWorkbook book) {
this(book, SpreadsheetVersion.EXCEL97);
}
private HSSFWorkbook(InternalWorkbook book, final SpreadsheetVersion spreadsheetVersion) {
super((DirectoryNode)null); super((DirectoryNode)null);
this.spreadsheetVersion = spreadsheetVersion;
workbook = book; workbook = book;
_sheets = new ArrayList<HSSFSheet>(INITIAL_CAPACITY); _sheets = new ArrayList<HSSFSheet>(INITIAL_CAPACITY);
names = new ArrayList<HSSFName>(INITIAL_CAPACITY); names = new ArrayList<HSSFName>(INITIAL_CAPACITY);
@ -331,6 +342,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
throws IOException throws IOException
{ {
super(directory); super(directory);
spreadsheetVersion = SpreadsheetVersion.EXCEL97;
String workbookName = getWorkbookDirEntryName(directory); String workbookName = getWorkbookDirEntryName(directory);
this.preserveNodes = preserveNodes; this.preserveNodes = preserveNodes;
@ -1366,6 +1378,12 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
super.close(); super.close();
} }
protected void validateSpreadsheetVersionWritePossible() throws IllegalStateException {
if (this.spreadsheetVersion != SpreadsheetVersion.EXCEL97) {
throw new IllegalStateException("SpreadsheetVersion not EXCEL97, cannot write file meant only for in-memory calculations");
}
}
/** /**
* Write out this workbook to the currently open {@link File} via the * Write out this workbook to the currently open {@link File} via the
* writeable {@link POIFSFileSystem} it was opened as. * writeable {@link POIFSFileSystem} it was opened as.
@ -1378,6 +1396,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
*/ */
@Override @Override
public void write() throws IOException { public void write() throws IOException {
validateSpreadsheetVersionWritePossible();
validateInPlaceWritePossible(); validateInPlaceWritePossible();
final DirectoryNode dir = getDirectory(); final DirectoryNode dir = getDirectory();
@ -1411,6 +1430,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
*/ */
@Override @Override
public void write(File newFile) throws IOException { public void write(File newFile) throws IOException {
validateSpreadsheetVersionWritePossible();
POIFSFileSystem fs = POIFSFileSystem.create(newFile); POIFSFileSystem fs = POIFSFileSystem.create(newFile);
try { try {
write(fs); write(fs);
@ -1437,6 +1457,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
*/ */
@Override @Override
public void write(OutputStream stream) throws IOException { public void write(OutputStream stream) throws IOException {
validateSpreadsheetVersionWritePossible();
NPOIFSFileSystem fs = new NPOIFSFileSystem(); NPOIFSFileSystem fs = new NPOIFSFileSystem();
try { try {
write(fs); write(fs);
@ -1448,6 +1469,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
/** Writes the workbook out to a brand new, empty POIFS */ /** Writes the workbook out to a brand new, empty POIFS */
private void write(NPOIFSFileSystem fs) throws IOException { private void write(NPOIFSFileSystem fs) throws IOException {
validateSpreadsheetVersionWritePossible();
// For tracking what we've written out, used if we're // For tracking what we've written out, used if we're
// going to be preserving nodes // going to be preserving nodes
List<String> excepts = new ArrayList<String>(1); List<String> excepts = new ArrayList<String>(1);
@ -2328,6 +2350,6 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
*/ */
@Override @Override
public SpreadsheetVersion getSpreadsheetVersion() { public SpreadsheetVersion getSpreadsheetVersion() {
return org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion; return this.spreadsheetVersion;
} }
} }

View File

@ -140,7 +140,7 @@ public final class FormulaParser {
_formulaString = formula; _formulaString = formula;
_pointer=0; _pointer=0;
_book = book; _book = book;
_ssVersion = book == null ? org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion : book.getSpreadsheetVersion(); _ssVersion = book == null ? SpreadsheetVersion.EXCEL97 : book.getSpreadsheetVersion();
_formulaLength = _formulaString.length(); _formulaLength = _formulaString.length();
_sheetIndex = sheetIndex; _sheetIndex = sheetIndex;
_rowIndex = rowIndex; _rowIndex = rowIndex;

View File

@ -184,7 +184,7 @@ public final class SheetNameFormatter {
* @see org.apache.poi.ss.util.CellReference * @see org.apache.poi.ss.util.CellReference
*/ */
/* package */ static boolean cellReferenceIsWithinRange(String lettersPrefix, String numbersSuffix) { /* package */ static boolean cellReferenceIsWithinRange(String lettersPrefix, String numbersSuffix) {
return CellReference.cellReferenceIsWithinRange(lettersPrefix, numbersSuffix, org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion); return CellReference.cellReferenceIsWithinRange(lettersPrefix, numbersSuffix, SpreadsheetVersion.EXCEL97);
} }
/** /**

View File

@ -286,7 +286,7 @@ public abstract class AreaPtgBase extends OperandPtg implements AreaI {
CellReference topLeft = new CellReference(getFirstRow(),getFirstColumn(),!isFirstRowRelative(),!isFirstColRelative()); CellReference topLeft = new CellReference(getFirstRow(),getFirstColumn(),!isFirstRowRelative(),!isFirstColRelative());
CellReference botRight = new CellReference(getLastRow(),getLastColumn(),!isLastRowRelative(),!isLastColRelative()); CellReference botRight = new CellReference(getLastRow(),getLastColumn(),!isLastRowRelative(),!isLastColRelative());
if(AreaReference.isWholeColumnReference(org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion, topLeft, botRight)) { if(AreaReference.isWholeColumnReference(SpreadsheetVersion.EXCEL97, topLeft, botRight)) {
return (new AreaReference(topLeft, botRight)).formatAsString(); return (new AreaReference(topLeft, botRight)).formatAsString();
} }
return topLeft.formatAsString() + ":" + botRight.formatAsString(); return topLeft.formatAsString() + ":" + botRight.formatAsString();

View File

@ -31,7 +31,7 @@ public class AreaReference {
private static final char CELL_DELIMITER = ':'; private static final char CELL_DELIMITER = ':';
/** The character (') used to quote sheet names when they contain special characters */ /** The character (') used to quote sheet names when they contain special characters */
private static final char SPECIAL_NAME_DELIMITER = '\''; private static final char SPECIAL_NAME_DELIMITER = '\'';
private static final SpreadsheetVersion DEFAULT_SPREADSHEET_VERSION = org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion; private static final SpreadsheetVersion DEFAULT_SPREADSHEET_VERSION = SpreadsheetVersion.EXCEL97;
private final CellReference _firstCell; private final CellReference _firstCell;
private final CellReference _lastCell; private final CellReference _lastCell;

View File

@ -75,12 +75,12 @@ public abstract class CellRangeAddressBase {
//TODO use the correct SpreadsheetVersion //TODO use the correct SpreadsheetVersion
public final boolean isFullColumnRange() { public final boolean isFullColumnRange() {
return (_firstRow == 0 && _lastRow == org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastRowIndex()) return (_firstRow == 0 && _lastRow == SpreadsheetVersion.EXCEL97.getLastRowIndex())
|| (_firstRow == -1 && _lastRow == -1); || (_firstRow == -1 && _lastRow == -1);
} }
//TODO use the correct SpreadsheetVersion //TODO use the correct SpreadsheetVersion
public final boolean isFullRowRange() { public final boolean isFullRowRange() {
return (_firstCol == 0 && _lastCol == org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion.getLastColumnIndex()) return (_firstCol == 0 && _lastCol == SpreadsheetVersion.EXCEL97.getLastColumnIndex())
|| (_firstCol == -1 && _lastCol == -1); || (_firstCol == -1 && _lastCol == -1);
} }

View File

@ -402,7 +402,7 @@ public final class XSSFName implements Name {
if (name.matches("[A-Za-z]+\\d+")) { if (name.matches("[A-Za-z]+\\d+")) {
String col = name.replaceAll("\\d", ""); String col = name.replaceAll("\\d", "");
String row = name.replaceAll("[A-Za-z]", ""); String row = name.replaceAll("[A-Za-z]", "");
if (CellReference.cellReferenceIsWithinRange(col, row, org.apache.poi.hssf.usermodel.HSSFWorkbook.spreadsheetVersion)) { if (CellReference.cellReferenceIsWithinRange(col, row, SpreadsheetVersion.EXCEL97)) { // todo: is EXCEL97 correct? sounds wrong...
throw new IllegalArgumentException("Invalid name: '"+name+"': cannot be $A$1-style cell reference"); throw new IllegalArgumentException("Invalid name: '"+name+"': cannot be $A$1-style cell reference");
} }
} }