📄 testformulas.java
字号:
HSSFCell c = null; //get our minimum values r = s.getRow((short)0); c = r.getCell((short)1); //get our minimum values assertTrue("minval Formula is as expected A2"+operator+"A3 != "+c.getCellFormula(), ( ("A2"+operator+"A3").equals(c.getCellFormula()) )); for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) { r = s.getRow((short) x); for (short y = 1; y < 256 && y > 0; y++) { String ref=null; String ref2=null; short refx1=0; short refy1=0; short refx2=0; short refy2=0; if (x +50 < Short.MAX_VALUE) { refx1=(short)(x+50); refx2=(short)(x+46); } else { refx1=(short)(x-4); refx2=(short)(x-3); } if (y+50 < 255) { refy1=(short)(y+50); refy2=(short)(y+49); } else { refy1=(short)(y-4); refy2=(short)(y-3); } c = r.getCell((short) y); CellReference cr= new CellReference(refx1,refy1); ref=cr.toString(); cr=new CellReference(refx2,refy2); ref2=cr.toString(); assertTrue("loop Formula is as expected "+ref+operator+ref2+"!="+c.getCellFormula(),( (""+ref+operator+ref2).equals(c.getCellFormula()) ) ); } } //test our maximum values r = s.getRow((short)0); c = r.getCell((short)0); assertTrue("maxval Formula is as expected",( ("B1"+operator+"IV255").equals(c.getCellFormula()) ) ); in.close(); assertTrue("file exists",file.exists()); } /** * tests order wrting out == order writing in for a given formula */ private void orderTest(String formula) throws Exception { File file = File.createTempFile("testFormula",".xls"); FileOutputStream out = new FileOutputStream(file); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = null; HSSFCell c = null; //get our minimum values r = s.createRow((short)0); c = r.createCell((short)1); c.setCellFormula(formula); wb.write(out); out.close(); assertTrue("file exists",file.exists()); FileInputStream in = new FileInputStream(file); wb = new HSSFWorkbook(in); s = wb.getSheetAt(0); //get our minimum values r = s.getRow((short)0); c = r.getCell((short)1); assertTrue("minval Formula is as expected", formula.equals(c.getCellFormula()) ); in.close(); } /** * All multi-binomial operator tests use this to create a worksheet with a * huge set of x operator y formulas. Next we call binomialVerify and verify * that they are all how we expect. */ private void binomialOperator(String operator) throws Exception { short rownum = 0; File file = File.createTempFile("testFormula",".xls"); FileOutputStream out = new FileOutputStream(file); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = null; HSSFCell c = null; //get our minimum values r = s.createRow((short)0); c = r.createCell((short)1); c.setCellFormula(1 + operator + 1); for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) { r = s.createRow((short) x); for (short y = 1; y < 256 && y > 0; y++) { c = r.createCell((short) y); c.setCellFormula("" + x + operator + y); } } //make sure we do the maximum value of the Int operator if (s.getLastRowNum() < Short.MAX_VALUE) { r = s.createRow((short)0); c = r.createCell((short)0); c.setCellFormula("" + Short.MAX_VALUE + operator + Short.MAX_VALUE); } wb.write(out); out.close(); assertTrue("file exists",file.exists()); binomialVerify(operator,file); } /** * Opens the sheet we wrote out by binomialOperator and makes sure the formulas * all match what we expect (x operator y) */ private void binomialVerify(String operator, File file) throws Exception { short rownum = 0; FileInputStream in = new FileInputStream(file); HSSFWorkbook wb = new HSSFWorkbook(in); HSSFSheet s = wb.getSheetAt(0); HSSFRow r = null; HSSFCell c = null; //get our minimum values r = s.getRow((short)0); c = r.getCell((short)1); assertTrue("minval Formula is as expected 1"+operator+"1 != "+c.getCellFormula(), ( ("1"+operator+"1").equals(c.getCellFormula()) )); for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) { r = s.getRow((short) x); for (short y = 1; y < 256 && y > 0; y++) { c = r.getCell((short) y); assertTrue("loop Formula is as expected "+x+operator+y+"!="+c.getCellFormula(),( (""+x+operator+y).equals(c.getCellFormula()) ) ); } } //test our maximum values r = s.getRow((short)0); c = r.getCell((short)0); assertTrue("maxval Formula is as expected",( (""+Short.MAX_VALUE+operator+Short.MAX_VALUE).equals(c.getCellFormula()) ) ); in.close(); assertTrue("file exists",file.exists()); } /** * Writes a function then tests to see if its correct * */ public void areaFunctionTest(String function) throws Exception { short rownum = 0; File file = File.createTempFile("testFormulaAreaFunction"+function,".xls"); FileOutputStream out = new FileOutputStream(file); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = null; HSSFCell c = null; r = s.createRow((short) 0); c = r.createCell((short) 0); c.setCellFormula(function+"(A2:A3)"); wb.write(out); out.close(); assertTrue("file exists",file.exists()); FileInputStream in = new FileInputStream(file); wb = new HSSFWorkbook(in); s = wb.getSheetAt(0); r = s.getRow(0); c = r.getCell((short)0); assertTrue("function ="+function+"(A2:A3)", ( (function+"(A2:A3)").equals((function+"(A2:A3)")) ) ); in.close(); } /** * Writes a function then tests to see if its correct * */ public void refArrayFunctionTest(String function) throws Exception { short rownum = 0; File file = File.createTempFile("testFormulaArrayFunction"+function,".xls"); FileOutputStream out = new FileOutputStream(file); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = null; HSSFCell c = null; r = s.createRow((short) 0); c = r.createCell((short) 0); c.setCellFormula(function+"(A2,A3)"); wb.write(out); out.close(); assertTrue("file exists",file.exists()); FileInputStream in = new FileInputStream(file); wb = new HSSFWorkbook(in); s = wb.getSheetAt(0); r = s.getRow(0); c = r.getCell((short)0); assertTrue("function ="+function+"(A2,A3)", ( (function+"(A2,A3)").equals(c.getCellFormula()) ) ); in.close(); } /** * Writes a function then tests to see if its correct * */ public void refAreaArrayFunctionTest(String function) throws Exception { short rownum = 0; File file = File.createTempFile("testFormulaAreaArrayFunction"+function,".xls"); FileOutputStream out = new FileOutputStream(file); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = null; HSSFCell c = null; r = s.createRow((short) 0); c = r.createCell((short) 0); c.setCellFormula(function+"(A2:A4,B2:B4)"); c=r.createCell((short) 1); c.setCellFormula(function+"($A$2:$A4,B$2:B4)"); wb.write(out); out.close(); assertTrue("file exists",file.exists()); FileInputStream in = new FileInputStream(file); wb = new HSSFWorkbook(in); s = wb.getSheetAt(0); r = s.getRow(0); c = r.getCell((short)0); assertTrue("function ="+function+"(A2:A4,B2:B4)", ( (function+"(A2:A4,B2:B4)").equals(c.getCellFormula()) ) ); c=r.getCell((short) 1); assertTrue("function ="+function+"($A$2:$A4,B$2:B4)", ( (function+"($A$2:$A4,B$2:B4)").equals(c.getCellFormula()) ) ); in.close(); } public void testAbsRefs() throws Exception { File file = File.createTempFile("testFormulaAbsRef",".xls"); FileOutputStream out = new FileOutputStream(file); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = null; HSSFCell c = null; r = s.createRow((short) 0); c = r.createCell((short) 0); c.setCellFormula("A3+A2"); c=r.createCell( (short) 1); c.setCellFormula("$A3+$A2"); c=r.createCell( (short) 2); c.setCellFormula("A$3+A$2"); c=r.createCell( (short) 3); c.setCellFormula("$A$3+$A$2"); c=r.createCell( (short) 4); c.setCellFormula("SUM($A$3,$A$2)"); wb.write(out); out.close(); assertTrue("file exists",file.exists()); FileInputStream in = new FileInputStream(file); wb = new HSSFWorkbook(in); s = wb.getSheetAt(0); r = s.getRow(0); c = r.getCell((short)0); assertTrue("A3+A2", ("A3+A2").equals(c.getCellFormula())); c = r.getCell((short)1); assertTrue("$A3+$A2", ("$A3+$A2").equals(c.getCellFormula())); c = r.getCell((short)2); assertTrue("A$3+A$2", ("A$3+A$2").equals(c.getCellFormula())); c = r.getCell((short)3); assertTrue("$A$3+$A$2", ("$A$3+$A$2").equals(c.getCellFormula())); c = r.getCell((short)4); assertTrue("SUM($A$3,$A$2)", ("SUM($A$3,$A$2)").equals(c.getCellFormula())); in.close(); } public void testSheetFunctions() throws IOException { String filename = System.getProperty("HSSF.testdata.path"); File file = File.createTempFile("testSheetFormula",".xls"); FileOutputStream out = new FileOutputStream(file); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet("A");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -