From: Yegor Kozlov Date: Tue, 21 Oct 2008 17:56:34 +0000 (+0000) Subject: 1. implemented XSSFSheet.autosizeColumn(), for now mostly duplicated HSSF code, will... X-Git-Tag: ooxml_20081107~22 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c63434df63697adf242805f7a0bde52d767907d2;p=poi.git 1. implemented XSSFSheet.autosizeColumn(), for now mostly duplicated HSSF code, will be refactored in future.2. fixed bug #45974: XSSFCell.getCellStyle can return null3. more code cleanup and reaftoring, removed usages of obsolete XSSFCell.getCellNum() in favor of XSSFCell.getColumnIndex(), also more javadoc in core classes 4. fixed a blocker: calling XSSFSheet.getNumMergedRegions() resulted in unreadable workbook, this methods structurally modified worksheet and added unnecessary data git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@706691 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/examples/jsp/HSSFExample.jsp b/src/examples/jsp/HSSFExample.jsp index 97b62a5531..b641846bd1 100644 --- a/src/examples/jsp/HSSFExample.jsp +++ b/src/examples/jsp/HSSFExample.jsp @@ -92,7 +92,7 @@ row.getRowNum() %> %> <%= "CELL col=" - + cell.getCellNum() + + cell.getColumnIndex() + " VALUE=" + value %> <% } diff --git a/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java b/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java index adedc2d918..d438facc1e 100644 --- a/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java +++ b/src/examples/src/org/apache/poi/ss/usermodel/examples/FromQuickGuide.java @@ -143,7 +143,7 @@ public class FromQuickGuide { public static void getCellContents(Sheet sheet) { for (Row row : sheet) { for (Cell cell : row) { - CellReference cellRef = new CellReference(row.getRowNum(), cell.getCellNum()); + CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex()); System.out.print(cellRef.formatAsString()); System.out.print(" - "); diff --git a/src/examples/src/org/apache/poi/xssf/usermodel/examples/IterateCells.java b/src/examples/src/org/apache/poi/xssf/usermodel/examples/IterateCells.java index fc41e27042..f644a58f51 100755 --- a/src/examples/src/org/apache/poi/xssf/usermodel/examples/IterateCells.java +++ b/src/examples/src/org/apache/poi/xssf/usermodel/examples/IterateCells.java @@ -28,7 +28,7 @@ import org.apache.poi.ss.usermodel.Row; public class IterateCells { public static void main(String[] args) throws Exception { - Workbook wb = new XSSFWorkbook(args[0]); + Workbook wb = null; for (int i = 0; i < wb.getNumberOfSheets(); i++) { Sheet sheet = wb.getSheetAt(i); System.out.println(wb.getSheetName(i)); diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Row.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Row.java index 087c142ed1..1c2efd0c35 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Row.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Row.java @@ -45,12 +45,12 @@ public interface Row extends Iterable { * * @param column - the column number this cell represents * - * @return HSSFCell a high level representation of the created cell. + * @return Cell a high level representation of the created cell. */ Cell createCell(int column, int type); /** - * remove the HSSFCell from this row. + * remove the Cell from this row. * @param cell to remove */ void removeCell(Cell cell); @@ -71,7 +71,7 @@ public interface Row extends Iterable { int getRowNum(); /** - * get the hssfcell representing a given column (logical cell) 0-based. If you + * get the cell representing a given column (logical cell) 0-based. If you * ask for a cell that is not defined....you get a null. * * @param cellnum 0 based column number @@ -80,7 +80,7 @@ public interface Row extends Iterable { Cell getCell(int cellnum); /** - * Get the hssfcell representing a given column (logical cell) + * Get the cell representing a given column (logical cell) * 0-based. If you ask for a cell that is not defined, then * your supplied policy says what to do * @@ -158,12 +158,6 @@ public interface Row extends Iterable { */ Iterator cellIterator(); - /** - * Alias for {@link #cellIterator()} to allow - * foreach loops - */ - Iterator iterator(); - /** * Used to specify the different possible policies * if for the case of null and blank cells diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java index d5ad5bddad..6625c6ef64 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Sheet.java @@ -19,7 +19,6 @@ package org.apache.poi.ss.usermodel; import java.util.Iterator; -import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.util.PaneInformation; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.Region; @@ -27,13 +26,17 @@ import org.apache.poi.ss.util.Region; public interface Sheet extends Iterable { /* Constants for margins */ - public static final short LeftMargin = Sheet.LeftMargin; + public static final short LeftMargin = 0; - public static final short RightMargin = Sheet.RightMargin; + public static final short RightMargin = 1; - public static final short TopMargin = Sheet.TopMargin; + public static final short TopMargin = 2; - public static final short BottomMargin = Sheet.BottomMargin; + public static final short BottomMargin = 3; + + public static final short HeaderMargin = 4; + + public static final short FooterMargin = 5; public static final byte PANE_LOWER_RIGHT = (byte) 0; @@ -57,7 +60,7 @@ public interface Sheet extends Iterable { * @param rownum row number * @return High level HSSFRow object representing a row in the sheet * @see org.apache.poi.hssf.usermodel.HSSFRow - * @see #removeRow(HSSFRow) + * @see #removeRow(Row) */ Row createRow(int rownum); @@ -246,12 +249,6 @@ public interface Sheet extends Iterable { * be the third row if say for instance the second row is undefined. */ Iterator rowIterator(); - - /** - * Alias for {@link #rowIterator()} to allow - * foreach loops - */ - Iterator iterator(); /** * whether alternate expression evaluation is on @@ -274,13 +271,6 @@ public interface Sheet extends Iterable { void setAutobreaks(boolean b); - /** - * set whether sheet is a dialog sheet or not - * @param b isDialog or not - */ - - void setDialog(boolean b); - /** * set whether to display the guts or not * @@ -331,13 +321,6 @@ public interface Sheet extends Iterable { boolean getAutobreaks(); - /** - * get whether sheet is a dialog sheet or not - * @return isDialog or not - */ - - boolean getDialog(); - /** * get whether to display the guts or not * diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java index 91d0cc9851..b853c92583 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Workbook.java @@ -171,22 +171,6 @@ public interface Workbook { void removeSheetAt(int index); - /** - * determine whether the Excel GUI will backup the workbook when saving. - * - * @param backupValue true to indicate a backup will be performed. - */ - - void setBackupFlag(boolean backupValue); - - /** - * determine whether the Excel GUI will backup the workbook when saving. - * - * @return the current setting for backups. - */ - - boolean getBackupFlag(); - /** * Sets the repeating rows and columns for a sheet (as found in * File->PageSetup->Sheet). This is function is included in the workbook @@ -337,7 +321,7 @@ public interface Workbook { * Sets the policy on what to do when * getting missing or blank cells from a row. * This will then apply to all calls to - * {@link Row.getCell()}. See + * {@link Row#getCell(int)} }. See * {@link MissingCellPolicy} */ void setMissingCellPolicy(MissingCellPolicy missingCellPolicy); @@ -391,22 +375,10 @@ public interface Workbook { */ List getAllPictures(); - /** - * protect a workbook with a password (not encypted, just sets writeprotect - * flags and the password. - * @param password to set - */ - void writeProtectWorkbook(String password, String username); - - /** - * removes the write protect flag - */ - void unwriteProtectWorkbook(); - /** * Gets all embedded OLE2 objects from the Workbook. * - * @return the list of embedded objects (a list of {@link HSSFObjectData} objects.) + * @return the list of embedded objects */ List getAllEmbeddedObjects(); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFAnchor.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFAnchor.java index 89f4fb8869..f640f408df 100755 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFAnchor.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFAnchor.java @@ -23,5 +23,14 @@ package org.apache.poi.xssf.usermodel; * @author Yegor Kozlov */ public abstract class XSSFAnchor { + + public abstract int getDx1(); + public abstract void setDx1( int dx1 ); + public abstract int getDy1(); + public abstract void setDy1( int dy1 ); + public abstract int getDy2(); + public abstract void setDy2( int dy2 ); + public abstract int getDx2(); + public abstract void setDx2( int dx2 ); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 8ac49b8443..6388258698 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -90,7 +90,7 @@ public final class XSSFCell implements Cell { } public Comment getCellComment() { - return row.getSheet().getCellComment(row.getRowNum(), getCellNum()); + return row.getSheet().getCellComment(row.getRowNum(), getColumnIndex()); } public String getCellFormula() { @@ -113,12 +113,15 @@ public final class XSSFCell implements Cell { return row.getRowNum(); } + /** + * Return the cell's style. + * + * @return the cell's style. Always not-null. Default cell style has zero index and can be obtained as + * workbook.getCellStyleAt(0) + */ public XSSFCellStyle getCellStyle() { - // Zero is the empty default - if(this.cell.getS() > 0) { - return stylesSource.getStyleAt(this.cell.getS()); - } - return null; + long idx = cell.isSetS() ? cell.getS() : 0; + return stylesSource.getStyleAt(idx); } public int getCellType() { @@ -156,7 +159,7 @@ public final class XSSFCell implements Cell { public Date getDateCellValue() { if (STCellType.N == this.cell.getT() || STCellType.STR == this.cell.getT()) { double value = this.getNumericCellValue(); - if (false /* book.isUsing1904DateWindowing() */) { // FIXME + if (row.getSheet().getWorkbook().isDate1904()) { return DateUtil.getJavaDate(value,true); } else { @@ -269,7 +272,7 @@ public final class XSSFCell implements Cell { public void setCellComment(Comment comment) { - String cellRef = new CellReference(row.getRowNum(), getCellNum()).formatAsString(); + String cellRef = new CellReference(row.getRowNum(), getColumnIndex()).formatAsString(); row.getSheet().setCellComment(cellRef, (XSSFComment)comment); } @@ -333,7 +336,7 @@ public final class XSSFCell implements Cell { } protected String formatPosition() { - int col = this.getCellNum(); + int col = this.getColumnIndex(); String result = Character.valueOf((char) (col % 26 + 'A')).toString(); if (col >= 26){ col = col / 26; diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index 6bdb8a0e1c..68f5784256 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -145,7 +145,12 @@ public class XSSFCellStyle implements CellStyle, Cloneable { * @return HorizontalAlignment - the type of alignment */ public HorizontalAlignment getAlignmentEnum() { - return getCellAlignment().getHorizontal(); + CTCellAlignment align = cellXf.getAlignment(); + if(align != null && align.isSetHorizontal()) { + return HorizontalAlignment.values()[align.getHorizontal().intValue()-1]; + } else { + return HorizontalAlignment.GENERAL; + } } /** @@ -485,7 +490,8 @@ public class XSSFCellStyle implements CellStyle, Cloneable { * @return indent - number of spaces */ public short getIndention() { - return (short) getCellAlignment().getIndent(); + CTCellAlignment align = cellXf.getAlignment(); + return (short)(align == null ? 0 : align.getIndent()); } /** @@ -571,7 +577,8 @@ public class XSSFCellStyle implements CellStyle, Cloneable { * @return rotation degrees (between 0 and 180 degrees) */ public short getRotation() { - return (short) getCellAlignment().getTextRotation(); + CTCellAlignment align = cellXf.getAlignment(); + return (short)(align == null ? 0 : align.getTextRotation()); } /** @@ -619,7 +626,12 @@ public class XSSFCellStyle implements CellStyle, Cloneable { * @see VerticalAlignment */ public VerticalAlignment getVerticalAlignmentEnum() { - return getCellAlignment().getVertical(); + CTCellAlignment align = cellXf.getAlignment(); + if(align != null && align.isSetVertical()) { + return VerticalAlignment.values()[align.getVertical().intValue()-1]; + } else { + return VerticalAlignment.BOTTOM; + } } /** @@ -628,7 +640,8 @@ public class XSSFCellStyle implements CellStyle, Cloneable { * @return a boolean value indicating if the text in a cell should be line-wrapped within the cell. */ public boolean getWrapText() { - return getCellAlignment().getWrapText(); + CTCellAlignment align = cellXf.getAlignment(); + return align != null && align.getWrapText(); } /** diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDialogsheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDialogsheet.java index b1f6246b6f..06751387ff 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDialogsheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDialogsheet.java @@ -27,6 +27,7 @@ import org.openxml4j.opc.PackageRelationship; import java.io.IOException; public class XSSFDialogsheet extends XSSFSheet implements Sheet{ + protected CTDialogsheet dialogsheet; public XSSFDialogsheet(XSSFSheet sheet) { this.packagePart = sheet.getPackagePart(); @@ -96,4 +97,7 @@ public class XSSFDialogsheet extends XSSFSheet implements Sheet{ return dialogsheet.getSheetProtection(); } + public boolean getDialog(){ + return true; + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java index 14378849ce..b42659dbb7 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java @@ -58,7 +58,7 @@ final class XSSFEvaluationCell implements EvaluationCell { return _cell.getCellType(); } public int getColumnIndex() { - return _cell.getCellNum(); + return _cell.getColumnIndex(); } public int getErrorCellValue() { return _cell.getErrorCellValue(); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java index 976bf61178..023cebbaa3 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFFont.java @@ -488,9 +488,7 @@ public class XSSFFont implements Font { public String toString() { - return "org.apache.poi.xssf.usermodel.XSSFFont{" + - ctFont + - "}"; + return ctFont.toString(); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java index 7b4494661d..7fa2ef967c 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java @@ -33,13 +33,13 @@ public class XSSFName implements Name { /** * this defined name specifies the workbook's print area */ - public final static String BUILTIN_PRINT_AREA = "_xlnm.Print_Area"; + public final static String BUILTIN_PRINT_AREA = "_xlnm.Print_Area"; /** * this defined name specifies the row(s) or column(s) to repeat - * at the top of each printed page. + * at the top of each printed page. */ - public final static String BUILTIN_PRINT_TITLE = "_xlnm.Print_Titles"; + public final static String BUILTIN_PRINT_TITLE = "_xlnm.Print_Titles"; //Filter & Advanced Filter @@ -47,7 +47,7 @@ public class XSSFName implements Name { * this defined name refers to a range containing the criteria values * to be used in applying an advanced filter to a range of data */ - public final static String BUILTIN_CRITERIA = "_xlnm.Criteria:"; + public final static String BUILTIN_CRITERIA = "_xlnm.Criteria:"; /** @@ -55,7 +55,7 @@ public class XSSFName implements Name { * output values resulting from applying an advanced filter criteria to a source * range */ - public final static String BUILTIN_EXTRACT = "_xlnm.Extract:"; + public final static String BUILTIN_EXTRACT = "_xlnm.Extract:"; /** * can be one of the following @@ -64,123 +64,127 @@ public class XSSFName implements Name { * b. This defined name refers to a range to which an AutoFilter has been * applied */ - public final static String BUILTIN_FILTER_DB = "_xlnm._FilterDatabase:"; - + public final static String BUILTIN_FILTER_DB = "_xlnm._FilterDatabase:"; //Miscellaneous /** * the defined name refers to a consolidation area */ - public final static String BUILTIN_CONSOLIDATE_AREA = "_xlnm.Consolidate_Area"; + public final static String BUILTIN_CONSOLIDATE_AREA = "_xlnm.Consolidate_Area"; /** * the range specified in the defined name is from a database data source */ - public final static String BUILTIN_DATABASE = "_xlnm.Database"; + public final static String BUILTIN_DATABASE = "_xlnm.Database"; /** * the defined name refers to a sheet title. */ - public final static String BUILTIN_SHEET_TITLE = "_xlnm.Sheet_Title"; + public final static String BUILTIN_SHEET_TITLE = "_xlnm.Sheet_Title"; private XSSFWorkbook workbook; private CTDefinedName ctName; protected XSSFName(XSSFWorkbook workbook) { - this.workbook = workbook; - this.ctName = CTDefinedName.Factory.newInstance(); + this.workbook = workbook; + this.ctName = CTDefinedName.Factory.newInstance(); } + protected XSSFName(CTDefinedName name, XSSFWorkbook workbook) { - this.workbook = workbook; - this.ctName = name; + this.workbook = workbook; + this.ctName = name; } public boolean isFunctionName() { - // TODO Figure out how HSSF does this, and do the same! - return ctName.getFunction(); // maybe this works - verify + // TODO Figure out how HSSF does this, and do the same! + return ctName.getFunction(); // maybe this works - verify } + /** * Returns the underlying named range object */ protected CTDefinedName getCTName() { - return ctName; + return ctName; } public String getNameName() { - return ctName.getName(); + return ctName.getName(); } + public void setNameName(String nameName) { - ctName.setName(nameName); + ctName.setName(nameName); } public String getReference() { - return ctName.getStringValue(); + return ctName.getStringValue(); } + public void setReference(String ref) { - ctName.setStringValue(ref); + ctName.setStringValue(ref); } public void setLocalSheetId(int sheetId) { - ctName.setLocalSheetId(sheetId); + ctName.setLocalSheetId(sheetId); } public int getLocalSheetId() { - return (int)ctName.getLocalSheetId(); + return (int) ctName.getLocalSheetId(); } public void setFunction(boolean value) { - ctName.setFunction(value); + ctName.setFunction(value); } public boolean getFunction() { - return ctName.getFunction(); + return ctName.getFunction(); } public void setFunctionGroupId(int functionGroupId) { - ctName.setFunctionGroupId(functionGroupId); + ctName.setFunctionGroupId(functionGroupId); } public int getFunctionGroupId() { - return (int)ctName.getFunctionGroupId(); + return (int) ctName.getFunctionGroupId(); } public String getSheetName() { - if(ctName.isSetLocalSheetId()) { - // Given as explicit sheet id - long sheetId = ctName.getLocalSheetId(); - if(sheetId >= 0) { - return workbook.getSheetName((int)sheetId); - } - } else { - // Is it embeded in the reference itself? - int excl = getReference().indexOf('!'); - if(excl > -1) { - return getReference().substring(0, excl); - } - } - - // Not given at all - return null; + if (ctName.isSetLocalSheetId()) { + // Given as explicit sheet id + long sheetId = ctName.getLocalSheetId(); + if (sheetId >= 0) { + return workbook.getSheetName((int) sheetId); + } + } else { + // Is it embeded in the reference itself? + int excl = getReference().indexOf('!'); + if (excl > -1) { + return getReference().substring(0, excl); + } + } + + // Not given at all + return null; } public String getComment() { - return ctName.getComment(); + return ctName.getComment(); } + public void setComment(String comment) { - ctName.setComment(comment); + ctName.setComment(comment); } - public int hashCode(){ - return ctName.toString().hashCode(); + public int hashCode() { + return ctName.toString().hashCode(); } - public boolean equals(Object o){ - if(!(o instanceof XSSFName)) return false; - XSSFName cf = (XSSFName)o; - return ctName.toString().equals(cf.getCTName().toString()); + public boolean equals(Object o) { + if (!(o instanceof XSSFName)) return false; + XSSFName cf = (XSSFName) o; + return ctName.toString().equals(cf.getCTName().toString()); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPicture.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPicture.java index 293aeaaee2..2e1394b442 100755 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPicture.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPicture.java @@ -47,12 +47,6 @@ public class XSSFPicture extends XSSFShape { */ private static CTPicture prototype = null; - /** - * Width of one character in pixels. Same for Calibry and Arial. - */ - private static final float CHARACTER_WIDTH = 7.0017f; - - /** * This object specifies a picture object and all its properties */ @@ -214,7 +208,7 @@ public class XSSFPicture extends XSSFShape { XSSFSheet sheet = (XSSFSheet)getDrawing().getParent(); float numChars = (float)sheet.getColumnWidth(columnIndex)/256; - return numChars*CHARACTER_WIDTH; + return numChars*XSSFWorkbook.DEFAULT_CHARACTER_WIDTH; } private float getRowHeightInPixels(int rowIndex){ diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java index 8d1a585730..71529e9f46 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java @@ -453,6 +453,7 @@ public class XSSFRichTextString implements RichTextString { if(c1.isSetTheme()) c2.setTheme(c1.getTheme()); if(c1.isSetTint()) c2.setTint(c1.getTint()); } + if(pr.sizeOfSzArray() > 0) ctFont.addNewSz().setVal(pr.getSzArray(0).getVal()); if(pr.sizeOfRFontArray() > 0) ctFont.addNewName().setVal(pr.getRFontArray(0).getVal()); if(pr.sizeOfFamilyArray() > 0) ctFont.addNewFamily().setVal(pr.getFamilyArray(0).getVal()); if(pr.sizeOfSchemeArray() > 0) ctFont.addNewScheme().setVal(pr.getSchemeArray(0).getVal()); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java index b938c1fac2..d797a7dfe5 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java @@ -27,7 +27,9 @@ import org.apache.poi.ss.usermodel.Row; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow; - +/** + * High level representation of a row of a spreadsheet. + */ public class XSSFRow implements Row, Comparable { private CTRow row; @@ -45,20 +47,30 @@ public class XSSFRow implements Row, Comparable { public XSSFRow(CTRow row, XSSFSheet sheet) { this.row = row; this.sheet = sheet; - this.cells = new LinkedList(); + this.cells = new LinkedList(); for (CTCell c : row.getCArray()) { this.cells.add(new XSSFCell(this, c)); } } + /** + * Returns the XSSFSheet this row belongs to + * + * @return the XSSFSheet that owns this row + */ public XSSFSheet getSheet() { return this.sheet; } + /** + * @return Cell iterator of the physically defined cells. Note element 4 may + * actually be row cell depending on how many are defined! + */ public Iterator cellIterator() { return cells.iterator(); } + /** * Alias for {@link #cellIterator()} to allow * foreach loops @@ -102,7 +114,7 @@ public class XSSFRow implements Row, Comparable { * * @param column Cell column number. * @param index Position where to insert cell. - * @param type TODO + * @param type cell type, one of Cell.CELL_TYPE_* * @return The new cell. */ protected XSSFCell addCell(int column, int index, int type) { @@ -148,7 +160,7 @@ public class XSSFRow implements Row, Comparable { Iterator it = cellIterator(); for ( ; it.hasNext() ; ) { Cell cell = it.next(); - if (cell.getCellNum() == cellnum) { + if (cell.getColumnIndex() == cellnum) { return (XSSFCell)cell; } } @@ -157,8 +169,9 @@ public class XSSFRow implements Row, Comparable { /** * Returns the cell at the given (0 based) index, - * with the {@link MissingCellPolicy} from the - * parent Workbook. + * with the {@link MissingCellPolicy} from the parent Workbook. + * + * @return the cell at the given (0 based) index */ public XSSFCell getCell(int cellnum) { return getCell(cellnum, sheet.getWorkbook().getMissingCellPolicy()); @@ -167,6 +180,8 @@ public class XSSFRow implements Row, Comparable { /** * Returns the cell at the given (0 based) index, * with the specified {@link MissingCellPolicy} + * + * @return the cell at the given (0 based) index */ public XSSFCell getCell(int cellnum, MissingCellPolicy policy) { XSSFCell cell = retrieveCell(cellnum); @@ -189,11 +204,16 @@ public class XSSFRow implements Row, Comparable { throw new IllegalArgumentException("Illegal policy " + policy + " (" + policy.id + ")"); } + /** + * Get the number of the first cell contained in this row. + * + * @return short representing the first logical cell in the row, or -1 if the row does not contain any cells. + */ public short getFirstCellNum() { for (Iterator it = cellIterator() ; it.hasNext() ; ) { Cell cell = it.next(); if (cell != null) { - return cell.getCellNum(); + return (short)cell.getColumnIndex(); } } return -1; @@ -248,12 +268,18 @@ public class XSSFRow implements Row, Comparable { for (Iterator it = cellIterator() ; it.hasNext() ; ) { Cell cell = it.next(); if (cell != null) { - lastCellNum = (short)(cell.getCellNum() + 1); + lastCellNum = (short)(cell.getColumnIndex() + 1); } } return lastCellNum; } + /** + * Gets the number of defined cells (NOT number of cells in the actual row!). + * That is to say if only columns 0,4,5 have values then there would be 3. + * + * @return int representing the number of defined cells in the row. + */ public int getPhysicalNumberOfCells() { int count = 0; for (Iterator it = cellIterator() ; it.hasNext() ; ) { @@ -264,19 +290,34 @@ public class XSSFRow implements Row, Comparable { return count; } + /** + * Get row number this row represents + * + * @return the row number (0 based) + */ public int getRowNum() { return (int) (row.getR() - 1); } + /** + * Get whether or not to display this row with 0 height + * + * @return - height is zero or not. + */ public boolean getZeroHeight() { return this.row.getHidden(); } + /** + * Remove the Cell from this row. + * + * @param cell to remove + */ public void removeCell(Cell cell) { int counter = 0; for (Iterator it = cellIterator(); it.hasNext(); ) { Cell c = it.next(); - if (c.getCellNum() == cell.getCellNum()) { + if (c.getColumnIndex() == cell.getColumnIndex()) { it.remove(); row.removeC(counter); continue; @@ -309,16 +350,31 @@ public class XSSFRow implements Row, Comparable { setHeight((short)(height*20)); } + /** + * Set the row number of this row. + * + * @param rowNum the row number (0-based) + */ public void setRowNum(int rowNum) { this.row.setR(rowNum + 1); } + /** + * Set whether or not to display this row with 0 height + * + * @param height height is zero or not. + */ public void setZeroHeight(boolean height) { this.row.setHidden(height); } + /** + * Returns the underlying CTRow xml bean representing this row + * + * @return the underlying CTRow bean + */ public CTRow getCTRow(){ return this.row; } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java index b4c15b5d53..6795adef80 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java @@ -71,20 +71,10 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { protected CTSheet sheet; protected CTWorksheet worksheet; - protected CTDialogsheet dialogsheet; protected List rows; protected List hyperlinks; protected ColumnHelper columnHelper; private CommentsSource sheetComments; - protected CTMergeCells ctMergeCells; - - public static final short LeftMargin = 0; - public static final short RightMargin = 1; - public static final short TopMargin = 2; - public static final short BottomMargin = 3; - public static final short HeaderMargin = 4; - public static final short FooterMargin = 5; - public XSSFSheet() { super(null, null); @@ -203,7 +193,9 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { public int addMergedRegion(Region region) { - addNewMergeCell(region); + CTMergeCells ctMergeCells = worksheet.isSetMergeCells() ? worksheet.getMergeCells() : worksheet.addNewMergeCells(); + CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell(); + ctMergeCell.setRef(region.getRegionRef()); return ctMergeCells.sizeOfMergeCellArray(); } @@ -234,8 +226,12 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { * @param useMergedCells whether to use the contents of merged cells when calculating the width of the column */ public void autoSizeColumn(short column, boolean useMergedCells) { - //TODO: - columnHelper.setColBestFit(column, true); + double width = ColumnHelper.getColumnWidth(this, column, useMergedCells); + if(width != -1){ + columnHelper.setColBestFit(column, true); + columnHelper.setCustomWidth(column, true); + columnHelper.setColWidth(column, width); + } } /** @@ -474,14 +470,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { worksheet.addNewSheetFormatPr(); } - public boolean getDialog() { - if (dialogsheet != null) { - return true; - } - return false; - } - - /** * Get whether to display the guts or not, * default value is true @@ -677,12 +665,30 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } public Region getMergedRegionAt(int index) { - CTMergeCell ctMergeCell = getMergedCells().getMergeCellArray(index); + CTMergeCells ctMergeCells = worksheet.getMergeCells(); + if(ctMergeCells == null) throw new IllegalStateException("This worksheet does not contain merged regions"); + + CTMergeCell ctMergeCell = ctMergeCells.getMergeCellArray(index); return new Region(ctMergeCell.getRef()); } + /** + * @return the merged region at the specified index + */ + public CellRangeAddress getMergedRegion(int index) { + CTMergeCells ctMergeCells = worksheet.getMergeCells(); + if(ctMergeCells == null) throw new IllegalStateException("This worksheet does not contain merged regions"); + + CTMergeCell ctMergeCell = ctMergeCells.getMergeCellArray(index); + String ref = ctMergeCell.getRef(); + CellReference cell1 = new CellReference(ref.substring(0, ref.indexOf(":"))); + CellReference cell2 = new CellReference(ref.substring(ref.indexOf(":") + 1)); + return new CellRangeAddress(cell1.getRow(), cell2.getRow(), cell1.getCol(), cell2.getCol()); + } + public int getNumMergedRegions() { - return getMergedCells().sizeOfMergeCellArray(); + CTMergeCells ctMergeCells = worksheet.getMergeCells(); + return ctMergeCells == null ? 0 : ctMergeCells.sizeOfMergeCellArray(); } public int getNumHyperlinks() { @@ -1040,17 +1046,24 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } + /** + * Removes a merged region of cells (hence letting them free) + * + * @param index of the region to unmerge + */ public void removeMergedRegion(int index) { - CTMergeCell[] mergeCellsArray = new CTMergeCell[getMergedCells().sizeOfMergeCellArray() - 1]; - for (int i = 0 ; i < getMergedCells().sizeOfMergeCellArray() ; i++) { + CTMergeCells ctMergeCells = worksheet.getMergeCells(); + + CTMergeCell[] mergeCellsArray = new CTMergeCell[ctMergeCells.sizeOfMergeCellArray() - 1]; + for (int i = 0 ; i < ctMergeCells.sizeOfMergeCellArray() ; i++) { if (i < index) { - mergeCellsArray[i] = getMergedCells().getMergeCellArray(i); + mergeCellsArray[i] = ctMergeCells.getMergeCellArray(i); } else if (i > index) { - mergeCellsArray[i - 1] = getMergedCells().getMergeCellArray(i); + mergeCellsArray[i - 1] = ctMergeCells.getMergeCellArray(i); } } - getMergedCells().setMergeCellArray(mergeCellsArray); + ctMergeCells.setMergeCellArray(mergeCellsArray); } public void removeRow(Row row) { @@ -1192,15 +1205,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { } - public void setDialog(boolean b) { - if(b && dialogsheet == null){ - CTDialogsheet dialogSheet = CTDialogsheet.Factory.newInstance(); - dialogsheet = dialogSheet; - }else{ - dialogsheet = null; - } - } - /** * Sets the flag indicating whether this sheet should display formulas. * @@ -1623,19 +1627,6 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { return sheetComments; } - private void addNewMergeCell(Region region) { - ctMergeCells = getMergedCells(); - CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell(); - ctMergeCell.setRef(region.getRegionRef()); - } - - private CTMergeCells getMergedCells() { - if (ctMergeCells == null) { - ctMergeCells = worksheet.addNewMergeCells(); - } - return ctMergeCells; - } - private CTPageSetUpPr getSheetTypePageSetUpPr() { CTSheetPr sheetPr = getSheetTypeSheetPr(); return sheetPr.isSetPageSetUpPr() ? sheetPr.getPageSetUpPr() : sheetPr.addNewPageSetUpPr(); @@ -1666,10 +1657,12 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet { if(worksheet.getColsArray().length == 1) { CTCols col = worksheet.getColsArray(0); - if(col.getColArray().length == 0) { + CTCol[] cols = col.getColArray(); + if(cols.length == 0) { worksheet.setColsArray(null); } } + // Now re-generate our CTHyperlinks, if needed if(hyperlinks.size() > 0) { if(worksheet.getHyperlinks() == null) { diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 105eea28c7..eeb8b37a7a 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -51,6 +51,11 @@ import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelations */ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable { + /** + * Width of one character of the default font in pixels. Same for Calibry and Arial. + */ + public static final float DEFAULT_CHARACTER_WIDTH = 7.0017f; + /** * The underlying XML bean */ @@ -140,7 +145,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable Short.MAX_VALUE) { throw new IllegalArgumentException("Sheet number ["+sheetNumber+"]is not valid "); } @@ -939,7 +933,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable 0) { + int pos = 0; + for (int j = 0; j < rt.numFormattingRuns(); j++) { + XSSFFont fnt = rt.getFontOfFormattingRun(j); + if (fnt != null) { + int len = rt.getLengthOfFormattingRun(j); + copyAttributes(fnt, str, pos, pos + len); + pos += len; + } + } + } + + layout = new TextLayout(str.getIterator(), frc); + if(style.getRotation() != 0){ + /* + * Transform the text using a scale so that it's height is increased by a multiple of the leading, + * and then rotate the text before computing the bounds. The scale results in some whitespace around + * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but + * is added by the standard Excel autosize. + */ + AffineTransform trans = new AffineTransform(); + trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0)); + trans.concatenate( + AffineTransform.getScaleInstance(1, fontHeightMultiple) + ); + width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention()); + } else { + width = Math.max(width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention()); + } + } + } else { + String sval = null; + if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { + String format = style.getDataFormatString().replaceAll("\"", ""); + double value = cell.getNumericCellValue(); + try { + NumberFormat fmt; + if ("General".equals(format)) + sval = "" + value; + else + { + fmt = new DecimalFormat(format); + sval = fmt.format(value); + } + } catch (Exception e) { + sval = "" + value; + } + } else if (cell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) { + sval = String.valueOf(cell.getBooleanCellValue()); + } + if(sval != null) { + String txt = sval + defaultChar; + str = new AttributedString(txt); + copyAttributes(font, str, 0, txt.length()); + + layout = new TextLayout(str.getIterator(), frc); + if(style.getRotation() != 0){ + /* + * Transform the text using a scale so that it's height is increased by a multiple of the leading, + * and then rotate the text before computing the bounds. The scale results in some whitespace around + * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but + * is added by the standard Excel autosize. + */ + AffineTransform trans = new AffineTransform(); + trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0)); + trans.concatenate( + AffineTransform.getScaleInstance(1, fontHeightMultiple) + ); + width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention()); + } else { + width = Math.max(width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention()); + } + } + } + + } + return width; + } + + /** + * Copy text attributes from the supplied HSSFFont to Java2D AttributedString + */ + private static void copyAttributes(XSSFFont font, AttributedString str, int startIdx, int endIdx) { + str.addAttribute(TextAttribute.FAMILY, font.getFontName(), startIdx, endIdx); + str.addAttribute(TextAttribute.SIZE, new Float(font.getFontHeightInPoints())); + if (font.getBoldweight() == XSSFFont.BOLDWEIGHT_BOLD) str.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD, startIdx, endIdx); + if (font.getItalic() ) str.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE, startIdx, endIdx); + if (font.getUnderline() == XSSFFont.U_SINGLE ) str.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON, startIdx, endIdx); + } + + private static boolean containsCell(CellRangeAddress cr, int rowIx, int colIx) { + if (cr.getFirstRow() <= rowIx && cr.getLastRow() >= rowIx + && cr.getFirstColumn() <= colIx && cr.getLastColumn() >= colIx) + { + return true; + } + return false; + } + } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java index f5d623c0fe..877cf34ec2 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java @@ -17,7 +17,6 @@ package org.apache.poi.xssf.usermodel; -import java.util.ArrayList; import java.util.Calendar; import java.util.Date; @@ -28,21 +27,13 @@ import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.Comment; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.XSSFTestDataSamples; -import org.apache.poi.xssf.model.CommentsTable; -import org.apache.poi.xssf.model.SharedStringSource; -import org.apache.poi.xssf.model.SharedStringsTable; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType; @@ -291,7 +282,8 @@ public final class TestXSSFCell extends TestCase { CellStyle cs = workbook.createCellStyle(); assertNotNull(cs); - + assertTrue(cs.getIndex() > 0); + assertNotNull(creationHelper); assertNotNull(creationHelper.createDataFormat()); @@ -299,9 +291,10 @@ public final class TestXSSFCell extends TestCase { creationHelper.createDataFormat().getFormat("yyyy/mm/dd") ); Cell cell = sheet.createRow(0).createCell((short)0); + assertNotNull(cell.getCellStyle()); + assertEquals(0, cell.getCellStyle().getIndex()); cell.setCellValue(new Date(654321)); - assertNull(cell.getCellStyle()); cell.setCellStyle(cs); assertEquals(new Date(654321), cell.getDateCellValue()); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java index 87aec4bc85..3e37019f47 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCellStyle.java @@ -532,10 +532,10 @@ public class TestXSSFCellStyle extends TestCase { public void testGetSetIndent() { assertEquals((short)0, cellStyle.getIndention()); - cellXf.getAlignment().setIndent(3); + cellStyle.setIndention((short)3); assertEquals((short)3, cellStyle.getIndention()); cellStyle.setIndention((short) 13); - assertEquals((short)13, cellXf.getAlignment().getIndent()); + assertEquals((short)13, cellStyle.getIndention()); } public void testGetSetAlignement() { @@ -576,10 +576,10 @@ public class TestXSSFCellStyle extends TestCase { public void testGetSetWrapText() { assertFalse(cellStyle.getWrapText()); - cellXf.getAlignment().setWrapText(true); + cellStyle.setWrapText(true); assertTrue(cellStyle.getWrapText()); cellStyle.setWrapText(false); - assertFalse(cellXf.getAlignment().getWrapText()); + assertFalse(cellStyle.getWrapText()); } /** diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDialogSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDialogSheet.java index 663cc5a4aa..e57d8825e5 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDialogSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDialogSheet.java @@ -36,9 +36,7 @@ public class TestXSSFDialogSheet extends TestCase { public void testGetDialog() { XSSFWorkbook workbook = new XSSFWorkbook(); - XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); - assertFalse(sheet.getDialog()); - XSSFSheet dialogsheet = (XSSFSheet) workbook.createDialogsheet("Dialogsheet 1", null); + XSSFDialogsheet dialogsheet = workbook.createDialogsheet("Dialogsheet 1", null); assertTrue(dialogsheet.getDialog()); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRow.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRow.java index 86352bb123..fc968be6bc 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRow.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRow.java @@ -204,12 +204,12 @@ public final class TestXSSFRow extends TestCase { assertEquals(Cell.CELL_TYPE_NUMERIC, row.getCell(5, XSSFRow.CREATE_NULL_AS_BLANK).getCellType()); // Check created ones get the right column - assertEquals((short)0, row.getCell(0, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum()); - assertEquals((short)1, row.getCell(1, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum()); - assertEquals((short)2, row.getCell(2, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum()); - assertEquals((short)3, row.getCell(3, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum()); - assertEquals((short)4, row.getCell(4, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum()); - assertEquals((short)5, row.getCell(5, XSSFRow.CREATE_NULL_AS_BLANK).getCellNum()); + assertEquals((short)0, row.getCell(0, XSSFRow.CREATE_NULL_AS_BLANK).getColumnIndex()); + assertEquals((short)1, row.getCell(1, XSSFRow.CREATE_NULL_AS_BLANK).getColumnIndex()); + assertEquals((short)2, row.getCell(2, XSSFRow.CREATE_NULL_AS_BLANK).getColumnIndex()); + assertEquals((short)3, row.getCell(3, XSSFRow.CREATE_NULL_AS_BLANK).getColumnIndex()); + assertEquals((short)4, row.getCell(4, XSSFRow.CREATE_NULL_AS_BLANK).getColumnIndex()); + assertEquals((short)5, row.getCell(5, XSSFRow.CREATE_NULL_AS_BLANK).getColumnIndex()); } /** diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java index 882a816ee8..4aa3c8d9c9 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java @@ -32,7 +32,6 @@ import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; @@ -305,7 +304,7 @@ public class TestXSSFSheet extends TestCase { XSSFOddFooter ftr; // Sheet 1 has a header with center and right text - XSSFSheet s1 = (XSSFSheet)workbook.getSheetAt(0); + XSSFSheet s1 = workbook.getSheetAt(0); assertNotNull(s1.getHeader()); assertNotNull(s1.getFooter()); hdr = (XSSFOddHeader)s1.getHeader(); @@ -324,7 +323,7 @@ public class TestXSSFSheet extends TestCase { // Sheet 2 has a footer, but it's empty - XSSFSheet s2 = (XSSFSheet)workbook.getSheetAt(1); + XSSFSheet s2 = workbook.getSheetAt(1); assertNotNull(s2.getHeader()); assertNotNull(s2.getFooter()); hdr = (XSSFOddHeader)s2.getHeader(); @@ -359,7 +358,7 @@ public class TestXSSFSheet extends TestCase { public void testGetAllHeadersFooters() { XSSFWorkbook workbook = new XSSFWorkbook(); - XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); + XSSFSheet sheet = workbook.createSheet("Sheet 1"); assertNotNull(sheet.getOddFooter()); assertNotNull(sheet.getEvenFooter()); assertNotNull(sheet.getFirstFooter()); @@ -447,28 +446,19 @@ public class TestXSSFSheet extends TestCase { public void testAutoSizeColumn() { XSSFWorkbook workbook = new XSSFWorkbook(); - XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); + XSSFSheet sheet = workbook.createSheet("Sheet 1"); + sheet.createRow(0).createCell(13).setCellValue("test"); + + sheet.autoSizeColumn((short)13); + ColumnHelper columnHelper = sheet.getColumnHelper(); CTCol col = columnHelper.getColumn(13, false); - assertNull(col); - sheet.autoSizeColumn((short)13); - col = columnHelper.getColumn(13, false); - assertNotNull(col); - assertTrue(col.getBestFit()); - } - - public void testGetDialog() { - XSSFWorkbook workbook = new XSSFWorkbook(); - XSSFSheet sheet = workbook.createSheet("Sheet 1"); - assertFalse(sheet.getDialog()); - XSSFSheet dialogsheet = workbook.createDialogsheet("Dialogsheet 1", null); - assertTrue(dialogsheet.getDialog()); - + assertTrue(col.getBestFit()); } public void testGetSetHorizontallyCentered() { XSSFWorkbook workbook = new XSSFWorkbook(); - XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); + XSSFSheet sheet = workbook.createSheet("Sheet 1"); assertFalse(sheet.getHorizontallyCenter()); sheet.setHorizontallyCenter(true); assertTrue(sheet.getHorizontallyCenter()); @@ -478,7 +468,7 @@ public class TestXSSFSheet extends TestCase { public void testGetSetVerticallyCentered() { XSSFWorkbook workbook = new XSSFWorkbook(); - XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); + XSSFSheet sheet = workbook.createSheet("Sheet 1"); assertFalse(sheet.getVerticallyCenter()); sheet.setVerticallyCenter(true); assertTrue(sheet.getVerticallyCenter()); @@ -488,7 +478,7 @@ public class TestXSSFSheet extends TestCase { public void testIsSetPrintGridlines() { XSSFWorkbook workbook = new XSSFWorkbook(); - XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); + XSSFSheet sheet = workbook.createSheet("Sheet 1"); assertFalse(sheet.isPrintGridlines()); sheet.setPrintGridlines(true); assertTrue(sheet.isPrintGridlines()); @@ -496,7 +486,7 @@ public class TestXSSFSheet extends TestCase { public void testIsSetDisplayFormulas() { XSSFWorkbook workbook = new XSSFWorkbook(); - XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); + XSSFSheet sheet = workbook.createSheet("Sheet 1"); assertFalse(sheet.isDisplayFormulas()); sheet.setDisplayFormulas(true); assertTrue(sheet.isDisplayFormulas()); @@ -504,7 +494,7 @@ public class TestXSSFSheet extends TestCase { public void testIsSetDisplayGridLines() { XSSFWorkbook workbook = new XSSFWorkbook(); - XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); + XSSFSheet sheet = workbook.createSheet("Sheet 1"); assertTrue(sheet.isDisplayGridlines()); sheet.setDisplayGridlines(false); assertFalse(sheet.isDisplayGridlines()); @@ -512,7 +502,7 @@ public class TestXSSFSheet extends TestCase { public void testIsSetDisplayGuts() { XSSFWorkbook workbook = new XSSFWorkbook(); - XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); + XSSFSheet sheet = workbook.createSheet("Sheet 1"); assertTrue(sheet.getDisplayGuts()); sheet.setDisplayGuts(false); assertFalse(sheet.getDisplayGuts()); @@ -520,7 +510,7 @@ public class TestXSSFSheet extends TestCase { public void testIsSetDisplayRowColHeadings() { XSSFWorkbook workbook = new XSSFWorkbook(); - XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); + XSSFSheet sheet = workbook.createSheet("Sheet 1"); assertTrue(sheet.isDisplayRowColHeadings()); sheet.setDisplayRowColHeadings(false); assertFalse(sheet.isDisplayRowColHeadings()); @@ -528,7 +518,7 @@ public class TestXSSFSheet extends TestCase { public void testGetScenarioProtect() { XSSFWorkbook workbook = new XSSFWorkbook(); - XSSFSheet sheet = (XSSFSheet) workbook.createSheet("Sheet 1"); + XSSFSheet sheet = workbook.createSheet("Sheet 1"); assertFalse(sheet.getScenarioProtect()); } @@ -720,8 +710,8 @@ public class TestXSSFSheet extends TestCase { sheet.setDefaultColumnStyle((short) 3, cellStyle); assertEquals(1, ctWorksheet.getColsArray(0).getColArray(0).getStyle()); - XSSFRow row = (XSSFRow) sheet.createRow(0); - XSSFCell cell = (XSSFCell) sheet.getRow(0).createCell(3); + XSSFRow row = sheet.createRow(0); + XSSFCell cell = sheet.getRow(0).createCell(3); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index e4157c7d08..beaafb3e85 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -322,8 +322,6 @@ public final class TestXSSFWorkbook extends TestCase { ((XSSFFont)font).setBold(true); font.setUnderline(Font.U_DOUBLE); StylesTable styleSource=new StylesTable(); - long index=styleSource.putFont(font); - workbook.setStylesSource(styleSource); fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, IndexedColors.BLACK.getIndex(), (short)15, "Calibri", false, false, Font.SS_NONE, Font.U_DOUBLE); assertNull(fontFind); } @@ -399,7 +397,7 @@ public final class TestXSSFWorkbook extends TestCase { public void testSetDisplayedTab(){ XSSFWorkbook workbook = new XSSFWorkbook(); - workbook.setFirstVisibleTab(new Integer(1).shortValue()); + workbook.setFirstVisibleTab(1); short i = (short) workbook.getFirstVisibleTab(); //0 (defualt value) is not longer set assertNotSame(0, i);