* @author Yegor Kozlov cell comments support
*/
public class HSSFCell implements Cell {
- /** Numeric Cell type (0) @see #setCellType(int) @see #getCellType() */
- public final static int CELL_TYPE_NUMERIC = 0;
- /** String Cell type (1) @see #setCellType(int) @see #getCellType() */
- public final static int CELL_TYPE_STRING = 1;
- /** Formula Cell type (2) @see #setCellType(int) @see #getCellType() */
- public final static int CELL_TYPE_FORMULA = 2;
- /** Blank Cell type (3) @see #setCellType(int) @see #getCellType() */
- public final static int CELL_TYPE_BLANK = 3;
- /** Boolean Cell type (4) @see #setCellType(int) @see #getCellType() */
- public final static int CELL_TYPE_BOOLEAN = 4;
- /** Error Cell type (5) @see #setCellType(int) @see #getCellType() */
- public final static int CELL_TYPE_ERROR = 5;
private static final String FILE_FORMAT_NAME = "BIFF8";
/**
private short index = 0;
private Workbook workbook = null;
- /**
- * general (normal) horizontal alignment
- */
-
- public final static short ALIGN_GENERAL = 0x0;
-
- /**
- * left-justified horizontal alignment
- */
-
- public final static short ALIGN_LEFT = 0x1;
-
- /**
- * center horizontal alignment
- */
-
- public final static short ALIGN_CENTER = 0x2;
-
- /**
- * right-justified horizontal alignment
- */
-
- public final static short ALIGN_RIGHT = 0x3;
-
- /**
- * fill? horizontal alignment
- */
-
- public final static short ALIGN_FILL = 0x4;
-
- /**
- * justified horizontal alignment
- */
-
- public final static short ALIGN_JUSTIFY = 0x5;
-
- /**
- * center-selection? horizontal alignment
- */
-
- public final static short ALIGN_CENTER_SELECTION = 0x6;
-
- /**
- * top-aligned vertical alignment
- */
-
- public final static short VERTICAL_TOP = 0x0;
-
- /**
- * center-aligned vertical alignment
- */
-
- public final static short VERTICAL_CENTER = 0x1;
-
- /**
- * bottom-aligned vertical alignment
- */
-
- public final static short VERTICAL_BOTTOM = 0x2;
-
- /**
- * vertically justified vertical alignment
- */
-
- public final static short VERTICAL_JUSTIFY = 0x3;
-
- /**
- * No border
- */
-
- public final static short BORDER_NONE = 0x0;
-
- /**
- * Thin border
- */
-
- public final static short BORDER_THIN = 0x1;
-
- /**
- * Medium border
- */
-
- public final static short BORDER_MEDIUM = 0x2;
-
- /**
- * dash border
- */
-
- public final static short BORDER_DASHED = 0x3;
-
- /**
- * dot border
- */
-
- public final static short BORDER_HAIR = 0x4;
-
- /**
- * Thick border
- */
-
- public final static short BORDER_THICK = 0x5;
-
- /**
- * double-line border
- */
-
- public final static short BORDER_DOUBLE = 0x6;
-
- /**
- * hair-line border
- */
-
- public final static short BORDER_DOTTED = 0x7;
-
- /**
- * Medium dashed border
- */
-
- public final static short BORDER_MEDIUM_DASHED = 0x8;
-
- /**
- * dash-dot border
- */
-
- public final static short BORDER_DASH_DOT = 0x9;
-
- /**
- * medium dash-dot border
- */
-
- public final static short BORDER_MEDIUM_DASH_DOT = 0xA;
-
- /**
- * dash-dot-dot border
- */
-
- public final static short BORDER_DASH_DOT_DOT = 0xB;
-
- /**
- * medium dash-dot-dot border
- */
-
- public final static short BORDER_MEDIUM_DASH_DOT_DOT = 0xC;
-
- /**
- * slanted dash-dot border
- */
-
- public final static short BORDER_SLANTED_DASH_DOT = 0xD;
-
- /** No background */
- public final static short NO_FILL = 0 ;
- /** Solidly filled */
- public final static short SOLID_FOREGROUND = 1 ;
- /** Small fine dots */
- public final static short FINE_DOTS = 2 ;
- /** Wide dots */
- public final static short ALT_BARS = 3 ;
- /** Sparse dots */
- public final static short SPARSE_DOTS = 4 ;
- /** Thick horizontal bands */
- public final static short THICK_HORZ_BANDS = 5 ;
- /** Thick vertical bands */
- public final static short THICK_VERT_BANDS = 6 ;
- /** Thick backward facing diagonals */
- public final static short THICK_BACKWARD_DIAG = 7 ;
- /** Thick forward facing diagonals */
- public final static short THICK_FORWARD_DIAG = 8 ;
- /** Large spots */
- public final static short BIG_SPOTS = 9 ;
- /** Brick-like layout */
- public final static short BRICKS = 10 ;
- /** Thin horizontal bands */
- public final static short THIN_HORZ_BANDS = 11 ;
- /** Thin vertical bands */
- public final static short THIN_VERT_BANDS = 12 ;
- /** Thin backward diagonal */
- public final static short THIN_BACKWARD_DIAG = 13 ;
- /** Thin forward diagonal */
- public final static short THIN_FORWARD_DIAG = 14 ;
- /** Squares */
- public final static short SQUARES = 15 ;
- /** Diamonds */
- public final static short DIAMONDS = 16 ;
- /** Less Dots */
- public final static short LESS_DOTS = 17 ;
- /** Least Dots */
- public final static short LEAST_DOTS = 18 ;
-
/** Creates new HSSFCellStyle why would you want to do this?? */
protected HSSFCellStyle(short index, ExtendedFormatRecord rec, HSSFWorkbook workbook)
*/
public final class HSSFFont implements Font {
+ /**
+ * Arial font
+ */
+
+ public final static String FONT_ARIAL = "Arial";
+
+
private FontRecord font;
private short index;
* @throws IllegalArgumentException if the name is invalid or the name already exists (case-insensitive)
*/
public void setNameName(String nameName){
+ validateName(nameName);
+
Workbook wb = _book.getWorkbook();
_definedNameRec.setNameText(nameName);
}
}
+ private static void validateName(String name){
+ if(name.length() == 0) throw new IllegalArgumentException("Name cannot be blank");
+
+ char c = name.charAt(0);
+ if(!(c == '_' || Character.isLetter(c)) || name.indexOf(' ') != -1) {
+ throw new IllegalArgumentException("Invalid name: '"+name+"'; Names must begin with a letter or underscore and not contain spaces");
+ }
+ }
+
/**
* Returns the formula that the name is defined to refer to.
*
public interface Font {
-
- /**
- * Arial font
- */
-
- public final static String FONT_ARIAL = "Arial";
-
/**
* Normal boldness (not bold)
*/
/**
* set the name for the font (i.e. Arial)
* @param name String representing the name of the font to use
- * @see #FONT_ARIAL
*/
void setFontName(String name);
/**
* get the name for the font (i.e. Arial)
* @return String representing the name of the font to use
- * @see #FONT_ARIAL
*/
String getFontName();
* <code>resize(2.0)</code> resizes to 200% of the original.\r
*/\r
void resize(double scale);\r
+\r
+ ClientAnchor getPreferredSize();\r
+\r
}\r
import org.apache.poi.ss.usermodel.FontFamily;
import org.apache.poi.ss.usermodel.FontScheme;
import org.apache.poi.ss.usermodel.BuiltinFormats;
+import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
*/
public XSSFFont findFont(short boldWeight, short color, short fontHeight, String name, boolean italic, boolean strikeout, short typeOffset, byte underline) {
for (XSSFFont font : fonts) {
- if ( (font.getBold() == (boldWeight == XSSFFont.BOLDWEIGHT_BOLD))
+ if ( (font.getBoldweight() == boldWeight)
&& font.getColor() == color
- && font.getFontHeightInPoints() == fontHeight
+ && font.getFontHeight() == fontHeight
&& font.getFontName().equals(name)
&& font.getItalic() == italic
&& font.getStrikeout() == strikeout
protected XSSFFont() {
this.ctFont = CTFont.Factory.newInstance();
setFontName(DEFAULT_FONT_NAME);
- setFontHeight(DEFAULT_FONT_SIZE);
+ setFontHeight((double)DEFAULT_FONT_SIZE);
}
/**
CTFontSize size = ctFont.sizeOfSzArray() == 0 ? null : ctFont.getSzArray(0);
if (size != null) {
double fontHeight = size.getVal();
- return (short) fontHeight;
+ return (short)(fontHeight*20);
} else
- return DEFAULT_FONT_SIZE;
+ return (short)(DEFAULT_FONT_SIZE*20);
}
/**
* @see #getFontHeight()
*/
public short getFontHeightInPoints() {
- CTFontSize size = ctFont.sizeOfSzArray() == 0 ? null : ctFont.getSzArray(0);
- if (size != null) {
- double fontHeight = size.getVal();
- return (short) fontHeight;
- } else
- return DEFAULT_FONT_SIZE;
+ return (short)(getFontHeight()/20);
}
/**
* @param height - height in points
*/
public void setFontHeight(short height) {
- setFontHeight((double) height);
+ setFontHeight((double) height/20);
}
/**
* @link #setFontHeight
*/
public void setFontHeightInPoints(short height) {
- setFontHeight(height);
+ setFontHeight((double)height);
}
/**
}
private static void validateName(String name){
+ if(name.length() == 0) throw new IllegalArgumentException("Name cannot be blank");
char c = name.charAt(0);
if(!(c == '_' || Character.isLetter(c)) || name.indexOf(' ') != -1) {
throw new IllegalArgumentException("Invalid name: '"+name+"'; Names must begin with a letter or underscore and not contain spaces");
*/
public XSSFSheet createSheet() {
String sheetname = "Sheet" + (sheets.size());
+ int idx = 0;
+ while(getSheet(sheetname) != null) {
+ sheetname = "Sheet" + idx;
+ idx++;
+ }
return createSheet(sheetname);
}
import org.apache.poi.ss.ITestDataProvider;\r
import org.apache.poi.ss.usermodel.Workbook;\r
import org.apache.poi.xssf.usermodel.XSSFWorkbook;\r
+import org.apache.poi.hssf.HSSFTestDataSamples;\r
\r
/**\r
* @author Yegor Kozlov\r
return new XSSFWorkbook();\r
}\r
\r
+ public byte[] getTestDataFileContent(String fileName) {\r
+ return HSSFTestDataSamples.getTestDataFileContent(fileName);\r
+ }\r
+\r
private XSSFITestDataProvider(){}\r
private static XSSFITestDataProvider inst = new XSSFITestDataProvider();\r
public static XSSFITestDataProvider getInstance(){\r
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.xssf.XSSFTestDataSamples;
+import org.apache.poi.xssf.XSSFITestDataProvider;
+import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
-public class TestXSSFBugs extends TestCase {
- private String getFilePath(String file) {
- File xml = new File(
- System.getProperty("HSSF.testdata.path") +
- File.separator + file
- );
- assertTrue(xml.exists());
+public class TestXSSFBugs extends BaseTestBugzillaIssues {
+ @Override
+ protected XSSFITestDataProvider getTestDataProvider(){
+ return XSSFITestDataProvider.getInstance();
+ }
- return xml.toString();
+ /**
+ * test writing a file with large number of unique strings,
+ * open resulting file in Excel to check results!
+ */
+ public void test15375_2() {
+ baseTest15375(1000);
}
/**
* the wrong sheet name
*/
public void test45430() throws Exception {
- XSSFWorkbook wb = new XSSFWorkbook(getFilePath("45430.xlsx"));
+ XSSFWorkbook wb = getTestDataProvider().openSampleWorkbook("45430.xlsx");
assertFalse(wb.isMacroEnabled());
assertEquals(3, wb.getNumberOfNames());
* We should carry vba macros over after save
*/
public void test45431() throws Exception {
- OPCPackage pkg = OPCPackage.open(getFilePath("45431.xlsm"));
- XSSFWorkbook wb = new XSSFWorkbook(pkg);
+ XSSFWorkbook wb = getTestDataProvider().openSampleWorkbook("45431.xlsm");
+ OPCPackage pkg = wb.getPackage();
assertTrue(wb.isMacroEnabled());
// Check the various macro related bits can be found
PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
);
assertNotNull(drw);
-
- // For testing with excel
-// FileOutputStream fout = new FileOutputStream("/tmp/foo.xlsm");
-// nwb.write(fout);
-// fout.close();
}
}
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.XSSFTestDataSamples;
+import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBooleanProperty;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STUnderlineValues;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STVerticalAlignRun;
-public final class TestXSSFFont extends TestCase{
+public final class TestXSSFFont extends BaseTestFont{
+
+ @Override
+ protected XSSFITestDataProvider getTestDataProvider(){
+ return XSSFITestDataProvider.getInstance();
+ }
+
+ public void testDefaultFont() {
+ baseTestDefaultFont("Calibri", (short)220, IndexedColors.BLACK.getIndex());
+ }
public void testConstructor() {
XSSFFont xssfFont=new XSSFFont();
ctFont.setSzArray(0,size);
XSSFFont xssfFont=new XSSFFont(ctFont);
- assertEquals(11,xssfFont.getFontHeight());
+ assertEquals(11,xssfFont.getFontHeightInPoints());
- xssfFont.setFontHeight((short)20);
+ xssfFont.setFontHeight(20);
assertEquals(20.0, ctFont.getSzArray(0).getVal(), 0.0);
}
font.setTypeOffset(XSSFFont.SS_SUPER);
assertEquals(STVerticalAlignRun.SUPERSCRIPT,ctFont.getVertAlignArray(0).getVal());
}
-
- /**
- * Tests that we can define fonts to a new
- * file, save, load, and still see them
- * @throws Exception
- */
- public void testCreateSave() {
- XSSFWorkbook wb = new XSSFWorkbook();
- XSSFSheet s1 = wb.createSheet();
- Row r1 = s1.createRow(0);
- Cell r1c1 = r1.createCell(0);
- r1c1.setCellValue(2.2);
-
- assertEquals(1, wb.getNumberOfFonts());
-
- XSSFFont font=wb.createFont();
- font.setBold(true);
- font.setStrikeout(true);
- font.setColor(IndexedColors.YELLOW.getIndex());
- font.setFontName("Courier");
- wb.createCellStyle().setFont(font);
- assertEquals(2, wb.getNumberOfFonts());
-
- CellStyle cellStyleTitle=wb.createCellStyle();
- cellStyleTitle.setFont(font);
- r1c1.setCellStyle(cellStyleTitle);
-
- // Save and re-load
- wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
- s1 = wb.getSheetAt(0);
-
- assertEquals(2, wb.getNumberOfFonts());
- short idx = s1.getRow(0).getCell(0).getCellStyle().getFontIndex();
- Font fnt = wb.getFontAt(idx);
- assertNotNull(fnt);
- assertEquals(IndexedColors.YELLOW.getIndex(), fnt.getColor());
- assertEquals("Courier", fnt.getFontName());
-
- // Now add an orphaned one
- XSSFFont font2 = wb.createFont();
- font2.setItalic(true);
- font2.setFontHeightInPoints((short)15);
- wb.createCellStyle().setFont(font2);
- assertEquals(3, wb.getNumberOfFonts());
-
- // Save and re-load
- wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
- s1 = wb.getSheetAt(0);
-
- assertEquals(3, wb.getNumberOfFonts());
- assertNotNull(wb.getFontAt((short)1));
- assertNotNull(wb.getFontAt((short)2));
-
- assertEquals(15, wb.getFontAt((short)2).getFontHeightInPoints());
- assertEquals(true, wb.getFontAt((short)2).getItalic());
- }
-
- public void testXSSFFont() {
- XSSFWorkbook workbook=new XSSFWorkbook();
- //Font font1=workbook.createFont();
-
- Sheet sheet=workbook.createSheet("sheet 1 - test font");
-
-
- Row row=sheet.createRow(0);
- Cell cell=row.createCell(0);
- cell.setCellValue(new XSSFRichTextString("XSSFFont test example file"));
- XSSFFont font=new XSSFFont();
- font.setBold(true);
- font.setFontHeightInPoints((short)22);
- font.setColor(IndexedColors.BLUE.getIndex());
- font.setFontName("Verdana");
- CellStyle cellStyleTitle=workbook.createCellStyle();
- cellStyleTitle.setFont(font);
- cell.setCellStyle(cellStyleTitle);
-
-
- row=sheet.createRow(3);
- XSSFFont font1=new XSSFFont();
- font1.setBold(true);
- font1.setItalic(true);
- font1.setFontHeightInPoints((short)18);
- font1.setColor(IndexedColors.RED.getIndex());
- font1.setFontName("Arial");
- CellStyle cellStyle1=workbook.createCellStyle();
- cellStyle1.setFont(font1);
-
- Cell cell1=row.createCell(0);
- cell1.setCellValue(new XSSFRichTextString("red bold 18pt italic Arial"));
- cell1.setCellStyle(cellStyle1);
-
-
- row=sheet.createRow(4);
- Font font2=new XSSFFont();
- font2.setFontHeight((short)1);
- font2.setFontName("Courier");
- font2.setColor(IndexedColors.BLACK.getIndex());
- font2.setUnderline(Font.U_DOUBLE);
- CellStyle cellStyle2=workbook.createCellStyle();
- cellStyle2.setFont(font2);
-
- Cell cell2=row.createCell(0);
- cell2.setCellValue(new XSSFRichTextString("Something in courier underlined"));
- cell2.setCellStyle(cellStyle2);
-
-
- row=sheet.createRow(5);
- cell1=row.createCell(0);
- Font font3=new XSSFFont();
- font3.setFontHeightInPoints((short)9);
- font3.setFontName("Times");
- font3.setStrikeout(true);
- font3.setColor(IndexedColors.PINK.getIndex());
- CellStyle cellStyle3=workbook.createCellStyle();
- cellStyle3.setFont(font3);
-
- cell1.setCellValue(new XSSFRichTextString("pink italic Times 9pt strikeout!!!"));
- cell1.setCellStyle(cellStyle3);
-
- XSSFTestDataSamples.writeOutAndReadBack(workbook);
- }
-
- /**
- * Test that fonts get added properly
- *
- * @see org.apache.poi.hssf.usermodel.TestBugs#test45338()
- */
- public void test45338() {
- XSSFWorkbook wb = new XSSFWorkbook();
- assertEquals(1, wb.getNumberOfFonts());
-
- XSSFSheet s = wb.createSheet();
- s.createRow(0);
- s.createRow(1);
- s.getRow(0).createCell(0);
- s.getRow(1).createCell(0);
-
- assertEquals(1, wb.getNumberOfFonts());
-
- XSSFFont f1 = wb.getFontAt((short)0);
- assertEquals(XSSFFont.BOLDWEIGHT_NORMAL, f1.getBoldweight());
-
- // Check that asking for the same font
- // multiple times gives you the same thing.
- // Otherwise, our tests wouldn't work!
- assertEquals(wb.getFontAt((short)0), wb.getFontAt((short)0));
-
- // Look for a new font we have
- // yet to add
- assertNull(
- wb.findFont(
- (short)11, (short)123, (short)22,
- "Thingy", false, true, (short)2, (byte)2
- )
- );
-
- XSSFFont nf = wb.createFont();
- assertEquals(2, wb.getNumberOfFonts());
-
- assertEquals(1, nf.getIndex());
- assertEquals(nf, wb.getFontAt((short)1));
-
- nf.setBoldweight((short)11);
- nf.setColor((short)123);
- nf.setFontHeight((short)22);
- nf.setFontName("Thingy");
- nf.setItalic(false);
- nf.setStrikeout(true);
- nf.setTypeOffset((short)2);
- nf.setUnderline((byte)2);
-
- assertEquals(2, wb.getNumberOfFonts());
- assertEquals(nf, wb.getFontAt((short)1));
-
- assertEquals(wb.getFontAt((short)1), wb.getFontAt((short)1));
- assertTrue(wb.getFontAt((short)0) != wb.getFontAt((short)1));
-
- // Find it now
- assertNotNull(
- wb.findFont(
- (short)11, (short)123, (short)22,
- "Thingy", false, true, (short)2, (byte)2
- )
- );
- assertEquals(
- 1,
- wb.findFont(
- (short)11, (short)123, (short)22,
- "Thingy", false, true, (short)2, (byte)2
- ).getIndex()
- );
- assertEquals(nf,
- wb.findFont(
- (short)11, (short)123, (short)22,
- "Thingy", false, true, (short)2, (byte)2
- )
- );
- }
-
}
return XSSFITestDataProvider.getInstance();\r
}\r
\r
+ //TODO combine testRepeatingRowsAndColums() for HSSF and XSSF\r
+ public void testRepeatingRowsAndColums() {\r
+ // First test that setting RR&C for same sheet more than once only creates a\r
+ // single Print_Titles built-in record\r
+ XSSFWorkbook wb = getTestDataProvider().createWorkbook();\r
+ XSSFSheet sheet = wb.createSheet("FirstSheet");\r
+\r
+ // set repeating rows and columns twice for the first sheet\r
+ for (int i = 0; i < 2; i++) {\r
+ wb.setRepeatingRowsAndColumns(0, 0, 0, 0, 3);\r
+ //sheet.createFreezePane(0, 3);\r
+ }\r
+ assertEquals(1, wb.getNumberOfNames());\r
+ XSSFName nr1 = wb.getNameAt(0);\r
+\r
+ assertEquals(XSSFName.BUILTIN_PRINT_TITLE, nr1.getNameName());\r
+ assertEquals("'FirstSheet'!$A:$A,'FirstSheet'!$1:$4", nr1.getRefersToFormula());\r
+\r
+ // Save and re-open\r
+ XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb);\r
+\r
+ assertEquals(1, nwb.getNumberOfNames());\r
+ nr1 = nwb.getNameAt(0);\r
+\r
+ assertEquals(XSSFName.BUILTIN_PRINT_TITLE, nr1.getNameName());\r
+ assertEquals("'FirstSheet'!$A:$A,'FirstSheet'!$1:$4", nr1.getRefersToFormula());\r
+\r
+ // check that setting RR&C on a second sheet causes a new Print_Titles built-in\r
+ // name to be created\r
+ sheet = nwb.createSheet("SecondSheet");\r
+ nwb.setRepeatingRowsAndColumns(1, 1, 2, 0, 0);\r
+\r
+ assertEquals(2, nwb.getNumberOfNames());\r
+ XSSFName nr2 = nwb.getNameAt(1);\r
+\r
+ assertEquals(XSSFName.BUILTIN_PRINT_TITLE, nr2.getNameName());\r
+ assertEquals("'SecondSheet'!$B:$C,'SecondSheet'!$1:$1", nr2.getRefersToFormula());\r
+\r
+ nwb.setRepeatingRowsAndColumns(1, -1, -1, -1, -1);\r
+ }\r
+\r
+\r
}\r
\r
import junit.framework.TestCase;\r
import org.apache.poi.ss.usermodel.ClientAnchor;\r
+import org.apache.poi.ss.usermodel.BaseTestPicture;\r
+import org.apache.poi.xssf.XSSFITestDataProvider;\r
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor;\r
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.STEditAs;\r
\r
/**\r
* @author Yegor Kozlov\r
*/\r
-public class TestXSSFPicture extends TestCase {\r
+public class TestXSSFPicture extends BaseTestPicture {\r
+\r
+ @Override\r
+ protected XSSFITestDataProvider getTestDataProvider(){\r
+ return XSSFITestDataProvider.getInstance();\r
+ }\r
+\r
+ public void testResize() {\r
+ baseTestResize(new XSSFClientAnchor(0, 0, 504825, 85725, (short)0, 0, (short)1, 8));\r
+ }\r
+\r
\r
public void testCreate(){\r
XSSFWorkbook wb = new XSSFWorkbook();\r
import java.io.File;
import java.util.Iterator;
+
import junit.framework.TestCase;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
public class TestXSSFSheet extends BaseTestSheet {
@Override
- protected XSSFITestDataProvider getTestDataProvider(){
+ protected XSSFITestDataProvider getTestDataProvider() {
return XSSFITestDataProvider.getInstance();
}
baseTestGetSetMargin(new double[]{0.7, 0.7, 0.75, 0.75, 0.3, 0.3});
}
- public void testGetFirstLastRowNum() {
- Workbook workbook = getTestDataProvider().createWorkbook();
- Sheet sheet = workbook.createSheet("Sheet 1");
- Row row10 = sheet.createRow(9);
- Row row1 = sheet.createRow(0);
- Row row2 = sheet.createRow(1);
- assertEquals(0, sheet.getFirstRowNum());
- assertEquals(9, sheet.getLastRowNum());
- }
-
- public void testGetFooter() {
- XSSFWorkbook workbook = getTestDataProvider().createWorkbook();
- XSSFSheet sheet = workbook.createSheet("Sheet 1");
- assertNotNull(sheet.getFooter());
- sheet.getFooter().setCenter("test center footer");
- assertEquals("test center footer", sheet.getFooter().getCenter());
-
- // Default is odd footer
- assertNotNull(sheet.getOddFooter());
- assertEquals("test center footer", sheet.getOddFooter().getCenter());
- }
-
public void testExistingHeaderFooter() throws Exception {
- File xml = new File(
- System.getProperty("HSSF.testdata.path") +
- File.separator + "45540_classic_Header.xlsx"
- );
- assertTrue(xml.exists());
-
- XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
- XSSFOddHeader hdr;
- XSSFOddFooter ftr;
-
- // Sheet 1 has a header with center and right text
- XSSFSheet s1 = workbook.getSheetAt(0);
- assertNotNull(s1.getHeader());
- assertNotNull(s1.getFooter());
- hdr = (XSSFOddHeader)s1.getHeader();
- ftr = (XSSFOddFooter)s1.getFooter();
-
- assertEquals("&Ctestdoc&Rtest phrase", hdr.getText());
- assertEquals(null, ftr.getText());
-
- assertEquals("", hdr.getLeft());
- assertEquals("testdoc", hdr.getCenter());
- assertEquals("test phrase", hdr.getRight());
-
- assertEquals("", ftr.getLeft());
- assertEquals("", ftr.getCenter());
- assertEquals("", ftr.getRight());
-
-
- // Sheet 2 has a footer, but it's empty
- XSSFSheet s2 = workbook.getSheetAt(1);
- assertNotNull(s2.getHeader());
- assertNotNull(s2.getFooter());
- hdr = (XSSFOddHeader)s2.getHeader();
- ftr = (XSSFOddFooter)s2.getFooter();
-
- assertEquals(null, hdr.getText());
- assertEquals("&L&F", ftr.getText());
-
- assertEquals("", hdr.getLeft());
- assertEquals("", hdr.getCenter());
- assertEquals("", hdr.getRight());
-
- assertEquals("&F", ftr.getLeft());
- assertEquals("", ftr.getCenter());
- assertEquals("", ftr.getRight());
-
-
- // Save and reload
- XSSFWorkbook wb = XSSFTestDataSamples.writeOutAndReadBack(workbook);
-
- hdr = (XSSFOddHeader)wb.getSheetAt(0).getHeader();
- ftr = (XSSFOddFooter)wb.getSheetAt(0).getFooter();
-
- assertEquals("", hdr.getLeft());
- assertEquals("testdoc", hdr.getCenter());
- assertEquals("test phrase", hdr.getRight());
-
- assertEquals("", ftr.getLeft());
- assertEquals("", ftr.getCenter());
- assertEquals("", ftr.getRight());
+ File xml = new File(
+ System.getProperty("HSSF.testdata.path") +
+ File.separator + "45540_classic_Header.xlsx"
+ );
+ assertTrue(xml.exists());
+
+ XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
+ XSSFOddHeader hdr;
+ XSSFOddFooter ftr;
+
+ // Sheet 1 has a header with center and right text
+ XSSFSheet s1 = workbook.getSheetAt(0);
+ assertNotNull(s1.getHeader());
+ assertNotNull(s1.getFooter());
+ hdr = (XSSFOddHeader) s1.getHeader();
+ ftr = (XSSFOddFooter) s1.getFooter();
+
+ assertEquals("&Ctestdoc&Rtest phrase", hdr.getText());
+ assertEquals(null, ftr.getText());
+
+ assertEquals("", hdr.getLeft());
+ assertEquals("testdoc", hdr.getCenter());
+ assertEquals("test phrase", hdr.getRight());
+
+ assertEquals("", ftr.getLeft());
+ assertEquals("", ftr.getCenter());
+ assertEquals("", ftr.getRight());
+
+ // Sheet 2 has a footer, but it's empty
+ XSSFSheet s2 = workbook.getSheetAt(1);
+ assertNotNull(s2.getHeader());
+ assertNotNull(s2.getFooter());
+ hdr = (XSSFOddHeader) s2.getHeader();
+ ftr = (XSSFOddFooter) s2.getFooter();
+
+ assertEquals(null, hdr.getText());
+ assertEquals("&L&F", ftr.getText());
+
+ assertEquals("", hdr.getLeft());
+ assertEquals("", hdr.getCenter());
+ assertEquals("", hdr.getRight());
+
+ assertEquals("&F", ftr.getLeft());
+ assertEquals("", ftr.getCenter());
+ assertEquals("", ftr.getRight());
+
+ // Save and reload
+ XSSFWorkbook wb = XSSFTestDataSamples.writeOutAndReadBack(workbook);
+
+ hdr = (XSSFOddHeader) wb.getSheetAt(0).getHeader();
+ ftr = (XSSFOddFooter) wb.getSheetAt(0).getFooter();
+
+ assertEquals("", hdr.getLeft());
+ assertEquals("testdoc", hdr.getCenter());
+ assertEquals("test phrase", hdr.getRight());
+
+ assertEquals("", ftr.getLeft());
+ assertEquals("", ftr.getCenter());
+ assertEquals("", ftr.getRight());
}
public void testGetAllHeadersFooters() {
assertEquals("odd header center", sheet.getHeader().getCenter());
}
- public void testGetSetColumnHidden() {
- Workbook workbook = getTestDataProvider().createWorkbook();
- Sheet sheet = workbook.createSheet("Sheet 1");
- sheet.setColumnHidden((short) 2, true);
- assertTrue(sheet.isColumnHidden((short) 2));
- }
-
public void testAutoSizeColumn() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet 1");
sheet.createRow(0).createCell(13).setCellValue("test");
- sheet.autoSizeColumn((short)13);
+ sheet.autoSizeColumn(13);
ColumnHelper columnHelper = sheet.getColumnHelper();
CTCol col = columnHelper.getColumn(13, false);
assertTrue(col.getBestFit());
}
-
+
public void testGetCellComment() {
XSSFWorkbook workbook = new XSSFWorkbook();
assertNotNull(sheet.getCellComment(9, 2));
assertEquals("test C10 author", sheet.getCellComment(9, 2).getAuthor());
}
-
+
public void testSetCellComment() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
XSSFComment comment = sheet.createComment();
- Cell cell = sheet.createRow(0).createCell((short)0);
+ Cell cell = sheet.createRow(0).createCell((short) 0);
CommentsTable comments = sheet.getCommentsTable();
CTComments ctComments = comments.getCTComments();
sheet.setCellComment("A1", comment);
assertEquals("A1", ctComments.getCommentList().getCommentArray(0).getRef());
comment.setAuthor("test A1 author");
- assertEquals("test A1 author", comments.getAuthor((int)ctComments.getCommentList().getCommentArray(0).getAuthorId()));
+ assertEquals("test A1 author", comments.getAuthor((int) ctComments.getCommentList().getCommentArray(0).getAuthorId()));
}
-
+
public void testGetActiveCell() {
- XSSFWorkbook workbook = new XSSFWorkbook();
- XSSFSheet sheet = workbook.createSheet();
- sheet.setActiveCell("R5");
+ XSSFWorkbook workbook = new XSSFWorkbook();
+ XSSFSheet sheet = workbook.createSheet();
+ sheet.setActiveCell("R5");
+
+ assertEquals("R5", sheet.getActiveCell());
- assertEquals("R5", sheet.getActiveCell());
-
}
-
+
public void testCreateFreezePane() {
- XSSFWorkbook workbook = new XSSFWorkbook();
- XSSFSheet sheet = workbook.createSheet();
+ XSSFWorkbook workbook = new XSSFWorkbook();
+ XSSFSheet sheet = workbook.createSheet();
CTWorksheet ctWorksheet = sheet.getCTWorksheet();
- sheet.createFreezePane(2, 4);
- assertEquals((double)2, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getXSplit());
- assertEquals(STPane.BOTTOM_RIGHT, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getActivePane());
- sheet.createFreezePane(3, 6, 10, 10);
- assertEquals((double)3, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getXSplit());
- // assertEquals(10, sheet.getTopRow());
- // assertEquals(10, sheet.getLeftCol());
- sheet.createSplitPane(4, 8, 12, 12, 1);
- assertEquals((double)8, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getYSplit());
- assertEquals(STPane.BOTTOM_RIGHT, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getActivePane());
+ sheet.createFreezePane(2, 4);
+ assertEquals((double) 2, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getXSplit());
+ assertEquals(STPane.BOTTOM_RIGHT, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getActivePane());
+ sheet.createFreezePane(3, 6, 10, 10);
+ assertEquals((double) 3, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getXSplit());
+ // assertEquals(10, sheet.getTopRow());
+ // assertEquals(10, sheet.getLeftCol());
+ sheet.createSplitPane(4, 8, 12, 12, 1);
+ assertEquals((double) 8, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getYSplit());
+ assertEquals(STPane.BOTTOM_RIGHT, ctWorksheet.getSheetViews().getSheetViewArray(0).getPane().getActivePane());
}
-
+
public void testNewMergedRegionAt() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
- CellRangeAddress region = CellRangeAddress.valueOf("B2:D4");
- sheet.addMergedRegion(region);
- assertEquals("B2:D4", sheet.getMergedRegion(0).formatAsString());
+ CellRangeAddress region = CellRangeAddress.valueOf("B2:D4");
+ sheet.addMergedRegion(region);
+ assertEquals("B2:D4", sheet.getMergedRegion(0).formatAsString());
+ assertEquals(1, sheet.getNumMergedRegions());
}
-
- public void testGetNumMergedRegions() {
+
+ public void testRemoveMergedRegion_lowlevel() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
- assertEquals(0, sheet.getNumMergedRegions());
- CellRangeAddress region = CellRangeAddress.valueOf("B2:D4");
- sheet.addMergedRegion(region);
- assertEquals(1, sheet.getNumMergedRegions());
+ CTWorksheet ctWorksheet = sheet.getCTWorksheet();
+ CellRangeAddress region_1 = CellRangeAddress.valueOf("A1:B2");
+ CellRangeAddress region_2 = CellRangeAddress.valueOf("C3:D4");
+ CellRangeAddress region_3 = CellRangeAddress.valueOf("E5:F6");
+ sheet.addMergedRegion(region_1);
+ sheet.addMergedRegion(region_2);
+ sheet.addMergedRegion(region_3);
+ assertEquals("C3:D4", ctWorksheet.getMergeCells().getMergeCellArray(1).getRef());
+ assertEquals(3, sheet.getNumMergedRegions());
+ sheet.removeMergedRegion(1);
+ assertEquals("E5:F6", ctWorksheet.getMergeCells().getMergeCellArray(1).getRef());
+ assertEquals(2, sheet.getNumMergedRegions());
+ sheet.removeMergedRegion(1);
+ sheet.removeMergedRegion(0);
+ assertEquals(0, sheet.getNumMergedRegions());
}
-
- public void testRemoveMergedRegion() {
+
+ public void testSetDefaultColumnStyle() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
CTWorksheet ctWorksheet = sheet.getCTWorksheet();
- CellRangeAddress region_1 = CellRangeAddress.valueOf("A1:B2");
- CellRangeAddress region_2 = CellRangeAddress.valueOf("C3:D4");
- CellRangeAddress region_3 = CellRangeAddress.valueOf("E5:F6");
- sheet.addMergedRegion(region_1);
- sheet.addMergedRegion(region_2);
- sheet.addMergedRegion(region_3);
- assertEquals("C3:D4", ctWorksheet.getMergeCells().getMergeCellArray(1).getRef());
- assertEquals(3, sheet.getNumMergedRegions());
- sheet.removeMergedRegion(1);
- assertEquals("E5:F6", ctWorksheet.getMergeCells().getMergeCellArray(1).getRef());
- assertEquals(2, sheet.getNumMergedRegions());
- sheet.removeMergedRegion(1);
- sheet.removeMergedRegion(0);
- assertEquals(0, sheet.getNumMergedRegions());
+ StylesTable stylesTable = workbook.getStylesSource();
+ XSSFFont font = new XSSFFont();
+ font.setFontName("Cambria");
+ stylesTable.putFont(font);
+ CTXf cellStyleXf = CTXf.Factory.newInstance();
+ cellStyleXf.setFontId(1);
+ cellStyleXf.setFillId(0);
+ cellStyleXf.setBorderId(0);
+ cellStyleXf.setNumFmtId(0);
+ stylesTable.putCellStyleXf(cellStyleXf);
+ CTXf cellXf = CTXf.Factory.newInstance();
+ cellXf.setXfId(1);
+ stylesTable.putCellXf(cellXf);
+ XSSFCellStyle cellStyle = new XSSFCellStyle(1, 1, stylesTable);
+ assertEquals(1, cellStyle.getFontIndex());
+
+ sheet.setDefaultColumnStyle((short) 3, cellStyle);
+ assertEquals(1, ctWorksheet.getColsArray(0).getColArray(0).getStyle());
}
-
- public void testSetDefaultColumnStyle() {
+
+
+ public void testGroupUngroupColumn() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
- CTWorksheet ctWorksheet = sheet.getCTWorksheet();
- StylesTable stylesTable = workbook.getStylesSource();
- XSSFFont font = new XSSFFont();
- font.setFontName("Cambria");
- stylesTable.putFont(font);
- CTXf cellStyleXf = CTXf.Factory.newInstance();
- cellStyleXf.setFontId(1);
- cellStyleXf.setFillId(0);
- cellStyleXf.setBorderId(0);
- cellStyleXf.setNumFmtId(0);
- stylesTable.putCellStyleXf(cellStyleXf);
- CTXf cellXf = CTXf.Factory.newInstance();
- cellXf.setXfId(1);
- stylesTable.putCellXf(cellXf);
- XSSFCellStyle cellStyle = new XSSFCellStyle(1, 1, stylesTable);
- assertEquals(1, cellStyle.getFontIndex());
-
- sheet.setDefaultColumnStyle((short) 3, cellStyle);
- assertEquals(1, ctWorksheet.getColsArray(0).getColArray(0).getStyle());
+
+ //one level
+ sheet.groupColumn((short) 2, (short) 7);
+ sheet.groupColumn((short) 10, (short) 11);
+ CTCols cols = sheet.getCTWorksheet().getColsArray(0);
+ assertEquals(2, cols.sizeOfColArray());
+ CTCol[] colArray = cols.getColArray();
+ assertNotNull(colArray);
+ assertEquals(2 + 1, colArray[0].getMin()); // 1 based
+ assertEquals(7 + 1, colArray[0].getMax()); // 1 based
+ assertEquals(1, colArray[0].getOutlineLevel());
+
+ //two level
+ sheet.groupColumn((short) 1, (short) 2);
+ cols = sheet.getCTWorksheet().getColsArray(0);
+ assertEquals(4, cols.sizeOfColArray());
+ colArray = cols.getColArray();
+ assertEquals(2, colArray[1].getOutlineLevel());
+
+ //three level
+ sheet.groupColumn((short) 6, (short) 8);
+ sheet.groupColumn((short) 2, (short) 3);
+ cols = sheet.getCTWorksheet().getColsArray(0);
+ assertEquals(7, cols.sizeOfColArray());
+ colArray = cols.getColArray();
+ assertEquals(3, colArray[1].getOutlineLevel());
+ assertEquals(3, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelCol());
+
+ sheet.ungroupColumn((short) 8, (short) 10);
+ colArray = cols.getColArray();
+ //assertEquals(3, colArray[1].getOutlineLevel());
+
+ sheet.ungroupColumn((short) 4, (short) 6);
+ sheet.ungroupColumn((short) 2, (short) 2);
+ colArray = cols.getColArray();
+ assertEquals(4, colArray.length);
+ assertEquals(2, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelCol());
}
-
-
-
-
-
- public void testGroupUngroupColumn() {
- XSSFWorkbook workbook = new XSSFWorkbook();
- XSSFSheet sheet = workbook.createSheet();
-
- //one level
- sheet.groupColumn((short)2,(short)7);
- sheet.groupColumn((short)10,(short)11);
- CTCols cols=sheet.getCTWorksheet().getColsArray(0);
- assertEquals(2,cols.sizeOfColArray());
- CTCol[]colArray=cols.getColArray();
- assertNotNull(colArray);
- assertEquals(2+1,colArray[0].getMin()); // 1 based
- assertEquals(7+1,colArray[0].getMax()); // 1 based
- assertEquals(1, colArray[0].getOutlineLevel());
-
- //two level
- sheet.groupColumn((short)1,(short)2);
- cols=sheet.getCTWorksheet().getColsArray(0);
- assertEquals(4,cols.sizeOfColArray());
- colArray=cols.getColArray();
- assertEquals(2, colArray[1].getOutlineLevel());
-
- //three level
- sheet.groupColumn((short)6,(short)8);
- sheet.groupColumn((short)2,(short)3);
- cols=sheet.getCTWorksheet().getColsArray(0);
- assertEquals(7,cols.sizeOfColArray());
- colArray=cols.getColArray();
- assertEquals(3, colArray[1].getOutlineLevel());
- assertEquals(3,sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelCol());
-
- sheet.ungroupColumn((short)8,(short) 10);
- colArray=cols.getColArray();
- //assertEquals(3, colArray[1].getOutlineLevel());
-
- sheet.ungroupColumn((short)4,(short)6);
- sheet.ungroupColumn((short)2,(short)2);
- colArray=cols.getColArray();
- assertEquals(4, colArray.length);
- assertEquals(2,sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelCol());
- }
-
-
- public void testGroupUngroupRow() {
- XSSFWorkbook workbook = new XSSFWorkbook();
- XSSFSheet sheet = workbook.createSheet();
-
- //one level
- sheet.groupRow(9,10);
- assertEquals(2,sheet.getPhysicalNumberOfRows());
- CTRow ctrow = sheet.getRow(9).getCTRow();
-
- assertNotNull(ctrow);
- assertEquals(10,ctrow.getR());
- assertEquals(1, ctrow.getOutlineLevel());
- assertEquals(1,sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
-
- //two level
- sheet.groupRow(10,13);
- assertEquals(5,sheet.getPhysicalNumberOfRows());
- ctrow = sheet.getRow(10).getCTRow();
- assertNotNull(ctrow);
- assertEquals(11,ctrow.getR());
- assertEquals(2, ctrow.getOutlineLevel());
- assertEquals(2,sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
-
-
- sheet.ungroupRow(8, 10);
- assertEquals(4,sheet.getPhysicalNumberOfRows());
- assertEquals(1,sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
-
- sheet.ungroupRow(10,10);
- assertEquals(3,sheet.getPhysicalNumberOfRows());
-
- assertEquals(1,sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
- }
-
+
+
+ public void testGroupUngroupRow() {
+ XSSFWorkbook workbook = new XSSFWorkbook();
+ XSSFSheet sheet = workbook.createSheet();
+
+ //one level
+ sheet.groupRow(9, 10);
+ assertEquals(2, sheet.getPhysicalNumberOfRows());
+ CTRow ctrow = sheet.getRow(9).getCTRow();
+
+ assertNotNull(ctrow);
+ assertEquals(10, ctrow.getR());
+ assertEquals(1, ctrow.getOutlineLevel());
+ assertEquals(1, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
+
+ //two level
+ sheet.groupRow(10, 13);
+ assertEquals(5, sheet.getPhysicalNumberOfRows());
+ ctrow = sheet.getRow(10).getCTRow();
+ assertNotNull(ctrow);
+ assertEquals(11, ctrow.getR());
+ assertEquals(2, ctrow.getOutlineLevel());
+ assertEquals(2, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
+
+
+ sheet.ungroupRow(8, 10);
+ assertEquals(4, sheet.getPhysicalNumberOfRows());
+ assertEquals(1, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
+
+ sheet.ungroupRow(10, 10);
+ assertEquals(3, sheet.getPhysicalNumberOfRows());
+
+ assertEquals(1, sheet.getCTWorksheet().getSheetFormatPr().getOutlineLevelRow());
+ }
+
public void testSetZoom() {
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet1 = workBook.createSheet("new sheet");
- sheet1.setZoom(3,4); // 75 percent magnification
+ sheet1.setZoom(3, 4); // 75 percent magnification
long zoom = sheet1.getCTWorksheet().getSheetViews().getSheetViewArray(0).getZoomScale();
assertEquals(zoom, 75);
try {
sheet1.setZoom(500);
fail("Expecting exception");
- } catch (IllegalArgumentException e){
+ } catch (IllegalArgumentException e) {
assertEquals("Valid scale values range from 10 to 400", e.getMessage());
}
}
public void testColumnWidth_lowlevel() {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Sheet 1");
- sheet.setColumnWidth(1, 22*256);
- assertEquals(22*256, sheet.getColumnWidth(1));
+ sheet.setColumnWidth(1, 22 * 256);
+ assertEquals(22 * 256, sheet.getColumnWidth(1));
// Now check the low level stuff, and check that's all
// been set correctly
assertEquals(2, col.getMax());
assertEquals(22.0, col.getWidth());
-
// Now set another
- sheet.setColumnWidth(3, 33*256);
+ sheet.setColumnWidth(3, 33 * 256);
cols_s = cts.getColsArray();
assertEquals(1, cols_s.length);
}
- public void testRepeatingRowsAndColums() {
- // First test that setting RR&C for same sheet more than once only creates a
- // single Print_Titles built-in record
- XSSFWorkbook wb = new XSSFWorkbook();
- XSSFSheet sheet = wb.createSheet("FirstSheet");
-
- // set repeating rows and columns twice for the first sheet
- for (int i = 0; i < 2; i++) {
- wb.setRepeatingRowsAndColumns(0, 0, 0, 0, 3);
- //sheet.createFreezePane(0, 3);
- }
- assertEquals(1, wb.getNumberOfNames());
- XSSFName nr1 = wb.getNameAt(0);
-
- assertEquals(XSSFName.BUILTIN_PRINT_TITLE, nr1.getNameName());
- assertEquals("'FirstSheet'!$A:$A,'FirstSheet'!$1:$4", nr1.getRefersToFormula());
-
- // Save and re-open
- XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb);
-
- assertEquals(1, nwb.getNumberOfNames());
- nr1 = nwb.getNameAt(0);
-
- assertEquals(XSSFName.BUILTIN_PRINT_TITLE, nr1.getNameName());
- assertEquals("'FirstSheet'!$A:$A,'FirstSheet'!$1:$4", nr1.getRefersToFormula());
-
- // check that setting RR&C on a second sheet causes a new Print_Titles built-in
- // name to be created
- sheet = nwb.createSheet("SecondSheet");
- nwb.setRepeatingRowsAndColumns(1, 1, 2, 0, 0);
-
- assertEquals(2, nwb.getNumberOfNames());
- XSSFName nr2 = nwb.getNameAt(1);
-
- assertEquals(XSSFName.BUILTIN_PRINT_TITLE, nr2.getNameName());
- assertEquals("'SecondSheet'!$B:$C,'SecondSheet'!$1:$1", nr2.getRefersToFormula());
-
- nwb.setRepeatingRowsAndColumns(1, -1, -1, -1, -1);
- }
-
/**
* Tests that we can save, and then re-load a new document
*/
}
- public void testFindFont(){
- //get default font and check against default value
- XSSFWorkbook workbook = new XSSFWorkbook();
- Font fontFind=workbook.findFont(Font.BOLDWEIGHT_NORMAL, IndexedColors.BLACK.getIndex(), (short)11, "Calibri", false, false, Font.SS_NONE, Font.U_NONE);
- assertNotNull(fontFind);
-
- //get default font, then change 2 values and check against different values (height changes)
- Font font=workbook.createFont();
- ((XSSFFont)font).setBold(true);
- font.setUnderline(Font.U_DOUBLE);
- fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, IndexedColors.BLACK.getIndex(), (short)15, "Calibri", false, false, Font.SS_NONE, Font.U_DOUBLE);
- assertNull(fontFind);
- }
-
public void testGetCellStyleAt(){
XSSFWorkbook workbook = new XSSFWorkbook();
short i = 0;
assertNotNull(fontAt);
}
- public void testGetNumberOfFonts(){
- XSSFWorkbook wb = new XSSFWorkbook();
-
- XSSFFont f1=wb.createFont();
- f1.setBold(true);
- wb.createCellStyle().setFont(f1);
-
- XSSFFont f2=wb.createFont();
- f2.setUnderline(Font.U_DOUBLE);
- wb.createCellStyle().setFont(f2);
-
- XSSFFont f3=wb.createFont();
- f3.setFontHeightInPoints((short)23);
- wb.createCellStyle().setFont(f3);
-
- assertEquals(4,wb.getNumberOfFonts());
- assertEquals(Font.U_DOUBLE,wb.getFontAt((short)2).getUnderline());
- }
-
public void testGetNumCellStyles(){
XSSFWorkbook workbook = new XSSFWorkbook();
short i = workbook.getNumCellStyles();
assertNotSame(2, i);
}
- public void testSetDisplayedTab(){
- XSSFWorkbook workbook = new XSSFWorkbook();
- workbook.setFirstVisibleTab(1);
- short i = (short) workbook.getFirstVisibleTab();
- //0 (defualt value) is not longer set
- assertNotSame(0, i);
- //1 is the default tab
- assertEquals(1, i);
- }
-
-
public void testLoadSave() {
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("Formatting.xlsx");
assertEquals(3, workbook.getNumberOfSheets());
return new HSSFWorkbook();\r
}\r
\r
+ public byte[] getTestDataFileContent(String fileName) {\r
+ return HSSFTestDataSamples.getTestDataFileContent(fileName);\r
+ }\r
+ \r
private HSSFITestDataProvider(){}\r
private static HSSFITestDataProvider inst = new HSSFITestDataProvider();\r
public static HSSFITestDataProvider getInstance(){\r
result.addTestSuite(TestHSSFTextbox.class);
result.addTestSuite(TestHSSFWorkbook.class);
result.addTestSuite(TestLinkTable.class);
- result.addTestSuite(TestNamedRange.class);
+ result.addTestSuite(TestHSSFName.class);
result.addTestSuite(TestOLE2Embeding.class);
result.addTestSuite(TestPOIFSProperties.class);
result.addTestSuite(TestReadWriteChart.class);
+++ /dev/null
-/* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-==================================================================== */
-
-package org.apache.poi.hssf.usermodel;
-
-import junit.framework.TestCase;
-
-/**
- *
- */
-public final class TestBug43093 extends TestCase {
-
- private static void addNewSheetWithCellsA1toD4(HSSFWorkbook book, int sheet) {
-
- HSSFSheet sht = book .createSheet("s" + sheet);
- for (int r=0; r < 4; r++) {
-
- HSSFRow row = sht.createRow (r);
- for (int c=0; c < 4; c++) {
-
- HSSFCell cel = row.createCell(c);
- cel.setCellValue(sheet*100 + r*10 + c);
- }
- }
- }
-
- public void testBug43093() {
- HSSFWorkbook xlw = new HSSFWorkbook();
-
- addNewSheetWithCellsA1toD4(xlw, 1);
- addNewSheetWithCellsA1toD4(xlw, 2);
- addNewSheetWithCellsA1toD4(xlw, 3);
- addNewSheetWithCellsA1toD4(xlw, 4);
-
- HSSFSheet s2 = xlw.getSheet("s2");
- HSSFRow s2r3 = s2.getRow(3);
- HSSFCell s2E4 = s2r3.createCell(4);
- s2E4.setCellFormula("SUM(s3!B2:C3)");
-
- HSSFFormulaEvaluator eva = new HSSFFormulaEvaluator(xlw);
- double d = eva.evaluate(s2E4).getNumberValue();
-
- // internalEvaluate(...) Area3DEval.: 311+312+321+322 expected
- assertEquals(d, (311+312+321+322), 0.0000001);
- // System.out.println("Area3DEval ok.: 311+312+321+322=" + d);
- }
-}
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.OldExcelFormatException;
+import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.CellValueRecordInterface;
import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.TempFile;
/**
* Testcases for bugs entered in bugzilla
* the Test name contains the bugzilla bug id
+ *
+ * <b>YK: If a bug can be tested in terms of common ss interfaces,
+ * define the test in the base class {@link BaseTestBugzillaIssues}</b>
+ *
* @author Avik Sengupta
* @author Yegor Kozlov
*/
-public final class TestBugs extends TestCase {
+public final class TestBugs extends BaseTestBugzillaIssues {
- private static HSSFWorkbook openSample(String sampleFileName) {
- return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
+ @Override
+ protected HSSFITestDataProvider getTestDataProvider(){
+ return HSSFITestDataProvider.getInstance();
}
- private static HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {
- return HSSFTestDataSamples.writeOutAndReadBack(original);
+ private HSSFWorkbook openSample(String sampleFileName) {
+ return getTestDataProvider().openSampleWorkbook(sampleFileName);
+ }
+
+ private HSSFWorkbook writeOutAndReadBack(HSSFWorkbook original) {
+ return getTestDataProvider().writeOutAndReadBack(original);
}
private static void writeTestOutputFileForViewing(HSSFWorkbook wb, String simpleFileName) {
c.setCellValue(10);
writeOutAndReadBack(wb);
}
- /**Test writing a hyperlink
- * Open resulting sheet in Excel and check that A1 contains a hyperlink*/
- public void test23094() {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet s = wb.createSheet();
- HSSFRow r = s.createRow(0);
- r.createCell(0).setCellFormula("HYPERLINK( \"http://jakarta.apache.org\", \"Jakarta\" )");
-
- writeTestOutputFileForViewing(wb, "test23094");
- }
/** test hyperlinks
* open resulting file in excel, and check that there is a link to Google
writeTestOutputFileForViewing(wb, "test15375");
}
-
- /** test writing a file with large number of unique strings
- *open resulting file in Excel to check results!*/
-
- public void test15375_2() throws Exception{
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet();
-
- String tmp1 = null;
- String tmp2 = null;
- String tmp3 = null;
-
- for (int i = 0; i < 6000; i++) {
- tmp1 = "Test1" + i;
- tmp2 = "Test2" + i;
- tmp3 = "Test3" + i;
-
- HSSFRow row = sheet.createRow(i);
-
- HSSFCell cell = row.createCell(0);
- setCellText(cell, tmp1);
- cell = row.createCell(1);
- setCellText(cell, tmp2);
- cell = row.createCell(2);
- setCellText(cell, tmp3);
- }
- writeTestOutputFileForViewing(wb, "test15375-2");
- }
- /** another test for the number of unique strings issue
- *test opening the resulting file in Excel*/
- public void test22568() {
- int r=2000;int c=3;
-
- HSSFWorkbook wb = new HSSFWorkbook() ;
- HSSFSheet sheet = wb.createSheet("ExcelTest") ;
-
- int col_cnt=0, rw_cnt=0 ;
-
- col_cnt = c;
- rw_cnt = r;
-
- HSSFRow rw ;
- rw = sheet.createRow(0) ;
- //Header row
- for(int j=0; j<col_cnt; j++){
- HSSFCell cell = rw.createCell(j) ;
- setCellText(cell, "Col " + (j+1)) ;
- }
-
- for(int i=1; i<rw_cnt; i++){
- rw = sheet.createRow(i) ;
- for(int j=0; j<col_cnt; j++){
- HSSFCell cell = rw.createCell(j) ;
- setCellText(cell, "Row:" + (i+1) + ",Column:" + (j+1)) ;
- }
- }
-
- sheet.setDefaultColumnWidth(18) ;
-
- writeTestOutputFileForViewing(wb, "test22568");
+ /**
+ * test writing a file with large number of unique strings,
+ * open resulting file in Excel to check results!
+ */
+ public void test15375_2() {
+ baseTest15375(6000);
}
-
+
/**Double byte strings*/
public void test15556() {
}
}
- public void test18800() {
- HSSFWorkbook book = new HSSFWorkbook();
- book.createSheet("TEST");
- HSSFSheet sheet = book.cloneSheet(0);
- book.setSheetName(1,"CLONE");
- sheet.createRow(0).createCell(0).setCellValue(new HSSFRichTextString("Test"));
-
- book = writeOutAndReadBack(book);
- sheet = book.getSheet("CLONE");
- HSSFRow row = sheet.getRow(0);
- HSSFCell cell = row.getCell(0);
- assertEquals("Test", cell.getRichStringCellValue().getString());
- }
-
- /**
- * Merged regions were being removed from the parent in cloned sheets
- */
- public void test22720() {
- HSSFWorkbook workBook = new HSSFWorkbook();
- workBook.createSheet("TEST");
- HSSFSheet template = workBook.getSheetAt(0);
-
- template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));
- template.addMergedRegion(new CellRangeAddress(1, 2, 0, 2));
-
- HSSFSheet clone = workBook.cloneSheet(0);
- int originalMerged = template.getNumMergedRegions();
- assertEquals("2 merged regions", 2, originalMerged);
-
-// remove merged regions from clone
- for (int i=template.getNumMergedRegions()-1; i>=0; i--) {
- clone.removeMergedRegion(i);
- }
-
- assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions());
-// check if template's merged regions are OK
- if (template.getNumMergedRegions()>0) {
- // fetch the first merged region...EXCEPTION OCCURS HERE
- template.getMergedRegion(0);
- }
- //make sure we dont exception
-
- }
-
/**Tests read and write of Unicode strings in formula results
* bug and testcase submitted by Sompop Kumnoonsate
* The file contains THAI unicode characters.
}
}
- public void test28031() {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet();
- wb.setSheetName(0, "Sheet1");
-
- HSSFRow row = sheet.createRow(0);
- HSSFCell cell = row.createCell(0);
- String formulaText =
- "IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))";
- cell.setCellFormula(formulaText);
-
- assertEquals(formulaText, cell.getCellFormula());
- writeTestOutputFileForViewing(wb, "output28031.xls");
- }
-
public void test33082() {
openSample("33082.xls");
}
*/
public void test29206() {
//the first check with blank workbook
- HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFWorkbook wb = openSample("Simple.xls");
HSSFSheet sheet = wb.createSheet();
-
- for(int i = 1; i < 400; i++) {
- HSSFRow row = sheet.getRow(i);
- if(row != null) {
- row.getCell(0);
- }
- }
-
- //now check on an existing xls file
- wb = openSample("Simple.xls");
-
for(int i = 1; i < 400; i++) {
HSSFRow row = sheet.getRow(i);
if(row != null) {
assertTrue("no errors writing sample xls", true);
}
- /**
- * Bug 21334: "File error: data may have been lost" with a file
- * that contains macros and this formula:
- * {=SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""))>0,1))}
- */
- public void test21334() {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sh = wb.createSheet();
- HSSFCell cell = sh.createRow(0).createCell(0);
- String formula = "SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"))>0,1))";
- cell.setCellFormula(formula);
-
- HSSFWorkbook wb_sv = writeOutAndReadBack(wb);
- HSSFCell cell_sv = wb_sv.getSheetAt(0).getRow(0).getCell(0);
- assertEquals(formula, cell_sv.getCellFormula());
- }
-
public void test36947() {
HSSFWorkbook wb = openSample("36947.xls");
assertTrue("no errors reading sample xls", true);
assertTrue("no errors writing sample xls", true);
}
- /**
- * Bug 42448: Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69
- */
- public void test42448(){
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFCell cell = wb.createSheet().createRow(0).createCell(0);
- cell.setCellFormula("SUMPRODUCT(A!C7:A!C67, B8:B68) / B69");
- assertTrue("no errors parsing formula", true);
- }
-
public void test39634() {
HSSFWorkbook wb = openSample("39634.xls");
assertTrue("no errors reading sample xls", true);
assertEquals(1, wb.getNumberOfSheets());
}
- /**
- * HSSFRichTextString.length() returns negative for really
- * long strings
- */
- public void test46368() {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet s = wb.createSheet();
- HSSFRow r = s.createRow(0);
- for(int i=0; i<15; i++) {
- int len = 32760 + i;
- HSSFCell c = r.createCell(i);
-
- StringBuffer sb = new StringBuffer();
- for(int j=0; j<len; j++) {
- sb.append("x");
- }
- HSSFRichTextString rtr = new HSSFRichTextString(sb.toString());
- assertEquals(len, rtr.length());
- c.setCellValue(rtr);
- }
-
- // Save and reload
- wb = writeOutAndReadBack(wb);
- s = wb.getSheetAt(0);
- r = s.getRow(0);
- for(int i=0; i<15; i++) {
- int len = 32760 + i;
- HSSFCell c = r.getCell(i);
- assertEquals(len, c.getRichStringCellValue().length());
- }
- }
-
/**
* In POI-2.5 user reported exception when parsing a name with a custom VBA function:
* =MY_VBA_FUNCTION("lskdjflsk")
--- /dev/null
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.hssf.usermodel;
+
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import junit.framework.AssertionFailedError;
+import org.apache.poi.hssf.HSSFITestDataProvider;
+import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.hssf.model.Sheet;
+import org.apache.poi.hssf.model.HSSFFormulaParser;
+import org.apache.poi.hssf.record.DBCellRecord;
+import org.apache.poi.hssf.record.FormulaRecord;
+import org.apache.poi.hssf.record.Record;
+import org.apache.poi.hssf.record.StringRecord;
+import org.apache.poi.hssf.record.formula.Ptg;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.util.AreaReference;
+import org.apache.poi.ss.util.CellReference;
+import org.apache.poi.ss.formula.FormulaType;
+
+/**
+ * Tests various functionality having to do with {@link org.apache.poi.ss.usermodel.Name}.
+ *
+ * @author Yegor Kozlov
+ */
+public final class TestHSSFFont extends BaseTestFont {
+
+ @Override
+ protected HSSFITestDataProvider getTestDataProvider(){
+ return HSSFITestDataProvider.getInstance();
+ }
+
+ public void testDefaultFont() {
+ baseTestDefaultFont(HSSFFont.FONT_ARIAL, (short)200, Font.COLOR_NORMAL);
+ }
+}
\ No newline at end of file
import java.util.Date;
import java.util.GregorianCalendar;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
import junit.framework.AssertionFailedError;
import org.apache.poi.hssf.HSSFITestDataProvider;
+import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.model.Sheet;
+import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.DBCellRecord;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.StringRecord;
+import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.ss.usermodel.ErrorConstants;
import org.apache.poi.ss.usermodel.BaseTestCell;
import org.apache.poi.ss.usermodel.BaseTestNamedRange;
+import org.apache.poi.ss.util.AreaReference;
+import org.apache.poi.ss.util.CellReference;
+import org.apache.poi.ss.formula.FormulaType;
/**
- * Tests various functionality having to do with {@link org.apache.poi.hssf.usermodel.HSSFCell}. For instance support for
- * particular datatypes, etc.
- * @author Andrew C. Oliver (andy at superlinksoftware dot com)
- * @author Dan Sherman (dsherman at isisph.com)
- * @author Alex Jacoby (ajacoby at gmail.com)
+ * Tests various functionality having to do with {@link org.apache.poi.ss.usermodel.Name}.
+ *
+ * @author Andrew C. Oliver (acoliver at apache dot org)
+ * @author ROMANL
+ * @author Danny Mui (danny at muibros.com)
+ * @author Amol S. Deshmukh < amol at ap ache dot org >
*/
public final class TestHSSFName extends BaseTestNamedRange {
return HSSFITestDataProvider.getInstance();
}
+ public void testRepeatingRowsAndColumsNames() {
+ // First test that setting RR&C for same sheet more than once only creates a
+ // single Print_Titles built-in record
+ HSSFWorkbook wb = new HSSFWorkbook();
+ HSSFSheet sheet = wb.createSheet("FirstSheet");
+
+ // set repeating rows and columns twice for the first sheet
+ for (int i = 0; i < 2; i++) {
+ wb.setRepeatingRowsAndColumns(0, 0, 0, 0, 3-1);
+ sheet.createFreezePane(0, 3);
+ }
+ assertEquals(1, wb.getNumberOfNames());
+ HSSFName nr1 = wb.getNameAt(0);
+
+ assertEquals("Print_Titles", nr1.getNameName());
+ if (false) {
+ // TODO - full column references not rendering properly, absolute markers not present either
+ assertEquals("FirstSheet!$A:$A,FirstSheet!$1:$3", nr1.getRefersToFormula());
+ } else {
+ assertEquals("FirstSheet!A:A,FirstSheet!$A$1:$IV$3", nr1.getRefersToFormula());
+ }
+
+ // Save and re-open
+ HSSFWorkbook nwb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+
+ assertEquals(1, nwb.getNumberOfNames());
+ nr1 = nwb.getNameAt(0);
+
+ assertEquals("Print_Titles", nr1.getNameName());
+ assertEquals("FirstSheet!A:A,FirstSheet!$A$1:$IV$3", nr1.getRefersToFormula());
+
+ // check that setting RR&C on a second sheet causes a new Print_Titles built-in
+ // name to be created
+ sheet = nwb.createSheet("SecondSheet");
+ nwb.setRepeatingRowsAndColumns(1, 1, 2, 0, 0);
+
+ assertEquals(2, nwb.getNumberOfNames());
+ HSSFName nr2 = nwb.getNameAt(1);
+
+ assertEquals("Print_Titles", nr2.getNameName());
+ assertEquals("SecondSheet!B:C,SecondSheet!$A$1:$IV$1", nr2.getRefersToFormula());
+
+ if (false) {
+ // In case you fancy checking in excel, to ensure it
+ // won't complain about the file now
+ try {
+ File tempFile = File.createTempFile("POI-45126-", ".xls");
+ FileOutputStream fout = new FileOutputStream(tempFile);
+ nwb.write(fout);
+ fout.close();
+ System.out.println("check out " + tempFile.getAbsolutePath());
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+
+ /** Test of TestCase method, of class test.RangeTest. */
+ public void testNamedRange() {
+ HSSFWorkbook wb = getTestDataProvider().openSampleWorkbook("Simple.xls");
+
+ //Creating new Named Range
+ HSSFName newNamedRange = wb.createName();
+
+ //Getting Sheet Name for the reference
+ String sheetName = wb.getSheetName(0);
+
+ //Setting its name
+ newNamedRange.setNameName("RangeTest");
+ //Setting its reference
+ newNamedRange.setRefersToFormula(sheetName + "!$D$4:$E$8");
+
+ //Getting NAmed Range
+ HSSFName namedRange1 = wb.getNameAt(0);
+ //Getting it sheet name
+ sheetName = namedRange1.getSheetName();
+
+ // sanity check
+ SanityChecker c = new SanityChecker();
+ c.checkHSSFWorkbook(wb);
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+ HSSFName nm =wb.getNameAt(wb.getNameIndex("RangeTest"));
+ assertTrue("Name is "+nm.getNameName(),"RangeTest".equals(nm.getNameName()));
+ assertEquals(wb.getSheetName(0)+"!$D$4:$E$8", nm.getRefersToFormula());
+ }
+
+ /**
+ * Reads an excel file already containing a named range.
+ * <p>
+ * Addresses Bug <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=9632" target="_bug">#9632</a>
+ */
+ public void testNamedRead() {
+ HSSFWorkbook wb = getTestDataProvider().openSampleWorkbook("namedinput.xls");
+
+ //Get index of the namedrange with the name = "NamedRangeName" , which was defined in input.xls as A1:D10
+ int NamedRangeIndex = wb.getNameIndex("NamedRangeName");
+
+ //Getting NAmed Range
+ HSSFName namedRange1 = wb.getNameAt(NamedRangeIndex);
+ String sheetName = wb.getSheetName(0);
+
+ //Getting its reference
+ String reference = namedRange1.getRefersToFormula();
+
+ assertEquals(sheetName+"!$A$1:$D$10", reference);
+
+ HSSFName namedRange2 = wb.getNameAt(1);
+
+ assertEquals(sheetName+"!$D$17:$G$27", namedRange2.getRefersToFormula());
+ assertEquals("SecondNamedRange", namedRange2.getNameName());
+ }
+
+ /**
+ * Reads an excel file already containing a named range and updates it
+ * <p>
+ * Addresses Bug <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=16411" target="_bug">#16411</a>
+ */
+ public void testNamedReadModify() {
+ HSSFWorkbook wb = getTestDataProvider().openSampleWorkbook("namedinput.xls");
+
+ HSSFName name = wb.getNameAt(0);
+ String sheetName = wb.getSheetName(0);
+
+ assertEquals(sheetName+"!$A$1:$D$10", name.getRefersToFormula());
+
+ name = wb.getNameAt(1);
+ String newReference = sheetName +"!$A$1:$C$36";
+
+ name.setRefersToFormula(newReference);
+ assertEquals(newReference, name.getRefersToFormula());
+ }
+
+ /**
+ * Test to see if the print area can be retrieved from an excel created file
+ */
+ public void testPrintAreaFileRead() {
+ HSSFWorkbook workbook = getTestDataProvider().openSampleWorkbook("SimpleWithPrintArea.xls");
+
+ String sheetName = workbook.getSheetName(0);
+ String reference = sheetName+"!$A$1:$C$5";
+
+ assertEquals(reference, workbook.getPrintArea(0));
+ }
+
+
+ public void testDeletedReference() throws Exception {
+ HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("24207.xls");
+ assertEquals(2, wb.getNumberOfNames());
+
+ HSSFName name1 = wb.getNameAt(0);
+ assertEquals("a", name1.getNameName());
+ assertEquals("Sheet1!$A$1", name1.getRefersToFormula());
+ new AreaReference(name1.getRefersToFormula());
+ assertTrue("Successfully constructed first reference", true);
+
+ HSSFName name2 = wb.getNameAt(1);
+ assertEquals("b", name2.getNameName());
+ assertEquals("Sheet1!#REF!", name2.getRefersToFormula());
+ assertTrue(name2.isDeleted());
+ try {
+ new AreaReference(name2.getRefersToFormula());
+ fail("attempt to supply an invalid reference to AreaReference constructor results in exception");
+ } catch (StringIndexOutOfBoundsException e) { // TODO - use a different exception for this condition
+ // expected during successful test
+ }
+ }
+
+ /**
+ * When setting A1 type of referencese HSSFName.setRefersToFormula
+ * must set the type of operands to Ptg.CLASS_REF,
+ * otherwise created named don't appear in the dropdown to the left opf formula bar in Excel
+ */
+ public void testTypeOfRootPtg(){
+ HSSFWorkbook wb = new HSSFWorkbook();
+ wb.createSheet("CSCO");
+
+ Ptg[] ptgs = HSSFFormulaParser.parse("CSCO!$E$71", wb, FormulaType.NAMEDRANGE, 0);
+ for (int i = 0; i < ptgs.length; i++) {
+ assertEquals('R', ptgs[i].getRVAType());
+ }
+
+ }
}
\ No newline at end of file
import junit.framework.TestCase;\r
\r
import org.apache.poi.hssf.HSSFTestDataSamples;\r
+import org.apache.poi.hssf.HSSFITestDataProvider;\r
+import org.apache.poi.ss.usermodel.BaseTestPicture;\r
\r
/**\r
* Test <code>HSSFPicture</code>.\r
*\r
* @author Yegor Kozlov (yegor at apache.org)\r
*/\r
-public final class TestHSSFPicture extends TestCase{\r
+public final class TestHSSFPicture extends BaseTestPicture {\r
\r
- public void testResize() {\r
- HSSFWorkbook wb = new HSSFWorkbook();\r
- HSSFSheet sh1 = wb.createSheet();\r
- HSSFPatriarch p1 = sh1.createDrawingPatriarch();\r
-\r
- byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("logoKarmokar4.png");\r
- int idx1 = wb.addPicture( pictureData, HSSFWorkbook.PICTURE_TYPE_PNG );\r
- HSSFPicture picture1 = p1.createPicture(new HSSFClientAnchor(), idx1);\r
- HSSFClientAnchor anchor1 = picture1.getPreferredSize();\r
+ @Override\r
+ protected HSSFITestDataProvider getTestDataProvider(){\r
+ return HSSFITestDataProvider.getInstance();\r
+ }\r
\r
- //assert against what would BiffViewer print if we insert the image in xls and dump the file\r
- assertEquals(0, anchor1.getCol1());\r
- assertEquals(0, anchor1.getRow1());\r
- assertEquals(1, anchor1.getCol2());\r
- assertEquals(9, anchor1.getRow2());\r
- assertEquals(0, anchor1.getDx1());\r
- assertEquals(0, anchor1.getDy1());\r
- assertEquals(848, anchor1.getDx2());\r
- assertEquals(240, anchor1.getDy2());\r
+ public void testResize() {\r
+ baseTestResize(new HSSFClientAnchor(0, 0, 848, 240, (short)0, 0, (short)1, 9));\r
}\r
\r
/**\r
assertEquals(3, nameRecord.getSheetNumber());
}
- public void testCaseInsensitiveNames() {
- HSSFWorkbook b = new HSSFWorkbook( );
- HSSFSheet originalSheet = b.createSheet("Sheet1");
- HSSFSheet fetchedSheet = b.getSheet("sheet1");
- if(fetchedSheet == null) {
- throw new AssertionFailedError("Identified bug 44892");
- }
- assertEquals(originalSheet, fetchedSheet);
- try {
- b.createSheet("sHeeT1");
- fail("should have thrown exceptiuon due to duplicate sheet name");
- } catch (IllegalArgumentException e) {
- // expected during successful test
- assertEquals("The workbook already contains a sheet of this name", e.getMessage());
- }
- }
-
public void testWindowOneDefaults() {
HSSFWorkbook b = new HSSFWorkbook( );
try {
// So, start again
b = getTestDataProvider().openSampleWorkbook("44010-SingleChart.xls");
- b = writeRead(b);
+ b = getTestDataProvider().writeOutAndReadBack(b);
assertEquals(2, b.getNumberOfSheets());
s = b.getSheetAt(1);
assertEquals(0, s.getFirstRowNum());
// So, start again
b = getTestDataProvider().openSampleWorkbook("44010-TwoCharts.xls");
- b = writeRead(b);
+ b = getTestDataProvider().writeOutAndReadBack(b);
assertEquals(3, b.getNumberOfSheets());
s = b.getSheetAt(1);
assertEquals(8, s.getLastRowNum());
}
- private static HSSFWorkbook writeRead(HSSFWorkbook b) {
- return HSSFTestDataSamples.writeOutAndReadBack(b);
- }
-
-
public void testSelectedSheet_bug44523() {
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("Sheet1");
+++ /dev/null
-/* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
-==================================================================== */
-
-package org.apache.poi.hssf.usermodel;
-
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-
-import junit.framework.TestCase;
-
-import org.apache.poi.hssf.HSSFTestDataSamples;
-import org.apache.poi.hssf.model.HSSFFormulaParser;
-import org.apache.poi.hssf.record.formula.Ptg;
-import org.apache.poi.ss.util.AreaReference;
-import org.apache.poi.ss.util.CellReference;
-import org.apache.poi.ss.formula.FormulaType;
-
-/**
- *
- * @author ROMANL
- * @author Andrew C. Oliver (acoliver at apache dot org)
- * @author Danny Mui (danny at muibros.com)
- * @author Amol S. Deshmukh < amol at ap ache dot org >
- */
-public final class TestNamedRange extends TestCase {
-
- private static HSSFWorkbook openSample(String sampleFileName) {
- return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
- }
-
- /** Test of TestCase method, of class test.RangeTest. */
- public void testNamedRange() {
- HSSFWorkbook wb = openSample("Simple.xls");
-
- //Creating new Named Range
- HSSFName newNamedRange = wb.createName();
-
- //Getting Sheet Name for the reference
- String sheetName = wb.getSheetName(0);
-
- //Setting its name
- newNamedRange.setNameName("RangeTest");
- //Setting its reference
- newNamedRange.setRefersToFormula(sheetName + "!$D$4:$E$8");
-
- //Getting NAmed Range
- HSSFName namedRange1 = wb.getNameAt(0);
- //Getting it sheet name
- sheetName = namedRange1.getSheetName();
-
- // sanity check
- SanityChecker c = new SanityChecker();
- c.checkHSSFWorkbook(wb);
-
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- HSSFName nm =wb.getNameAt(wb.getNameIndex("RangeTest"));
- assertTrue("Name is "+nm.getNameName(),"RangeTest".equals(nm.getNameName()));
- assertEquals(wb.getSheetName(0)+"!$D$4:$E$8", nm.getRefersToFormula());
- }
-
- /**
- * Reads an excel file already containing a named range.
- * <p>
- * Addresses Bug <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=9632" target="_bug">#9632</a>
- */
- public void testNamedRead() {
- HSSFWorkbook wb = openSample("namedinput.xls");
-
- //Get index of the namedrange with the name = "NamedRangeName" , which was defined in input.xls as A1:D10
- int NamedRangeIndex = wb.getNameIndex("NamedRangeName");
-
- //Getting NAmed Range
- HSSFName namedRange1 = wb.getNameAt(NamedRangeIndex);
- String sheetName = wb.getSheetName(0);
-
- //Getting its reference
- String reference = namedRange1.getRefersToFormula();
-
- assertEquals(sheetName+"!$A$1:$D$10", reference);
-
- HSSFName namedRange2 = wb.getNameAt(1);
-
- assertEquals(sheetName+"!$D$17:$G$27", namedRange2.getRefersToFormula());
- assertEquals("SecondNamedRange", namedRange2.getNameName());
- }
-
- /**
- * Reads an excel file already containing a named range and updates it
- * <p>
- * Addresses Bug <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=16411" target="_bug">#16411</a>
- */
- public void testNamedReadModify() {
- HSSFWorkbook wb = openSample("namedinput.xls");
-
- HSSFName name = wb.getNameAt(0);
- String sheetName = wb.getSheetName(0);
-
- assertEquals(sheetName+"!$A$1:$D$10", name.getRefersToFormula());
-
- name = wb.getNameAt(1);
- String newReference = sheetName +"!$A$1:$C$36";
-
- name.setRefersToFormula(newReference);
- assertEquals(newReference, name.getRefersToFormula());
- }
-
- /**
- * Test that multiple named ranges can be added written and read
- */
- public void testMultipleNamedWrite() {
- HSSFWorkbook wb = new HSSFWorkbook();
-
-
- wb.createSheet("testSheet1");
- String sheetName = wb.getSheetName(0);
-
- assertEquals("testSheet1", sheetName);
-
- //Creating new Named Range
- HSSFName newNamedRange = wb.createName();
-
- newNamedRange.setNameName("RangeTest");
- newNamedRange.setRefersToFormula(sheetName + "!$D$4:$E$8");
-
- //Creating another new Named Range
- HSSFName newNamedRange2 = wb.createName();
-
- newNamedRange2.setNameName("AnotherTest");
- newNamedRange2.setRefersToFormula(sheetName + "!$F$1:$G$6");
-
- wb.getNameAt(0);
-
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- HSSFName nm =wb.getNameAt(wb.getNameIndex("RangeTest"));
- assertTrue("Name is "+nm.getNameName(),"RangeTest".equals(nm.getNameName()));
- assertTrue("Reference is "+nm.getRefersToFormula(),(wb.getSheetName(0)+"!$D$4:$E$8").equals(nm.getRefersToFormula()));
-
- nm = wb.getNameAt(wb.getNameIndex("AnotherTest"));
- assertTrue("Name is "+nm.getNameName(),"AnotherTest".equals(nm.getNameName()));
- assertTrue("Reference is "+nm.getRefersToFormula(),newNamedRange2.getRefersToFormula().equals(nm.getRefersToFormula()));
- }
-
- /**
- * Test case provided by czhang@cambian.com (Chun Zhang)
- * <p>
- * Addresses Bug <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=13775" target="_bug">#13775</a>
- */
- public void testMultiNamedRange() {
-
- // Create a new workbook
- HSSFWorkbook wb = new HSSFWorkbook ();
-
-
- // Create a worksheet 'sheet1' in the new workbook
- wb.createSheet ();
- wb.setSheetName (0, "sheet1");
-
- // Create another worksheet 'sheet2' in the new workbook
- wb.createSheet ();
- wb.setSheetName (1, "sheet2");
-
- // Create a new named range for worksheet 'sheet1'
- HSSFName namedRange1 = wb.createName();
-
- // Set the name for the named range for worksheet 'sheet1'
- namedRange1.setNameName("RangeTest1");
-
- // Set the reference for the named range for worksheet 'sheet1'
- namedRange1.setRefersToFormula("sheet1" + "!$A$1:$L$41");
-
- // Create a new named range for worksheet 'sheet2'
- HSSFName namedRange2 = wb.createName();
-
- // Set the name for the named range for worksheet 'sheet2'
- namedRange2.setNameName("RangeTest2");
-
- // Set the reference for the named range for worksheet 'sheet2'
- namedRange2.setRefersToFormula("sheet2" + "!$A$1:$O$21");
-
- // Write the workbook to a file
- // Read the Excel file and verify its content
- wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
- HSSFName nm1 =wb.getNameAt(wb.getNameIndex("RangeTest1"));
- assertTrue("Name is "+nm1.getNameName(),"RangeTest1".equals(nm1.getNameName()));
- assertTrue("Reference is "+nm1.getRefersToFormula(),(wb.getSheetName(0)+"!$A$1:$L$41").equals(nm1.getRefersToFormula()));
-
- HSSFName nm2 =wb.getNameAt(wb.getNameIndex("RangeTest2"));
- assertTrue("Name is "+nm2.getNameName(),"RangeTest2".equals(nm2.getNameName()));
- assertTrue("Reference is "+nm2.getRefersToFormula(),(wb.getSheetName(1)+"!$A$1:$O$21").equals(nm2.getRefersToFormula()));
- }
-
- public void testUnicodeNamedRange() {
- HSSFWorkbook workBook = new HSSFWorkbook();
- workBook.createSheet("Test");
- HSSFName name = workBook.createName();
- name.setNameName("\u03B1");
- name.setRefersToFormula("Test!$D$3:$E$8");
-
-
- HSSFWorkbook workBook2 = HSSFTestDataSamples.writeOutAndReadBack(workBook);
- HSSFName name2 = workBook2.getNameAt(0);
-
- assertEquals("\u03B1", name2.getNameName());
- assertEquals("Test!$D$3:$E$8", name2.getRefersToFormula());
- }
-
- /**
- * Test to see if the print areas can be retrieved/created in memory
- */
- public void testSinglePrintArea() {
- HSSFWorkbook workbook = new HSSFWorkbook();
- workbook.createSheet("Test Print Area");
- String sheetName = workbook.getSheetName(0);
-
- String reference = "$A$1:$B$1";
- workbook.setPrintArea(0, reference);
-
- String retrievedPrintArea = workbook.getPrintArea(0);
-
- assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
- assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
- }
-
- /**
- * For Convenience, don't force sheet names to be used
- */
- public void testSinglePrintAreaWOSheet()
- {
- HSSFWorkbook workbook = new HSSFWorkbook();
- workbook.createSheet("Test Print Area");
- String sheetName = workbook.getSheetName(0);
-
- String reference = "$A$1:$B$1";
- workbook.setPrintArea(0, reference);
-
- String retrievedPrintArea = workbook.getPrintArea(0);
-
- assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
- assertEquals("'" + sheetName + "'!" + reference, retrievedPrintArea);
- }
-
- /**
- * Test to see if the print area can be retrieved from an excel created file
- */
- public void testPrintAreaFileRead() {
- HSSFWorkbook workbook = openSample("SimpleWithPrintArea.xls");
-
- String sheetName = workbook.getSheetName(0);
- String reference = sheetName+"!$A$1:$C$5";
-
- assertEquals(reference, workbook.getPrintArea(0));
- }
-
- /**
- * Test to see if the print area made it to the file
- */
- public void testPrintAreaFile() {
- HSSFWorkbook workbook = new HSSFWorkbook();
- workbook.createSheet("Test Print Area");
- String sheetName = workbook.getSheetName(0);
-
-
- String reference = "$A$1:$B$1";
- workbook.setPrintArea(0, reference);
-
- workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook);
-
- String retrievedPrintArea = workbook.getPrintArea(0);
- assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
- assertEquals("References Match", "'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
- }
-
- /**
- * Test to see if multiple print areas made it to the file
- */
- public void testMultiplePrintAreaFile() {
- HSSFWorkbook workbook = new HSSFWorkbook();
-
- workbook.createSheet("Sheet1");
- workbook.createSheet("Sheet2");
- workbook.createSheet("Sheet3");
- String reference1 = "$A$1:$B$1";
- String reference2 = "$B$2:$D$5";
- String reference3 = "$D$2:$F$5";
-
- workbook.setPrintArea(0, reference1);
- workbook.setPrintArea(1, reference2);
- workbook.setPrintArea(2, reference3);
-
- //Check created print areas
- String retrievedPrintArea;
-
- retrievedPrintArea = workbook.getPrintArea(0);
- assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea);
- assertEquals("Sheet1!" + reference1, retrievedPrintArea);
-
- retrievedPrintArea = workbook.getPrintArea(1);
- assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea);
- assertEquals("Sheet2!" + reference2, retrievedPrintArea);
-
- retrievedPrintArea = workbook.getPrintArea(2);
- assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea);
- assertEquals("Sheet3!" + reference3, retrievedPrintArea);
-
- // Check print areas after re-reading workbook
- workbook = HSSFTestDataSamples.writeOutAndReadBack(workbook);
-
- retrievedPrintArea = workbook.getPrintArea(0);
- assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea);
- assertEquals("Sheet1!" + reference1, retrievedPrintArea);
-
- retrievedPrintArea = workbook.getPrintArea(1);
- assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea);
- assertEquals("Sheet2!" + reference2, retrievedPrintArea);
-
- retrievedPrintArea = workbook.getPrintArea(2);
- assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea);
- assertEquals("Sheet3!" + reference3, retrievedPrintArea);
- }
-
- /**
- * Tests the setting of print areas with coordinates (Row/Column designations)
- *
- */
- public void testPrintAreaCoords(){
- HSSFWorkbook workbook = new HSSFWorkbook();
- workbook.createSheet("Test Print Area");
- String sheetName = workbook.getSheetName(0);
-
- workbook.setPrintArea(0, 0, 1, 0, 0);
-
- String retrievedPrintArea = workbook.getPrintArea(0);
-
- assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
- assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
- }
-
-
- /**
- * Tests the parsing of union area expressions, and re-display in the presence of sheet names
- * with special characters.
- */
- public void testPrintAreaUnion(){
- HSSFWorkbook workbook = new HSSFWorkbook();
- workbook.createSheet("Test Print Area");
-
- String reference = "$A$1:$B$1,$D$1:$F$2";
- workbook.setPrintArea(0, reference);
- String retrievedPrintArea = workbook.getPrintArea(0);
- assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
- assertEquals("'Test Print Area'!$A$1:$B$1,'Test Print Area'!$D$1:$F$2", retrievedPrintArea);
- }
-
- /**
- * Verifies an existing print area is deleted
- *
- */
- public void testPrintAreaRemove() {
- HSSFWorkbook workbook = new HSSFWorkbook();
- workbook.createSheet("Test Print Area");
- workbook.getSheetName(0);
-
- workbook.setPrintArea(0, 0, 1, 0, 0);
-
- String retrievedPrintArea = workbook.getPrintArea(0);
-
- assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
-
- workbook.removePrintArea(0);
- assertNull("PrintArea was not removed", workbook.getPrintArea(0));
- }
-
- /**
- * Verifies correct functioning for "single cell named range" (aka "named cell")
- */
- public void testNamedCell_1() {
-
- // setup for this testcase
- String sheetName = "Test Named Cell";
- String cellName = "A name for a named cell";
- String cellValue = "TEST Value";
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet(sheetName);
- sheet.createRow(0).createCell(0).setCellValue(new HSSFRichTextString(cellValue));
-
- // create named range for a single cell using areareference
- HSSFName namedCell = wb.createName();
- namedCell.setNameName(cellName);
- String reference = "'" + sheetName + "'" + "!A1:A1";
- namedCell.setRefersToFormula(reference);
-
- // retrieve the newly created named range
- int namedCellIdx = wb.getNameIndex(cellName);
- HSSFName aNamedCell = wb.getNameAt(namedCellIdx);
- assertNotNull(aNamedCell);
-
- // retrieve the cell at the named range and test its contents
- AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula());
- assertTrue("Should be exactly 1 cell in the named cell :'" +cellName+"'", aref.isSingleCell());
-
- CellReference cref = aref.getFirstCell();
- assertNotNull(cref);
- HSSFSheet s = wb.getSheet(cref.getSheetName());
- assertNotNull(s);
- HSSFRow r = sheet.getRow(cref.getRow());
- HSSFCell c = r.getCell(cref.getCol());
- String contents = c.getRichStringCellValue().getString();
- assertEquals("Contents of cell retrieved by its named reference", contents, cellValue);
- }
-
- /**
- * Verifies correct functioning for "single cell named range" (aka "named cell")
- */
- public void testNamedCell_2() {
-
- // setup for this testcase
- String sname = "TestSheet", cname = "TestName", cvalue = "TestVal";
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet(sname);
- sheet.createRow(0).createCell(0).setCellValue(new HSSFRichTextString(cvalue));
-
- // create named range for a single cell using cellreference
- HSSFName namedCell = wb.createName();
- namedCell.setNameName(cname);
- String reference = sname+"!A1";
- namedCell.setRefersToFormula(reference);
-
- // retrieve the newly created named range
- int namedCellIdx = wb.getNameIndex(cname);
- HSSFName aNamedCell = wb.getNameAt(namedCellIdx);
- assertNotNull(aNamedCell);
-
- // retrieve the cell at the named range and test its contents
- CellReference cref = new CellReference(aNamedCell.getRefersToFormula());
- assertNotNull(cref);
- HSSFSheet s = wb.getSheet(cref.getSheetName());
- HSSFRow r = sheet.getRow(cref.getRow());
- HSSFCell c = r.getCell(cref.getCol());
- String contents = c.getRichStringCellValue().getString();
- assertEquals("Contents of cell retrieved by its named reference", contents, cvalue);
- }
-
- public void testDeletedReference() throws Exception {
- HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("24207.xls");
- assertEquals(2, wb.getNumberOfNames());
-
- HSSFName name1 = wb.getNameAt(0);
- assertEquals("a", name1.getNameName());
- assertEquals("Sheet1!$A$1", name1.getRefersToFormula());
- new AreaReference(name1.getRefersToFormula());
- assertTrue("Successfully constructed first reference", true);
-
- HSSFName name2 = wb.getNameAt(1);
- assertEquals("b", name2.getNameName());
- assertEquals("Sheet1!#REF!", name2.getRefersToFormula());
- assertTrue(name2.isDeleted());
- try {
- new AreaReference(name2.getRefersToFormula());
- fail("attempt to supply an invalid reference to AreaReference constructor results in exception");
- } catch (StringIndexOutOfBoundsException e) { // TODO - use a different exception for this condition
- // expected during successful test
- }
- }
-
- public void testRepeatingRowsAndColumsNames() {
- // First test that setting RR&C for same sheet more than once only creates a
- // single Print_Titles built-in record
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet("FirstSheet");
-
- // set repeating rows and columns twice for the first sheet
- for (int i = 0; i < 2; i++) {
- wb.setRepeatingRowsAndColumns(0, 0, 0, 0, 3-1);
- sheet.createFreezePane(0, 3);
- }
- assertEquals(1, wb.getNumberOfNames());
- HSSFName nr1 = wb.getNameAt(0);
-
- assertEquals("Print_Titles", nr1.getNameName());
- if (false) {
- // TODO - full column references not rendering properly, absolute markers not present either
- assertEquals("FirstSheet!$A:$A,FirstSheet!$1:$3", nr1.getRefersToFormula());
- } else {
- assertEquals("FirstSheet!A:A,FirstSheet!$A$1:$IV$3", nr1.getRefersToFormula());
- }
-
- // Save and re-open
- HSSFWorkbook nwb = HSSFTestDataSamples.writeOutAndReadBack(wb);
-
- assertEquals(1, nwb.getNumberOfNames());
- nr1 = nwb.getNameAt(0);
-
- assertEquals("Print_Titles", nr1.getNameName());
- assertEquals("FirstSheet!A:A,FirstSheet!$A$1:$IV$3", nr1.getRefersToFormula());
-
- // check that setting RR&C on a second sheet causes a new Print_Titles built-in
- // name to be created
- sheet = nwb.createSheet("SecondSheet");
- nwb.setRepeatingRowsAndColumns(1, 1, 2, 0, 0);
-
- assertEquals(2, nwb.getNumberOfNames());
- HSSFName nr2 = nwb.getNameAt(1);
-
- assertEquals("Print_Titles", nr2.getNameName());
- assertEquals("SecondSheet!B:C,SecondSheet!$A$1:$IV$1", nr2.getRefersToFormula());
-
- if (false) {
- // In case you fancy checking in excel, to ensure it
- // won't complain about the file now
- try {
- File tempFile = File.createTempFile("POI-45126-", ".xls");
- FileOutputStream fout = new FileOutputStream(tempFile);
- nwb.write(fout);
- fout.close();
- System.out.println("check out " + tempFile.getAbsolutePath());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- }
-
- /**
- * When setting A1 type of referencese HSSFName.setRefersToFormula
- * must set the type of operands to Ptg.CLASS_REF,
- * otherwise created named don't appear in the dropdown to the left opf formula bar in Excel
- */
- public void testTypeOfRootPtg(){
- HSSFWorkbook wb = new HSSFWorkbook();
- wb.createSheet("CSCO");
-
- Ptg[] ptgs = HSSFFormulaParser.parse("CSCO!$E$71", wb, FormulaType.NAMEDRANGE, 0);
- for (int i = 0; i < ptgs.length; i++) {
- assertEquals('R', ptgs[i].getRVAType());
- }
-
- }
-
- public void testScope() {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sh1 = wb.createSheet();
- HSSFSheet sh2 = wb.createSheet();
-
- HSSFName name;
-
- name = wb.createName();
- name.setNameName("aaa");
- name = wb.createName();
- try {
- name.setNameName("aaa");
- fail("Expected exception");
- } catch(Exception e){
- assertEquals("The workbook already contains this name: aaa", e.getMessage());
- name.setNameName("aaa-2");
- }
-
- name = wb.createName();
- name.setSheetIndex(0);
- name.setNameName("aaa");
- name = wb.createName();
- name.setSheetIndex(0);
- try {
- name.setNameName("aaa");
- fail("Expected exception");
- } catch(Exception e){
- assertEquals("The sheet already contains this name: aaa", e.getMessage());
- name.setNameName("aaa-2");
- }
-
- name = wb.createName();
- name.setSheetIndex(1);
- name.setNameName("aaa");
- name = wb.createName();
- name.setSheetIndex(1);
- try {
- name.setNameName("aaa");
- fail("Expected exception");
- } catch(Exception e){
- assertEquals("The sheet already contains this name: aaa", e.getMessage());
- name.setNameName("aaa-2");
- }
-
- int cnt = 0;
- for (int i = 0; i < wb.getNumberOfNames(); i++) {
- if("aaa".equals(wb.getNameName(i))) cnt++;
- }
- assertEquals(3, cnt);
- }
-}
*/
package org.apache.poi.hssf.usermodel;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.FileInputStream;
-
-import org.apache.poi.util.TempFile;
-
import junit.framework.TestCase;
+import org.apache.poi.hssf.HSSFTestDataSamples;
public class TestUnicodeWorkbook extends TestCase {
- public TestUnicodeWorkbook(String s) {
- super(s);
- }
-
- /** Tests that all of the unicode capable string fields can be set, written and then read back
- *
- *
- */
- public void testUnicodeInAll() throws Exception {
- HSSFWorkbook wb = new HSSFWorkbook();
- //Create a unicode dataformat (contains euro symbol)
- HSSFDataFormat df = wb.createDataFormat();
- final String formatStr = "_([$\u20ac-2]\\\\\\ * #,##0.00_);_([$\u20ac-2]\\\\\\ * \\\\\\(#,##0.00\\\\\\);_([$\u20ac-2]\\\\\\ *\\\"\\-\\\\\"??_);_(@_)";
- short fmt = df.getFormat(formatStr);
-
- //Create a unicode sheet name (euro symbol)
- HSSFSheet s = wb.createSheet("\u20ac");
-
- //Set a unicode header (you guessed it the euro symbol)
- HSSFHeader h = s.getHeader();
- h.setCenter("\u20ac");
- h.setLeft("\u20ac");
- h.setRight("\u20ac");
-
- //Set a unicode footer
- HSSFFooter f = s.getFooter();
- f.setCenter("\u20ac");
- f.setLeft("\u20ac");
- f.setRight("\u20ac");
-
- HSSFRow r = s.createRow(0);
- HSSFCell c = r.createCell(1);
- c.setCellValue(12.34);
- c.getCellStyle().setDataFormat(fmt);
-
- HSSFCell c2 = r.createCell(2);
- c.setCellValue(new HSSFRichTextString("\u20ac"));
-
- HSSFCell c3 = r.createCell(3);
- String formulaString = "TEXT(12.34,\"\u20ac###,##\")";
- c3.setCellFormula(formulaString);
-
-
- File tempFile = TempFile.createTempFile("unicode", "test.xls");
- FileOutputStream stream = new FileOutputStream(tempFile);
- wb.write(stream);
-
- wb = null;
- FileInputStream in = new FileInputStream(tempFile);
- wb = new HSSFWorkbook(in);
-
- //Test the sheetname
- s = wb.getSheet("\u20ac");
- assertNotNull(s);
-
- //Test the header
- h = s.getHeader();
- assertEquals(h.getCenter(), "\u20ac");
- assertEquals(h.getLeft(), "\u20ac");
- assertEquals(h.getRight(), "\u20ac");
-
- //Test the footer
- f = s.getFooter();
- assertEquals(f.getCenter(), "\u20ac");
- assertEquals(f.getLeft(), "\u20ac");
- assertEquals(f.getRight(), "\u20ac");
-
- //Test the dataformat
- r = s.getRow(0);
- c = r.getCell(1);
- df = wb.createDataFormat();
- assertEquals(formatStr, df.getFormat(c.getCellStyle().getDataFormat()));
-
- //Test the cell string value
- c2 = r.getCell(2);
- assertEquals(c.getRichStringCellValue().getString(), "\u20ac");
-
- //Test the cell formula
- c3 = r.getCell(3);
- assertEquals(c3.getCellFormula(), formulaString);
- }
-
/** Tests Bug38230
* That a Umlat is written and then read back.
* It should have been written as a compressed unicode.
- *
- *
+ *
+ *
*
*/
public void testUmlatReadWrite() throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
-
+
//Create a unicode sheet name (euro symbol)
HSSFSheet s = wb.createSheet("test");
-
+
HSSFRow r = s.createRow(0);
HSSFCell c = r.createCell(1);
c.setCellValue(new HSSFRichTextString("\u00e4"));
-
+
//Confirm that the sring will be compressed
assertEquals(c.getRichStringCellValue().getUnicodeString().getOptionFlags(), 0);
-
- File tempFile = TempFile.createTempFile("umlat", "test.xls");
- FileOutputStream stream = new FileOutputStream(tempFile);
- wb.write(stream);
-
- wb = null;
- FileInputStream in = new FileInputStream(tempFile);
- wb = new HSSFWorkbook(in);
+
+ wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
//Test the sheetname
s = wb.getSheet("test");
assertNotNull(s);
-
+
c = r.getCell(1);
assertEquals(c.getRichStringCellValue().getString(), "\u00e4");
- }
+ }
}
*/\r
Workbook createWorkbook();\r
\r
+ /**\r
+ * Opens a sample file from the standard HSSF test data directory\r
+ *\r
+ * @return an open <tt>InputStream</tt> for the specified sample file\r
+ */\r
+ byte[] getTestDataFileContent(String fileName);\r
}\r
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.ss.usermodel;\r
+\r
+import junit.framework.TestCase;\r
+import junit.framework.AssertionFailedError;\r
+import org.apache.poi.ss.ITestDataProvider;\r
+import org.apache.poi.ss.util.CellRangeAddress;\r
+\r
+/**\r
+ * A base class for bugzilla issues that can be described in terms of common ss interfaces.\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public abstract class BaseTestBugzillaIssues extends TestCase {\r
+\r
+ protected abstract ITestDataProvider getTestDataProvider();\r
+\r
+ /**\r
+ *\r
+ * Test writing a hyperlink\r
+ * Open resulting sheet in Excel and check that A1 contains a hyperlink\r
+ *\r
+ * Also tests bug 15353 (problems with hyperlinks to Google)\r
+ */\r
+ public void test23094() {\r
+ Workbook wb = getTestDataProvider().createWorkbook();\r
+ Sheet s = wb.createSheet();\r
+ Row r = s.createRow(0);\r
+ r.createCell(0).setCellFormula("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")");\r
+ r.createCell(1).setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");\r
+\r
+ wb = getTestDataProvider().writeOutAndReadBack(wb);\r
+ r = wb.getSheetAt(0).getRow(0);\r
+\r
+ Cell cell_0 = r.getCell(0);\r
+ assertEquals("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")", cell_0.getCellFormula());\r
+ Cell cell_1 = r.getCell(1);\r
+ assertEquals("HYPERLINK(\"http://google.com\",\"Google\")", cell_1.getCellFormula());\r
+ }\r
+\r
+ /**\r
+ * test writing a file with large number of unique strings,\r
+ * open resulting file in Excel to check results!\r
+ * @param num the number of strings to generate\r
+ */\r
+ public void baseTest15375(int num) {\r
+ Workbook wb = getTestDataProvider().createWorkbook();\r
+ Sheet sheet = wb.createSheet();\r
+ CreationHelper factory = wb.getCreationHelper();\r
+\r
+ String tmp1 = null;\r
+ String tmp2 = null;\r
+ String tmp3 = null;\r
+\r
+ for (int i = 0; i < num; i++) {\r
+ tmp1 = "Test1" + i;\r
+ tmp2 = "Test2" + i;\r
+ tmp3 = "Test3" + i;\r
+\r
+ Row row = sheet.createRow(i);\r
+\r
+ Cell cell = row.createCell(0);\r
+ cell.setCellValue(factory.createRichTextString(tmp1));\r
+ cell = row.createCell(1);\r
+ cell.setCellValue(factory.createRichTextString(tmp2));\r
+ cell = row.createCell(2);\r
+ cell.setCellValue(factory.createRichTextString(tmp3));\r
+ }\r
+ wb = getTestDataProvider().writeOutAndReadBack(wb);\r
+ for (int i = 0; i < num; i++) {\r
+ tmp1 = "Test1" + i;\r
+ tmp2 = "Test2" + i;\r
+ tmp3 = "Test3" + i;\r
+\r
+ Row row = sheet.getRow(i);\r
+\r
+ assertEquals(tmp1, row.getCell(0).getStringCellValue());\r
+ assertEquals(tmp2, row.getCell(1).getStringCellValue());\r
+ assertEquals(tmp3, row.getCell(2).getStringCellValue());\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Merged regions were being removed from the parent in cloned sheets\r
+ */\r
+ public void test22720() {\r
+ Workbook workBook = getTestDataProvider().createWorkbook();\r
+ workBook.createSheet("TEST");\r
+ Sheet template = workBook.getSheetAt(0);\r
+\r
+ template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));\r
+ template.addMergedRegion(new CellRangeAddress(1, 2, 0, 2));\r
+\r
+ Sheet clone = workBook.cloneSheet(0);\r
+ int originalMerged = template.getNumMergedRegions();\r
+ assertEquals("2 merged regions", 2, originalMerged);\r
+\r
+ //remove merged regions from clone\r
+ for (int i=template.getNumMergedRegions()-1; i>=0; i--) {\r
+ clone.removeMergedRegion(i);\r
+ }\r
+\r
+ assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions());\r
+ //check if template's merged regions are OK\r
+ if (template.getNumMergedRegions()>0) {\r
+ // fetch the first merged region...EXCEPTION OCCURS HERE\r
+ template.getMergedRegion(0);\r
+ }\r
+ //make sure we dont exception\r
+\r
+ }\r
+\r
+ public void test28031() {\r
+ Workbook wb = getTestDataProvider().createWorkbook();\r
+ Sheet sheet = wb.createSheet();\r
+ wb.setSheetName(0, "Sheet1");\r
+\r
+ Row row = sheet.createRow(0);\r
+ Cell cell = row.createCell(0);\r
+ String formulaText =\r
+ "IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))";\r
+ cell.setCellFormula(formulaText);\r
+\r
+ assertEquals(formulaText, cell.getCellFormula());\r
+ wb = getTestDataProvider().writeOutAndReadBack(wb);\r
+ cell = wb.getSheetAt(0).getRow(0).getCell(0);\r
+ assertEquals("IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))", cell.getCellFormula());\r
+ }\r
+\r
+ /**\r
+ * Bug 21334: "File error: data may have been lost" with a file\r
+ * that contains macros and this formula:\r
+ * {=SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""))>0,1))}\r
+ */\r
+ public void test21334() {\r
+ Workbook wb = getTestDataProvider().createWorkbook();\r
+ Sheet sh = wb.createSheet();\r
+ Cell cell = sh.createRow(0).createCell(0);\r
+ String formula = "SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"))>0,1))";\r
+ cell.setCellFormula(formula);\r
+\r
+ Workbook wb_sv = getTestDataProvider().writeOutAndReadBack(wb);\r
+ Cell cell_sv = wb_sv.getSheetAt(0).getRow(0).getCell(0);\r
+ assertEquals(formula, cell_sv.getCellFormula());\r
+ }\r
+\r
+ /** another test for the number of unique strings issue\r
+ *test opening the resulting file in Excel*/\r
+ public void test22568() {\r
+ int r=2000;int c=3;\r
+\r
+ Workbook wb = getTestDataProvider().createWorkbook();\r
+ Sheet sheet = wb.createSheet("ExcelTest") ;\r
+\r
+ int col_cnt=0, rw_cnt=0 ;\r
+\r
+ col_cnt = c;\r
+ rw_cnt = r;\r
+\r
+ Row rw ;\r
+ rw = sheet.createRow(0) ;\r
+ //Header row\r
+ for(int j=0; j<col_cnt; j++){\r
+ Cell cell = rw.createCell(j) ;\r
+ cell.setCellValue("Col " + (j+1));\r
+ }\r
+\r
+ for(int i=1; i<rw_cnt; i++){\r
+ rw = sheet.createRow(i) ;\r
+ for(int j=0; j<col_cnt; j++){\r
+ Cell cell = rw.createCell(j) ;\r
+ cell.setCellValue("Row:" + (i+1) + ",Column:" + (j+1));\r
+ }\r
+ }\r
+\r
+ sheet.setDefaultColumnWidth(18) ;\r
+\r
+ wb = getTestDataProvider().writeOutAndReadBack(wb);\r
+ sheet = wb.getSheetAt(0);\r
+ rw = sheet.getRow(0);\r
+ //Header row\r
+ for(int j=0; j<col_cnt; j++){\r
+ Cell cell = rw.getCell(j) ;\r
+ assertEquals("Col " + (j+1), cell.getStringCellValue());\r
+ }\r
+ for(int i=1; i<rw_cnt; i++){\r
+ rw = sheet.getRow(i) ;\r
+ for(int j=0; j<col_cnt; j++){\r
+ Cell cell = rw.getCell(j) ;\r
+ assertEquals("Row:" + (i+1) + ",Column:" + (j+1), cell.getStringCellValue());\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Bug 42448: Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69\r
+ */\r
+ public void test42448(){\r
+ Workbook wb = getTestDataProvider().createWorkbook();\r
+ Cell cell = wb.createSheet().createRow(0).createCell(0);\r
+ cell.setCellFormula("SUMPRODUCT(A!C7:A!C67, B8:B68) / B69");\r
+ assertTrue("no errors parsing formula", true);\r
+ }\r
+\r
+ /**\r
+ * HSSFRichTextString.length() returns negative for really\r
+ * long strings\r
+ */\r
+ public void test46368() {\r
+ Workbook wb = getTestDataProvider().createWorkbook();\r
+ Sheet s = wb.createSheet();\r
+ Row r = s.createRow(0);\r
+ for(int i=0; i<15; i++) {\r
+ int len = 32760 + i;\r
+ Cell c = r.createCell(i);\r
+\r
+ StringBuffer sb = new StringBuffer();\r
+ for(int j=0; j<len; j++) {\r
+ sb.append("x");\r
+ }\r
+ RichTextString rtr = wb.getCreationHelper().createRichTextString(sb.toString());\r
+ assertEquals(len, rtr.length());\r
+ c.setCellValue(rtr);\r
+ }\r
+\r
+ // Save and reload\r
+ wb = getTestDataProvider().writeOutAndReadBack(wb);\r
+ s = wb.getSheetAt(0);\r
+ r = s.getRow(0);\r
+ for(int i=0; i<15; i++) {\r
+ int len = 32760 + i;\r
+ Cell c = r.getCell(i);\r
+ assertEquals(len, c.getRichStringCellValue().length());\r
+ }\r
+ }\r
+\r
+ public void test18800() {\r
+ Workbook book = getTestDataProvider().createWorkbook();\r
+ book.createSheet("TEST");\r
+ Sheet sheet = book.cloneSheet(0);\r
+ book.setSheetName(1,"CLONE");\r
+ sheet.createRow(0).createCell(0).setCellValue("Test");\r
+\r
+ book = getTestDataProvider().writeOutAndReadBack(book);\r
+ sheet = book.getSheet("CLONE");\r
+ Row row = sheet.getRow(0);\r
+ Cell cell = row.getCell(0);\r
+ assertEquals("Test", cell.getRichStringCellValue().getString());\r
+ }\r
+\r
+ private static void addNewSheetWithCellsA1toD4(Workbook book, int sheet) {\r
+\r
+ Sheet sht = book .createSheet("s" + sheet);\r
+ for (int r=0; r < 4; r++) {\r
+\r
+ Row row = sht.createRow (r);\r
+ for (int c=0; c < 4; c++) {\r
+\r
+ Cell cel = row.createCell(c);\r
+ cel.setCellValue(sheet*100 + r*10 + c);\r
+ }\r
+ }\r
+ }\r
+\r
+ public void testBug43093() {\r
+ Workbook xlw = getTestDataProvider().createWorkbook();\r
+\r
+ addNewSheetWithCellsA1toD4(xlw, 1);\r
+ addNewSheetWithCellsA1toD4(xlw, 2);\r
+ addNewSheetWithCellsA1toD4(xlw, 3);\r
+ addNewSheetWithCellsA1toD4(xlw, 4);\r
+\r
+ Sheet s2 = xlw.getSheet("s2");\r
+ Row s2r3 = s2.getRow(3);\r
+ Cell s2E4 = s2r3.createCell(4);\r
+ s2E4.setCellFormula("SUM(s3!B2:C3)");\r
+\r
+ FormulaEvaluator eva = xlw.getCreationHelper().createFormulaEvaluator();\r
+ double d = eva.evaluate(s2E4).getNumberValue();\r
+\r
+ assertEquals(d, (311+312+321+322), 0.0000001);\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.ss.usermodel;\r
+\r
+import junit.framework.TestCase;\r
+import junit.framework.AssertionFailedError;\r
+import org.apache.poi.ss.ITestDataProvider;\r
+import org.apache.poi.ss.util.CellRangeAddress;\r
+\r
+/**\r
+ * @author Yegor Kozlov\r
+ */\r
+public abstract class BaseTestFont extends TestCase {\r
+\r
+ protected abstract ITestDataProvider getTestDataProvider();\r
+\r
+ public void baseTestDefaultFont(String defaultName, short defaultSize, short defaultColor){\r
+ //get default font and check against default value\r
+ Workbook workbook = getTestDataProvider().createWorkbook();\r
+ Font fontFind=workbook.findFont(Font.BOLDWEIGHT_NORMAL, defaultColor, defaultSize, defaultName, false, false, Font.SS_NONE, Font.U_NONE);\r
+ assertNotNull(fontFind);\r
+\r
+ //get default font, then change 2 values and check against different values (height changes)\r
+ Font font=workbook.createFont();\r
+ font.setBoldweight(Font.BOLDWEIGHT_BOLD);\r
+ assertEquals(Font.BOLDWEIGHT_BOLD, font.getBoldweight());\r
+ font.setUnderline(Font.U_DOUBLE);\r
+ assertEquals(Font.U_DOUBLE, font.getUnderline());\r
+ font.setFontHeightInPoints((short)15);\r
+ assertEquals(15*20, font.getFontHeight());\r
+ assertEquals(15, font.getFontHeightInPoints());\r
+ fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, defaultColor, (short)(15*20), defaultName, false, false, Font.SS_NONE, Font.U_DOUBLE);\r
+ assertNotNull(fontFind);\r
+ }\r
+\r
+ public void testGetNumberOfFonts(){\r
+ Workbook wb = getTestDataProvider().createWorkbook();\r
+ int num0 = wb.getNumberOfFonts();\r
+\r
+ Font f1=wb.createFont();\r
+ f1.setBoldweight(Font.BOLDWEIGHT_BOLD);\r
+ short idx1 = f1.getIndex();\r
+ wb.createCellStyle().setFont(f1);\r
+\r
+ Font f2=wb.createFont();\r
+ f2.setUnderline(Font.U_DOUBLE);\r
+ short idx2 = f2.getIndex();\r
+ wb.createCellStyle().setFont(f2);\r
+\r
+ Font f3=wb.createFont();\r
+ f3.setFontHeightInPoints((short)23);\r
+ short idx3 = f3.getIndex();\r
+ wb.createCellStyle().setFont(f3);\r
+\r
+ assertEquals(num0 + 3,wb.getNumberOfFonts());\r
+ assertEquals(Font.BOLDWEIGHT_BOLD,wb.getFontAt(idx1).getBoldweight());\r
+ assertEquals(Font.U_DOUBLE,wb.getFontAt(idx2).getUnderline());\r
+ assertEquals(23,wb.getFontAt(idx3).getFontHeightInPoints());\r
+ }\r
+\r
+ /**\r
+ * Tests that we can define fonts to a new\r
+ * file, save, load, and still see them\r
+ * @throws Exception\r
+ */\r
+ public void testCreateSave() {\r
+ Workbook wb = getTestDataProvider().createWorkbook();\r
+ Sheet s1 = wb.createSheet();\r
+ Row r1 = s1.createRow(0);\r
+ Cell r1c1 = r1.createCell(0);\r
+ r1c1.setCellValue(2.2);\r
+\r
+ int num0 = wb.getNumberOfFonts();\r
+\r
+ Font font=wb.createFont();\r
+ font.setBoldweight(Font.BOLDWEIGHT_BOLD);\r
+ font.setStrikeout(true);\r
+ font.setColor(IndexedColors.YELLOW.getIndex());\r
+ font.setFontName("Courier");\r
+ short font1Idx = font.getIndex();\r
+ wb.createCellStyle().setFont(font);\r
+ assertEquals(num0 + 1, wb.getNumberOfFonts());\r
+\r
+ CellStyle cellStyleTitle=wb.createCellStyle();\r
+ cellStyleTitle.setFont(font);\r
+ r1c1.setCellStyle(cellStyleTitle);\r
+\r
+ // Save and re-load\r
+ wb = getTestDataProvider().writeOutAndReadBack(wb);\r
+ s1 = wb.getSheetAt(0);\r
+\r
+ assertEquals(num0 + 1, wb.getNumberOfFonts());\r
+ short idx = s1.getRow(0).getCell(0).getCellStyle().getFontIndex();\r
+ Font fnt = wb.getFontAt(idx);\r
+ assertNotNull(fnt);\r
+ assertEquals(IndexedColors.YELLOW.getIndex(), fnt.getColor());\r
+ assertEquals("Courier", fnt.getFontName());\r
+\r
+ // Now add an orphaned one\r
+ Font font2 = wb.createFont();\r
+ font2.setItalic(true);\r
+ font2.setFontHeightInPoints((short)15);\r
+ short font2Idx = font2.getIndex();\r
+ wb.createCellStyle().setFont(font2);\r
+ assertEquals(num0 + 2, wb.getNumberOfFonts());\r
+\r
+ // Save and re-load\r
+ wb = getTestDataProvider().writeOutAndReadBack(wb);\r
+ s1 = wb.getSheetAt(0);\r
+\r
+ assertEquals(num0 + 2, wb.getNumberOfFonts());\r
+ assertNotNull(wb.getFontAt(font1Idx));\r
+ assertNotNull(wb.getFontAt(font2Idx));\r
+\r
+ assertEquals(15, wb.getFontAt(font2Idx).getFontHeightInPoints());\r
+ assertEquals(true, wb.getFontAt(font2Idx).getItalic());\r
+ }\r
+\r
+\r
+\r
+ /**\r
+ * Test that fonts get added properly\r
+ *\r
+ * @see org.apache.poi.hssf.usermodel.TestBugs#test45338()\r
+ */\r
+ public void test45338() {\r
+ Workbook wb = getTestDataProvider().createWorkbook();\r
+ int num0 = wb.getNumberOfFonts();\r
+\r
+ Sheet s = wb.createSheet();\r
+ s.createRow(0);\r
+ s.createRow(1);\r
+ s.getRow(0).createCell(0);\r
+ s.getRow(1).createCell(0);\r
+\r
+ //default font\r
+ Font f1 = wb.getFontAt((short)0);\r
+ assertEquals(Font.BOLDWEIGHT_NORMAL, f1.getBoldweight());\r
+\r
+ // Check that asking for the same font\r
+ // multiple times gives you the same thing.\r
+ // Otherwise, our tests wouldn't work!\r
+ assertSame(wb.getFontAt((short)0), wb.getFontAt((short)0));\r
+\r
+ // Look for a new font we have\r
+ // yet to add\r
+ assertNull(\r
+ wb.findFont(\r
+ Font.BOLDWEIGHT_BOLD, (short)123, (short)(22*20),\r
+ "Thingy", false, true, (short)2, (byte)2\r
+ )\r
+ );\r
+\r
+ Font nf = wb.createFont();\r
+ short nfIdx = nf.getIndex();\r
+ assertEquals(num0 + 1, wb.getNumberOfFonts());\r
+\r
+ assertSame(nf, wb.getFontAt(nfIdx));\r
+\r
+ nf.setBoldweight(Font.BOLDWEIGHT_BOLD);\r
+ nf.setColor((short)123);\r
+ nf.setFontHeightInPoints((short)22);\r
+ nf.setFontName("Thingy");\r
+ nf.setItalic(false);\r
+ nf.setStrikeout(true);\r
+ nf.setTypeOffset((short)2);\r
+ nf.setUnderline((byte)2);\r
+\r
+ assertEquals(num0 + 1, wb.getNumberOfFonts());\r
+ assertEquals(nf, wb.getFontAt(nfIdx));\r
+\r
+ assertEquals(wb.getFontAt(nfIdx), wb.getFontAt(nfIdx));\r
+ assertTrue(wb.getFontAt((short)0) != wb.getFontAt(nfIdx));\r
+\r
+ // Find it now\r
+ assertNotNull(\r
+ wb.findFont(\r
+ Font.BOLDWEIGHT_BOLD, (short)123, (short)(22*20),\r
+ "Thingy", false, true, (short)2, (byte)2\r
+ )\r
+ );\r
+ assertSame(nf,\r
+ wb.findFont(\r
+ Font.BOLDWEIGHT_BOLD, (short)123, (short)(22*20),\r
+ "Thingy", false, true, (short)2, (byte)2\r
+ )\r
+ );\r
+ }\r
+\r
+}
\ No newline at end of file
import junit.framework.TestCase;
import org.apache.poi.ss.ITestDataProvider;
+import org.apache.poi.ss.formula.FormulaParser;
+import org.apache.poi.ss.formula.FormulaType;
+import org.apache.poi.ss.util.CellReference;
+import org.apache.poi.ss.util.AreaReference;
+import org.apache.poi.hssf.record.formula.Ptg;
/**
- * Tests of implementation of {@link org.apache.poi.ss.usermodel.Name}
+ * Tests of implementations of {@link org.apache.poi.ss.usermodel.Name}.
*
+ * @author Yegor Kozlov
*/
public abstract class BaseTestNamedRange extends TestCase {
//-1 means the name applies to the entire workbook
name1.setSheetIndex(-1);
assertEquals(-1, name1.getSheetIndex());
+
+ //names cannot be blank and must begin with a letter or underscore and not contain spaces
+ String[] invalidNames = {"", "123", "1Name", "Named Range"};
+ for (String name : invalidNames) {
+ try {
+ name1.setNameName(name);
+ fail("should have thrown exceptiuon due to invalid name: " + name);
+ } catch (IllegalArgumentException e) {
+ // expected during successful test
+ ;
+ }
+ }
}
public void testUnicodeNamedRange() {
}
assertEquals(3, cnt);
}
+
+ /**
+ * Test case provided by czhang@cambian.com (Chun Zhang)
+ * <p>
+ * Addresses Bug <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=13775" target="_bug">#13775</a>
+ */
+ public void testMultiNamedRange() {
+
+ // Create a new workbook
+ Workbook wb = getTestDataProvider().createWorkbook();
+
+ // Create a worksheet 'sheet1' in the new workbook
+ wb.createSheet ();
+ wb.setSheetName (0, "sheet1");
+
+ // Create another worksheet 'sheet2' in the new workbook
+ wb.createSheet ();
+ wb.setSheetName (1, "sheet2");
+
+ // Create a new named range for worksheet 'sheet1'
+ Name namedRange1 = wb.createName();
+
+ // Set the name for the named range for worksheet 'sheet1'
+ namedRange1.setNameName("RangeTest1");
+
+ // Set the reference for the named range for worksheet 'sheet1'
+ namedRange1.setRefersToFormula("sheet1" + "!$A$1:$L$41");
+
+ // Create a new named range for worksheet 'sheet2'
+ Name namedRange2 = wb.createName();
+
+ // Set the name for the named range for worksheet 'sheet2'
+ namedRange2.setNameName("RangeTest2");
+
+ // Set the reference for the named range for worksheet 'sheet2'
+ namedRange2.setRefersToFormula("sheet2" + "!$A$1:$O$21");
+
+ // Write the workbook to a file
+ // Read the Excel file and verify its content
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+ Name nm1 =wb.getNameAt(wb.getNameIndex("RangeTest1"));
+ assertTrue("Name is "+nm1.getNameName(),"RangeTest1".equals(nm1.getNameName()));
+ assertTrue("Reference is "+nm1.getRefersToFormula(),(wb.getSheetName(0)+"!$A$1:$L$41").equals(nm1.getRefersToFormula()));
+
+ Name nm2 =wb.getNameAt(wb.getNameIndex("RangeTest2"));
+ assertTrue("Name is "+nm2.getNameName(),"RangeTest2".equals(nm2.getNameName()));
+ assertTrue("Reference is "+nm2.getRefersToFormula(),(wb.getSheetName(1)+"!$A$1:$O$21").equals(nm2.getRefersToFormula()));
+ }
+
+ /**
+ * Test to see if the print areas can be retrieved/created in memory
+ */
+ public void testSinglePrintArea() {
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ workbook.createSheet("Test Print Area");
+ String sheetName = workbook.getSheetName(0);
+
+ String reference = "$A$1:$B$1";
+ workbook.setPrintArea(0, reference);
+
+ String retrievedPrintArea = workbook.getPrintArea(0);
+
+ assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
+ assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
+ }
+
+ /**
+ * For Convenience, don't force sheet names to be used
+ */
+ public void testSinglePrintAreaWOSheet()
+ {
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ workbook.createSheet("Test Print Area");
+ String sheetName = workbook.getSheetName(0);
+
+ String reference = "$A$1:$B$1";
+ workbook.setPrintArea(0, reference);
+
+ String retrievedPrintArea = workbook.getPrintArea(0);
+
+ assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
+ assertEquals("'" + sheetName + "'!" + reference, retrievedPrintArea);
+ }
+
+ /**
+ * Test to see if the print area made it to the file
+ */
+ public void testPrintAreaFile() {
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ workbook.createSheet("Test Print Area");
+ String sheetName = workbook.getSheetName(0);
+
+
+ String reference = "$A$1:$B$1";
+ workbook.setPrintArea(0, reference);
+
+ workbook = getTestDataProvider().writeOutAndReadBack(workbook);
+
+ String retrievedPrintArea = workbook.getPrintArea(0);
+ assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
+ assertEquals("References Match", "'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
+ }
+
+ /**
+ * Test to see if multiple print areas made it to the file
+ */
+ public void testMultiplePrintAreaFile() {
+ Workbook workbook = getTestDataProvider().createWorkbook();
+
+ workbook.createSheet("Sheet1");
+ workbook.createSheet("Sheet2");
+ workbook.createSheet("Sheet3");
+ String reference1 = "$A$1:$B$1";
+ String reference2 = "$B$2:$D$5";
+ String reference3 = "$D$2:$F$5";
+
+ workbook.setPrintArea(0, reference1);
+ workbook.setPrintArea(1, reference2);
+ workbook.setPrintArea(2, reference3);
+
+ //Check created print areas
+ String retrievedPrintArea;
+
+ retrievedPrintArea = workbook.getPrintArea(0);
+ assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea);
+ assertEquals("Sheet1!" + reference1, retrievedPrintArea);
+
+ retrievedPrintArea = workbook.getPrintArea(1);
+ assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea);
+ assertEquals("Sheet2!" + reference2, retrievedPrintArea);
+
+ retrievedPrintArea = workbook.getPrintArea(2);
+ assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea);
+ assertEquals("Sheet3!" + reference3, retrievedPrintArea);
+
+ // Check print areas after re-reading workbook
+ workbook = getTestDataProvider().writeOutAndReadBack(workbook);
+
+ retrievedPrintArea = workbook.getPrintArea(0);
+ assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea);
+ assertEquals("Sheet1!" + reference1, retrievedPrintArea);
+
+ retrievedPrintArea = workbook.getPrintArea(1);
+ assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea);
+ assertEquals("Sheet2!" + reference2, retrievedPrintArea);
+
+ retrievedPrintArea = workbook.getPrintArea(2);
+ assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea);
+ assertEquals("Sheet3!" + reference3, retrievedPrintArea);
+ }
+
+ /**
+ * Tests the setting of print areas with coordinates (Row/Column designations)
+ *
+ */
+ public void testPrintAreaCoords(){
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ workbook.createSheet("Test Print Area");
+ String sheetName = workbook.getSheetName(0);
+
+ workbook.setPrintArea(0, 0, 1, 0, 0);
+
+ String retrievedPrintArea = workbook.getPrintArea(0);
+
+ assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
+ assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
+ }
+
+
+ /**
+ * Tests the parsing of union area expressions, and re-display in the presence of sheet names
+ * with special characters.
+ */
+ public void testPrintAreaUnion(){
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ workbook.createSheet("Test Print Area");
+
+ String reference = "$A$1:$B$1,$D$1:$F$2";
+ workbook.setPrintArea(0, reference);
+ String retrievedPrintArea = workbook.getPrintArea(0);
+ assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
+ assertEquals("'Test Print Area'!$A$1:$B$1,'Test Print Area'!$D$1:$F$2", retrievedPrintArea);
+ }
+
+ /**
+ * Verifies an existing print area is deleted
+ *
+ */
+ public void testPrintAreaRemove() {
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ workbook.createSheet("Test Print Area");
+ workbook.getSheetName(0);
+
+ workbook.setPrintArea(0, 0, 1, 0, 0);
+
+ String retrievedPrintArea = workbook.getPrintArea(0);
+
+ assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
+
+ workbook.removePrintArea(0);
+ assertNull("PrintArea was not removed", workbook.getPrintArea(0));
+ }
+
+ /**
+ * Test that multiple named ranges can be added written and read
+ */
+ public void testMultipleNamedWrite() {
+ Workbook wb = getTestDataProvider().createWorkbook();
+
+
+ wb.createSheet("testSheet1");
+ String sheetName = wb.getSheetName(0);
+
+ assertEquals("testSheet1", sheetName);
+
+ //Creating new Named Range
+ Name newNamedRange = wb.createName();
+
+ newNamedRange.setNameName("RangeTest");
+ newNamedRange.setRefersToFormula(sheetName + "!$D$4:$E$8");
+
+ //Creating another new Named Range
+ Name newNamedRange2 = wb.createName();
+
+ newNamedRange2.setNameName("AnotherTest");
+ newNamedRange2.setRefersToFormula(sheetName + "!$F$1:$G$6");
+
+ wb.getNameAt(0);
+
+ wb = getTestDataProvider().writeOutAndReadBack(wb);
+ Name nm =wb.getNameAt(wb.getNameIndex("RangeTest"));
+ assertTrue("Name is "+nm.getNameName(),"RangeTest".equals(nm.getNameName()));
+ assertTrue("Reference is "+nm.getRefersToFormula(),(wb.getSheetName(0)+"!$D$4:$E$8").equals(nm.getRefersToFormula()));
+
+ nm = wb.getNameAt(wb.getNameIndex("AnotherTest"));
+ assertTrue("Name is "+nm.getNameName(),"AnotherTest".equals(nm.getNameName()));
+ assertTrue("Reference is "+nm.getRefersToFormula(),newNamedRange2.getRefersToFormula().equals(nm.getRefersToFormula()));
+ }
+ /**
+ * Verifies correct functioning for "single cell named range" (aka "named cell")
+ */
+ public void testNamedCell_1() {
+
+ // setup for this testcase
+ String sheetName = "Test Named Cell";
+ String cellName = "named_cell";
+ String cellValue = "TEST Value";
+ Workbook wb = getTestDataProvider().createWorkbook();
+ Sheet sheet = wb.createSheet(sheetName);
+ CreationHelper factory = wb.getCreationHelper();
+ sheet.createRow(0).createCell(0).setCellValue(factory.createRichTextString(cellValue));
+
+ // create named range for a single cell using areareference
+ Name namedCell = wb.createName();
+ namedCell.setNameName(cellName);
+ String reference = "'" + sheetName + "'" + "!A1:A1";
+ namedCell.setRefersToFormula(reference);
+
+ // retrieve the newly created named range
+ int namedCellIdx = wb.getNameIndex(cellName);
+ Name aNamedCell = wb.getNameAt(namedCellIdx);
+ assertNotNull(aNamedCell);
+
+ // retrieve the cell at the named range and test its contents
+ AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula());
+ assertTrue("Should be exactly 1 cell in the named cell :'" +cellName+"'", aref.isSingleCell());
+
+ CellReference cref = aref.getFirstCell();
+ assertNotNull(cref);
+ Sheet s = wb.getSheet(cref.getSheetName());
+ assertNotNull(s);
+ Row r = sheet.getRow(cref.getRow());
+ Cell c = r.getCell(cref.getCol());
+ String contents = c.getRichStringCellValue().getString();
+ assertEquals("Contents of cell retrieved by its named reference", contents, cellValue);
+ }
+
+ /**
+ * Verifies correct functioning for "single cell named range" (aka "named cell")
+ */
+ public void testNamedCell_2() {
+
+ // setup for this testcase
+ String sname = "TestSheet", cname = "TestName", cvalue = "TestVal";
+ Workbook wb = getTestDataProvider().createWorkbook();
+ CreationHelper factory = wb.getCreationHelper();
+ Sheet sheet = wb.createSheet(sname);
+ sheet.createRow(0).createCell(0).setCellValue(factory.createRichTextString(cvalue));
+
+ // create named range for a single cell using cellreference
+ Name namedCell = wb.createName();
+ namedCell.setNameName(cname);
+ String reference = sname+"!A1";
+ namedCell.setRefersToFormula(reference);
+
+ // retrieve the newly created named range
+ int namedCellIdx = wb.getNameIndex(cname);
+ Name aNamedCell = wb.getNameAt(namedCellIdx);
+ assertNotNull(aNamedCell);
+
+ // retrieve the cell at the named range and test its contents
+ CellReference cref = new CellReference(aNamedCell.getRefersToFormula());
+ assertNotNull(cref);
+ Sheet s = wb.getSheet(cref.getSheetName());
+ Row r = sheet.getRow(cref.getRow());
+ Cell c = r.getCell(cref.getCol());
+ String contents = c.getRichStringCellValue().getString();
+ assertEquals("Contents of cell retrieved by its named reference", contents, cvalue);
+ }
+
}
\ No newline at end of file
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.ss.usermodel;\r
+\r
+import junit.framework.TestCase;\r
+import org.apache.poi.ss.ITestDataProvider;\r
+\r
+/**\r
+ * @author Yegor Kozlov\r
+ */\r
+public abstract class BaseTestPicture extends TestCase {\r
+\r
+ protected abstract ITestDataProvider getTestDataProvider();\r
+\r
+ public void baseTestResize(ClientAnchor referenceAnchor) {\r
+ Workbook wb = getTestDataProvider().createWorkbook();\r
+ Sheet sh1 = wb.createSheet();\r
+ Drawing p1 = sh1.createDrawingPatriarch();\r
+ CreationHelper factory = wb.getCreationHelper();\r
+\r
+ byte[] pictureData = getTestDataProvider().getTestDataFileContent("logoKarmokar4.png");\r
+ int idx1 = wb.addPicture( pictureData, Workbook.PICTURE_TYPE_PNG );\r
+ Picture picture = p1.createPicture(factory.createClientAnchor(), idx1);\r
+ picture.resize();\r
+ ClientAnchor anchor1 = picture.getPreferredSize();\r
+\r
+ //assert against what would BiffViewer print if we insert the image in xls and dump the file\r
+ assertEquals(referenceAnchor.getCol1(), anchor1.getCol1());\r
+ assertEquals(referenceAnchor.getRow1(), anchor1.getRow1());\r
+ assertEquals(referenceAnchor.getCol2(), anchor1.getCol2());\r
+ assertEquals(referenceAnchor.getRow2(), anchor1.getRow2());\r
+ assertEquals(referenceAnchor.getDx1(), anchor1.getDx1());\r
+ assertEquals(referenceAnchor.getDy1(), anchor1.getDy1());\r
+ assertEquals(referenceAnchor.getDx2(), anchor1.getDx2());\r
+ assertEquals(referenceAnchor.getDy2(), anchor1.getDy2());\r
+ }\r
+\r
+\r
+}
\ No newline at end of file
assertEquals(-1, row.getFirstCellNum());
assertEquals(-1, row.getLastCellNum());
+ //getting cells from an empty row should returns null
+ for(int i=0; i < 10; i++) assertNull(row.getCell(i));
+
row.createCell(2);
assertEquals(2, row.getFirstCellNum());
assertEquals(3, row.getLastCellNum());
assertFalse(sheet.isColumnBroken(11));
assertFalse(sheet.isColumnBroken(12));
}
+
+ public void testGetFirstLastRowNum() {
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ Sheet sheet = workbook.createSheet("Sheet 1");
+ Row row10 = sheet.createRow(9);
+ Row row1 = sheet.createRow(0);
+ Row row2 = sheet.createRow(1);
+ assertEquals(0, sheet.getFirstRowNum());
+ assertEquals(9, sheet.getLastRowNum());
+ }
+
+ public void testGetFooter() {
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ Sheet sheet = workbook.createSheet("Sheet 1");
+ assertNotNull(sheet.getFooter());
+ sheet.getFooter().setCenter("test center footer");
+ assertEquals("test center footer", sheet.getFooter().getCenter());
+ }
+
+ public void testGetSetColumnHidden() {
+ Workbook workbook = getTestDataProvider().createWorkbook();
+ Sheet sheet = workbook.createSheet("Sheet 1");
+ sheet.setColumnHidden(2, true);
+ assertTrue(sheet.isColumnHidden(2));
+ }
+
+
}
\ No newline at end of file
assertSame(sheet, cell.getSheet());\r
assertSame(row, cell.getRow());\r
}\r
+\r
+ /** Tests that all of the unicode capable string fields can be set, written and then read back\r
+ *\r
+ *\r
+ */\r
+ public void testUnicodeInAll() throws Exception {\r
+ Workbook wb = getTestDataProvider().createWorkbook();\r
+ CreationHelper factory = wb.getCreationHelper();\r
+ //Create a unicode dataformat (contains euro symbol)\r
+ DataFormat df = wb.createDataFormat();\r
+ final String formatStr = "_([$\u20ac-2]\\\\\\ * #,##0.00_);_([$\u20ac-2]\\\\\\ * \\\\\\(#,##0.00\\\\\\);_([$\u20ac-2]\\\\\\ *\\\"\\-\\\\\"??_);_(@_)";\r
+ short fmt = df.getFormat(formatStr);\r
+\r
+ //Create a unicode sheet name (euro symbol)\r
+ Sheet s = wb.createSheet("\u20ac");\r
+\r
+ //Set a unicode header (you guessed it the euro symbol)\r
+ Header h = s.getHeader();\r
+ h.setCenter("\u20ac");\r
+ h.setLeft("\u20ac");\r
+ h.setRight("\u20ac");\r
+\r
+ //Set a unicode footer\r
+ Footer f = s.getFooter();\r
+ f.setCenter("\u20ac");\r
+ f.setLeft("\u20ac");\r
+ f.setRight("\u20ac");\r
+\r
+ Row r = s.createRow(0);\r
+ Cell c = r.createCell(1);\r
+ c.setCellValue(12.34);\r
+ c.getCellStyle().setDataFormat(fmt);\r
+\r
+ Cell c2 = r.createCell(2);\r
+ c.setCellValue(factory.createRichTextString("\u20ac"));\r
+\r
+ Cell c3 = r.createCell(3);\r
+ String formulaString = "TEXT(12.34,\"\u20ac###,##\")";\r
+ c3.setCellFormula(formulaString);\r
+\r
+ wb = getTestDataProvider().writeOutAndReadBack(wb);\r
+\r
+ //Test the sheetname\r
+ s = wb.getSheet("\u20ac");\r
+ assertNotNull(s);\r
+\r
+ //Test the header\r
+ h = s.getHeader();\r
+ assertEquals(h.getCenter(), "\u20ac");\r
+ assertEquals(h.getLeft(), "\u20ac");\r
+ assertEquals(h.getRight(), "\u20ac");\r
+\r
+ //Test the footer\r
+ f = s.getFooter();\r
+ assertEquals(f.getCenter(), "\u20ac");\r
+ assertEquals(f.getLeft(), "\u20ac");\r
+ assertEquals(f.getRight(), "\u20ac");\r
+\r
+ //Test the dataformat\r
+ r = s.getRow(0);\r
+ c = r.getCell(1);\r
+ df = wb.createDataFormat();\r
+ assertEquals(formatStr, df.getFormat(c.getCellStyle().getDataFormat()));\r
+\r
+ //Test the cell string value\r
+ c2 = r.getCell(2);\r
+ assertEquals(c.getRichStringCellValue().getString(), "\u20ac");\r
+\r
+ //Test the cell formula\r
+ c3 = r.getCell(3);\r
+ assertEquals(c3.getCellFormula(), formulaString);\r
+ }\r
+ \r
}\r