2002-04-27 13:26:38 -04:00
|
|
|
|
|
|
|
/* ====================================================================
|
2006-12-22 14:18:16 -05:00
|
|
|
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
|
2004-04-09 09:05:39 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
==================================================================== */
|
|
|
|
|
2002-04-27 13:26:38 -04:00
|
|
|
|
|
|
|
package org.apache.poi.hssf.usermodel;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
2003-03-16 23:47:58 -05:00
|
|
|
import java.io.IOException;
|
2002-04-27 13:26:38 -04:00
|
|
|
import java.util.Date;
|
2003-03-16 23:47:58 -05:00
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
import org.apache.poi.hssf.util.CellReference;
|
2004-10-12 01:49:01 -04:00
|
|
|
import org.apache.poi.util.TempFile;
|
2002-04-27 13:26:38 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Andrew C. Oliver (acoliver at apache dot org)
|
2002-04-29 20:18:47 -04:00
|
|
|
* @author Avik Sengupta
|
2002-04-27 13:26:38 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
public class TestFormulas
|
2002-04-27 20:45:10 -04:00
|
|
|
extends TestCase {
|
|
|
|
public TestFormulas(String s) {
|
2002-04-27 13:26:38 -04:00
|
|
|
super(s);
|
|
|
|
}
|
2002-04-27 20:45:10 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add 1+1 -- WHoohoo!
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void testBasicAddIntegers()
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
short rownum = 0;
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testFormula",".xls");
|
2002-04-27 20:45:10 -04:00
|
|
|
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)1);
|
2002-04-28 11:42:42 -04:00
|
|
|
c = r.createCell((short)1);
|
2002-04-27 20:45:10 -04:00
|
|
|
c.setCellFormula(1 + "+" + 1);
|
|
|
|
|
|
|
|
wb.write(out);
|
2002-04-28 11:42:42 -04:00
|
|
|
out.close();
|
2002-04-27 20:45:10 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
FileInputStream in = new FileInputStream(file);
|
|
|
|
wb = new HSSFWorkbook(in);
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
r = s.getRow((short)1);
|
|
|
|
c = r.getCell((short)1);
|
|
|
|
|
|
|
|
assertTrue("Formula is as expected",("1+1".equals(c.getCellFormula())));
|
|
|
|
in.close();
|
2002-04-27 20:45:10 -04:00
|
|
|
}
|
|
|
|
|
2002-04-27 13:26:38 -04:00
|
|
|
/**
|
|
|
|
* Add various integers
|
|
|
|
*/
|
2002-04-27 20:45:10 -04:00
|
|
|
|
2002-04-27 13:26:38 -04:00
|
|
|
public void testAddIntegers()
|
2002-04-27 20:45:10 -04:00
|
|
|
throws Exception {
|
2002-04-27 22:03:53 -04:00
|
|
|
binomialOperator("+");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Multiply various integers
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void testMultplyIntegers()
|
|
|
|
throws Exception {
|
|
|
|
binomialOperator("*");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtract various integers
|
2002-04-28 11:42:42 -04:00
|
|
|
*/
|
2002-04-27 22:03:53 -04:00
|
|
|
public void testSubtractIntegers()
|
|
|
|
throws Exception {
|
|
|
|
binomialOperator("-");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subtract various integers
|
2002-04-28 11:42:42 -04:00
|
|
|
*/
|
2002-04-27 22:03:53 -04:00
|
|
|
public void testDivideIntegers()
|
|
|
|
throws Exception {
|
|
|
|
binomialOperator("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exponentialize various integers;
|
|
|
|
*/
|
2002-04-28 12:10:00 -04:00
|
|
|
public void testPowerIntegers()
|
2002-04-27 22:03:53 -04:00
|
|
|
throws Exception {
|
|
|
|
binomialOperator("^");
|
|
|
|
}
|
2002-04-28 12:10:00 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Concatinate two numbers 1&2 = 12
|
|
|
|
*/
|
|
|
|
public void testConcatIntegers()
|
|
|
|
throws Exception {
|
|
|
|
binomialOperator("&");
|
|
|
|
}
|
2002-04-27 22:03:53 -04:00
|
|
|
|
2002-04-28 12:10:00 -04:00
|
|
|
/**
|
|
|
|
* tests 1*2+3*4
|
|
|
|
*/
|
|
|
|
public void testOrderOfOperationsMultiply()
|
|
|
|
throws Exception {
|
|
|
|
orderTest("1*2+3*4");
|
|
|
|
}
|
2002-04-27 22:03:53 -04:00
|
|
|
|
2002-04-28 12:10:00 -04:00
|
|
|
/**
|
|
|
|
* tests 1*2+3^4
|
|
|
|
*/
|
|
|
|
public void testOrderOfOperationsPower()
|
|
|
|
throws Exception {
|
|
|
|
orderTest("1*2+3^4");
|
|
|
|
}
|
2002-04-28 12:40:18 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that parenthesis are obeyed
|
|
|
|
*/
|
|
|
|
public void testParenthesis()
|
|
|
|
throws Exception {
|
|
|
|
orderTest("(1*3)+2+(1+2)*(3^4)^5");
|
|
|
|
}
|
2002-04-28 16:51:55 -04:00
|
|
|
|
|
|
|
public void testReferencesOpr()
|
|
|
|
throws Exception {
|
|
|
|
String[] operation = new String[] {
|
|
|
|
"+", "-", "*", "/", "^", "&"
|
|
|
|
};
|
|
|
|
for (int k = 0; k < operation.length; k++) {
|
|
|
|
operationRefTest(operation[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-29 22:12:25 -04:00
|
|
|
/**
|
|
|
|
* Tests creating a file with floating point in a formula.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public void testFloat()
|
2002-04-30 21:11:05 -04:00
|
|
|
throws Exception {
|
|
|
|
floatTest("*");
|
|
|
|
floatTest("/");
|
|
|
|
}
|
2002-04-29 22:12:25 -04:00
|
|
|
|
2002-04-30 21:11:05 -04:00
|
|
|
private void floatTest(String operator)
|
|
|
|
throws Exception {
|
|
|
|
short rownum = 0;
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testFormulaFloat",".xls");
|
2002-04-30 21:11:05 -04:00
|
|
|
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(""+Float.MIN_VALUE + operator + Float.MIN_VALUE);
|
|
|
|
|
|
|
|
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= (short) (y +2)) {
|
|
|
|
|
|
|
|
c = r.createCell((short) y);
|
|
|
|
c.setCellFormula("" + x+"."+y + operator + y +"."+x);
|
|
|
|
|
|
|
|
|
2002-04-29 22:12:25 -04:00
|
|
|
}
|
2002-04-30 21:11:05 -04:00
|
|
|
}
|
|
|
|
if (s.getLastRowNum() < Short.MAX_VALUE) {
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c = r.createCell((short)0);
|
|
|
|
c.setCellFormula("" + Float.MAX_VALUE + operator + Float.MAX_VALUE);
|
|
|
|
}
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
assertTrue("file exists",file.exists());
|
|
|
|
out=null;wb=null; //otherwise we get out of memory error!
|
|
|
|
floatVerify(operator,file);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void floatVerify(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;
|
|
|
|
|
|
|
|
// dont know how to check correct result .. for the moment, we just verify that the file can be read.
|
|
|
|
|
|
|
|
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
|
|
|
|
r = s.getRow((short) x);
|
2002-04-29 22:12:25 -04:00
|
|
|
|
2002-04-30 21:11:05 -04:00
|
|
|
for (short y = 1; y < 256 && y > 0; y=(short)(y+2)) {
|
2002-04-29 22:12:25 -04:00
|
|
|
|
2002-04-30 21:11:05 -04:00
|
|
|
c = r.getCell((short) y);
|
|
|
|
assertTrue("got a formula",c.getCellFormula()!=null);
|
|
|
|
|
|
|
|
assertTrue("loop Formula is as expected "+x+"."+y+operator+y+"."+x+"!="+c.getCellFormula(),(
|
|
|
|
(""+x+"."+y+operator+y+"."+x).equals(c.getCellFormula()) ));
|
|
|
|
|
|
|
|
}
|
2002-04-29 22:12:25 -04:00
|
|
|
}
|
2002-04-30 21:11:05 -04:00
|
|
|
|
|
|
|
in.close();
|
|
|
|
assertTrue("file exists",file.exists());
|
|
|
|
}
|
2002-04-29 22:12:25 -04:00
|
|
|
|
|
|
|
public void testAreaSum()
|
|
|
|
throws Exception {
|
|
|
|
areaFunctionTest("SUM");
|
|
|
|
}
|
|
|
|
|
2002-04-30 21:11:05 -04:00
|
|
|
public void testAreaAverage()
|
|
|
|
throws Exception {
|
|
|
|
areaFunctionTest("AVERAGE");
|
|
|
|
}
|
2002-04-29 22:12:25 -04:00
|
|
|
|
2002-04-30 21:41:51 -04:00
|
|
|
public void testRefArraySum()
|
|
|
|
throws Exception {
|
|
|
|
refArrayFunctionTest("SUM");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testAreaArraySum()
|
|
|
|
throws Exception {
|
|
|
|
refAreaArrayFunctionTest("SUM");
|
|
|
|
}
|
|
|
|
|
2002-04-30 21:19:28 -04:00
|
|
|
|
|
|
|
|
2002-04-28 16:51:55 -04:00
|
|
|
private void operationRefTest(String operator)
|
|
|
|
throws Exception {
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testFormula",".xls");
|
2002-04-28 16:51:55 -04:00
|
|
|
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("A2" + operator + "A3");
|
|
|
|
|
|
|
|
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++) {
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2002-06-11 16:20:41 -04:00
|
|
|
c = r.getCell((short) y);
|
2008-02-15 07:04:42 -05:00
|
|
|
CellReference cr= new CellReference(refx1,refy1, false, false);
|
|
|
|
ref=cr.formatAsString();
|
|
|
|
cr=new CellReference(refx2,refy2, false, false);
|
|
|
|
ref2=cr.formatAsString();
|
2002-04-28 16:51:55 -04:00
|
|
|
|
|
|
|
c = r.createCell((short) y);
|
|
|
|
c.setCellFormula("" + ref + operator + ref2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//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("" + "B1" + operator + "IV255");
|
|
|
|
}
|
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
assertTrue("file exists",file.exists());
|
|
|
|
operationalRefVerify(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 operationalRefVerify(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);
|
|
|
|
//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);
|
2008-02-15 07:04:42 -05:00
|
|
|
CellReference cr= new CellReference(refx1, refy1, false, false);
|
|
|
|
ref=cr.formatAsString();
|
|
|
|
cr=new CellReference(refx2,refy2, false, false);
|
|
|
|
ref2=cr.formatAsString();
|
2002-04-28 16:51:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-28 11:42:42 -04:00
|
|
|
|
2002-04-28 12:10:00 -04:00
|
|
|
/**
|
|
|
|
* tests order wrting out == order writing in for a given formula
|
|
|
|
*/
|
|
|
|
private void orderTest(String formula)
|
|
|
|
throws Exception {
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testFormula",".xls");
|
2002-04-28 12:10:00 -04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2002-04-28 11:42:42 -04:00
|
|
|
/**
|
|
|
|
* 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)
|
2002-04-27 22:03:53 -04:00
|
|
|
throws Exception {
|
2002-04-27 13:26:38 -04:00
|
|
|
short rownum = 0;
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testFormula",".xls");
|
2002-04-27 13:26:38 -04:00
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
|
2002-04-27 20:45:10 -04:00
|
|
|
//get our minimum values
|
2002-04-27 22:03:53 -04:00
|
|
|
r = s.createRow((short)0);
|
2002-04-27 20:45:10 -04:00
|
|
|
c = r.createCell((short)1);
|
2002-04-27 22:03:53 -04:00
|
|
|
c.setCellFormula(1 + operator + 1);
|
2002-04-27 20:45:10 -04:00
|
|
|
|
|
|
|
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
|
2002-04-27 13:26:38 -04:00
|
|
|
r = s.createRow((short) x);
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
for (short y = 1; y < 256 && y > 0; y++) {
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2002-04-27 13:26:38 -04:00
|
|
|
c = r.createCell((short) y);
|
2002-04-27 22:03:53 -04:00
|
|
|
c.setCellFormula("" + x + operator + y);
|
2002-04-27 20:45:10 -04:00
|
|
|
|
2002-04-27 13:26:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//make sure we do the maximum value of the Int operator
|
2002-04-27 20:45:10 -04:00
|
|
|
if (s.getLastRowNum() < Short.MAX_VALUE) {
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c = r.createCell((short)0);
|
2002-04-27 22:03:53 -04:00
|
|
|
c.setCellFormula("" + Short.MAX_VALUE + operator + Short.MAX_VALUE);
|
2002-04-27 20:45:10 -04:00
|
|
|
}
|
2002-04-27 13:26:38 -04:00
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
2002-04-27 22:03:53 -04:00
|
|
|
assertTrue("file exists",file.exists());
|
2002-04-27 13:26:38 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
binomialVerify(operator,file);
|
|
|
|
}
|
|
|
|
|
2002-04-28 11:42:42 -04:00
|
|
|
/**
|
|
|
|
* 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)
|
2002-04-27 22:03:53 -04:00
|
|
|
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;
|
2002-04-27 13:26:38 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
//get our minimum values
|
|
|
|
r = s.getRow((short)0);
|
|
|
|
c = r.getCell((short)1);
|
2002-04-28 12:40:18 -04:00
|
|
|
assertTrue("minval Formula is as expected 1"+operator+"1 != "+c.getCellFormula(),
|
2002-04-28 11:42:42 -04:00
|
|
|
( ("1"+operator+"1").equals(c.getCellFormula())
|
|
|
|
));
|
2002-04-27 13:26:38 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
for (short x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
|
|
|
|
r = s.getRow((short) x);
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
for (short y = 1; y < 256 && y > 0; y++) {
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
c = r.getCell((short) y);
|
|
|
|
|
2002-04-28 11:42:42 -04:00
|
|
|
assertTrue("loop Formula is as expected "+x+operator+y+"!="+c.getCellFormula(),(
|
|
|
|
(""+x+operator+y).equals(c.getCellFormula())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2002-04-28 11:42:42 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
//test our maximum values
|
|
|
|
r = s.getRow((short)0);
|
|
|
|
c = r.getCell((short)0);
|
2002-04-28 11:42:42 -04:00
|
|
|
|
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
assertTrue("maxval Formula is as expected",(
|
2002-04-28 11:42:42 -04:00
|
|
|
(""+Short.MAX_VALUE+operator+Short.MAX_VALUE).equals(c.getCellFormula())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
in.close();
|
2002-04-28 11:42:42 -04:00
|
|
|
assertTrue("file exists",file.exists());
|
2002-04-27 13:26:38 -04:00
|
|
|
}
|
2002-04-29 20:18:47 -04:00
|
|
|
|
2002-04-29 22:12:25 -04:00
|
|
|
|
|
|
|
|
2002-04-29 20:18:47 -04:00
|
|
|
/**
|
2002-04-29 22:12:25 -04:00
|
|
|
* Writes a function then tests to see if its correct
|
2002-04-29 20:18:47 -04:00
|
|
|
*
|
|
|
|
*/
|
2002-04-29 22:12:25 -04:00
|
|
|
public void areaFunctionTest(String function)
|
|
|
|
throws Exception {
|
|
|
|
|
2002-04-29 20:18:47 -04:00
|
|
|
short rownum = 0;
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testFormulaAreaFunction"+function,".xls");
|
2002-04-29 20:18:47 -04:00
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
|
|
|
|
|
2002-04-29 22:12:25 -04:00
|
|
|
r = s.createRow((short) 0);
|
2002-04-29 20:18:47 -04:00
|
|
|
|
2002-04-29 22:12:25 -04:00
|
|
|
c = r.createCell((short) 0);
|
|
|
|
c.setCellFormula(function+"(A2:A3)");
|
2002-04-29 20:18:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
assertTrue("file exists",file.exists());
|
2002-04-29 22:12:25 -04:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2002-04-27 13:26:38 -04:00
|
|
|
|
2002-04-30 21:41:51 -04:00
|
|
|
/**
|
|
|
|
* Writes a function then tests to see if its correct
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public void refArrayFunctionTest(String function)
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
short rownum = 0;
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testFormulaArrayFunction"+function,".xls");
|
2002-04-30 21:41:51 -04:00
|
|
|
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)",
|
2002-05-03 11:47:20 -04:00
|
|
|
( (function+"(A2,A3)").equals(c.getCellFormula()) )
|
2002-04-30 21:41:51 -04:00
|
|
|
);
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a function then tests to see if its correct
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public void refAreaArrayFunctionTest(String function)
|
|
|
|
throws Exception {
|
|
|
|
|
|
|
|
short rownum = 0;
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testFormulaAreaArrayFunction"+function,".xls");
|
2002-04-30 21:41:51 -04:00
|
|
|
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)");
|
2002-05-05 12:55:41 -04:00
|
|
|
c=r.createCell((short) 1);
|
|
|
|
c.setCellFormula(function+"($A$2:$A4,B$2:B4)");
|
2002-04-30 21:41:51 -04:00
|
|
|
|
|
|
|
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)",
|
2002-05-03 11:47:20 -04:00
|
|
|
( (function+"(A2:A4,B2:B4)").equals(c.getCellFormula()) )
|
2002-05-05 12:55:41 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
c=r.getCell((short) 1);
|
|
|
|
assertTrue("function ="+function+"($A$2:$A4,B$2:B4)",
|
|
|
|
( (function+"($A$2:$A4,B$2:B4)").equals(c.getCellFormula()) )
|
2002-04-30 21:41:51 -04:00
|
|
|
);
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-05-04 11:45:05 -04:00
|
|
|
public void testAbsRefs() throws Exception {
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testFormulaAbsRef",".xls");
|
2002-05-04 11:45:05 -04:00
|
|
|
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");
|
2002-05-06 14:23:24 -04:00
|
|
|
c=r.createCell( (short) 4);
|
2002-05-04 11:45:05 -04:00
|
|
|
c.setCellFormula("SUM($A$3,$A$2)");
|
2002-05-06 14:23:24 -04:00
|
|
|
|
2002-05-04 11:45:05 -04:00
|
|
|
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()));
|
2002-05-06 14:23:24 -04:00
|
|
|
c = r.getCell((short)4);
|
2002-05-04 11:45:05 -04:00
|
|
|
assertTrue("SUM($A$3,$A$2)", ("SUM($A$3,$A$2)").equals(c.getCellFormula()));
|
|
|
|
in.close();
|
|
|
|
}
|
2002-05-06 14:23:24 -04:00
|
|
|
|
2002-06-25 04:35:16 -04:00
|
|
|
public void testSheetFunctions()
|
2003-08-31 02:16:57 -04:00
|
|
|
throws IOException
|
2002-06-25 04:35:16 -04:00
|
|
|
{
|
|
|
|
String filename = System.getProperty("HSSF.testdata.path");
|
|
|
|
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testSheetFormula",".xls");
|
2002-06-25 04:35:16 -04:00
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet("A");
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c = r.createCell((short)0);c.setCellValue(1);
|
|
|
|
c = r.createCell((short)1);c.setCellValue(2);
|
|
|
|
|
|
|
|
s = wb.createSheet("B");
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c=r.createCell((short)0); c.setCellFormula("AVERAGE(A!A1:B1)");
|
|
|
|
c=r.createCell((short)1); c.setCellFormula("A!A1+A!B1");
|
2002-06-26 14:05:40 -04:00
|
|
|
c=r.createCell((short)2); c.setCellFormula("A!$A$1+A!$B1");
|
2002-06-25 04:35:16 -04:00
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
assertTrue("file exists",file.exists());
|
|
|
|
|
|
|
|
FileInputStream in = new FileInputStream(file);
|
|
|
|
wb = new HSSFWorkbook(in);
|
|
|
|
s = wb.getSheet("B");
|
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell((short)0);
|
2002-06-26 14:05:40 -04:00
|
|
|
assertTrue("expected: AVERAGE(A!A1:B1) got: "+c.getCellFormula(), ("AVERAGE(A!A1:B1)").equals(c.getCellFormula()));
|
2002-06-25 04:35:16 -04:00
|
|
|
c = r.getCell((short)1);
|
2002-06-26 14:05:40 -04:00
|
|
|
assertTrue("expected: A!A1+A!B1 got: "+c.getCellFormula(), ("A!A1+A!B1").equals(c.getCellFormula()));
|
2002-06-25 04:35:16 -04:00
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
|
2002-05-06 14:23:24 -04:00
|
|
|
public void testRVAoperands() throws Exception {
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testFormulaRVA",".xls");
|
2002-05-06 14:23:24 -04:00
|
|
|
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("AVERAGE(A3,A2)");
|
|
|
|
c=r.createCell( (short) 2);
|
|
|
|
c.setCellFormula("ROW(A3)");
|
|
|
|
c=r.createCell( (short) 3);
|
|
|
|
c.setCellFormula("AVERAGE(A2:A3)");
|
|
|
|
c=r.createCell( (short) 4);
|
|
|
|
c.setCellFormula("POWER(A2,A3)");
|
2002-05-27 17:00:12 -04:00
|
|
|
c=r.createCell( (short) 5);
|
|
|
|
c.setCellFormula("SIN(A2)");
|
2002-05-06 14:23:24 -04:00
|
|
|
|
2002-06-12 08:40:03 -04:00
|
|
|
c=r.createCell( (short) 6);
|
|
|
|
c.setCellFormula("SUM(A2:A3)");
|
|
|
|
|
|
|
|
c=r.createCell( (short) 7);
|
|
|
|
c.setCellFormula("SUM(A2,A3)");
|
|
|
|
|
2002-05-06 14:23:24 -04:00
|
|
|
r = s.createRow((short) 1);c=r.createCell( (short) 0); c.setCellValue(2.0);
|
|
|
|
r = s.createRow((short) 2);c=r.createCell( (short) 0); c.setCellValue(3.0);
|
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
assertTrue("file exists",file.exists());
|
|
|
|
}
|
2002-08-03 14:16:54 -04:00
|
|
|
|
|
|
|
public void testStringFormulas()
|
2003-08-31 02:16:57 -04:00
|
|
|
throws IOException
|
2002-08-03 14:16:54 -04:00
|
|
|
{
|
|
|
|
String readFilename = System.getProperty("HSSF.testdata.path");
|
|
|
|
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testStringFormula",".xls");
|
2002-08-03 14:16:54 -04:00
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet("A");
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c=r.createCell((short)1); c.setCellFormula("UPPER(\"abc\")");
|
|
|
|
c=r.createCell((short)2); c.setCellFormula("LOWER(\"ABC\")");
|
|
|
|
c=r.createCell((short)3); c.setCellFormula("CONCATENATE(\" my \",\" name \")");
|
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
assertTrue("file exists",file.exists());
|
|
|
|
|
|
|
|
FileInputStream in = new FileInputStream(readFilename+File.separator+"StringFormulas.xls");
|
|
|
|
wb = new HSSFWorkbook(in);
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell((short)0);
|
|
|
|
assertTrue("expected: UPPER(\"xyz\") got "+c.getCellFormula(), ("UPPER(\"xyz\")").equals(c.getCellFormula()));
|
|
|
|
//c = r.getCell((short)1);
|
|
|
|
//assertTrue("expected: A!A1+A!B1 got: "+c.getCellFormula(), ("A!A1+A!B1").equals(c.getCellFormula()));
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
|
2003-05-04 14:22:09 -04:00
|
|
|
|
|
|
|
|
|
|
|
public void testLogicalFormulas()
|
2003-08-31 02:16:57 -04:00
|
|
|
throws IOException
|
2003-05-04 14:22:09 -04:00
|
|
|
{
|
|
|
|
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testLogicalFormula",".xls");
|
2003-05-04 14:22:09 -04:00
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet("A");
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c=r.createCell((short)1); c.setCellFormula("IF(A1<A2,B1,B2)");
|
|
|
|
|
|
|
|
|
|
|
|
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)1);
|
|
|
|
assertEquals("Formula in cell 1 ","IF(A1<A2,B1,B2)",c.getCellFormula());
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
|
2002-10-23 22:14:29 -04:00
|
|
|
public void testDateFormulas()
|
2003-08-31 02:16:57 -04:00
|
|
|
throws IOException
|
2002-10-23 22:14:29 -04:00
|
|
|
{
|
|
|
|
String readFilename = System.getProperty("HSSF.testdata.path");
|
|
|
|
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testDateFormula",".xls");
|
2002-10-23 22:14:29 -04:00
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
2005-08-18 03:06:44 -04:00
|
|
|
HSSFSheet s = wb.createSheet("testSheet1");
|
2002-10-23 22:14:29 -04:00
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
|
|
|
|
r = s.createRow( (short)0 );
|
|
|
|
c = r.createCell( (short)0 );
|
|
|
|
|
|
|
|
HSSFCellStyle cellStyle = wb.createCellStyle();
|
|
|
|
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
|
|
|
|
c.setCellValue(new Date());
|
|
|
|
c.setCellStyle(cellStyle);
|
|
|
|
|
|
|
|
// assertEquals("Checking hour = " + hour, date.getTime().getTime(),
|
|
|
|
// HSSFDateUtil.getJavaDate(excelDate).getTime());
|
|
|
|
|
|
|
|
for (int k=1; k < 100; k++) {
|
|
|
|
r=s.createRow((short)k);
|
|
|
|
c=r.createCell((short)0);
|
|
|
|
c.setCellFormula("A"+(k)+"+1");
|
|
|
|
c.setCellStyle(cellStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
assertTrue("file exists",file.exists());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-03-14 20:57:41 -05:00
|
|
|
|
2002-09-02 12:13:48 -04:00
|
|
|
public void testIfFormulas()
|
2003-08-31 02:16:57 -04:00
|
|
|
throws IOException
|
2002-09-02 12:13:48 -04:00
|
|
|
{
|
|
|
|
String readFilename = System.getProperty("HSSF.testdata.path");
|
|
|
|
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testIfFormula",".xls");
|
2002-09-02 12:13:48 -04:00
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
2005-08-18 03:06:44 -04:00
|
|
|
HSSFSheet s = wb.createSheet("testSheet1");
|
2002-09-02 12:13:48 -04:00
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c=r.createCell((short)1); c.setCellValue(1);
|
|
|
|
c=r.createCell((short)2); c.setCellValue(2);
|
|
|
|
c=r.createCell((short)3); c.setCellFormula("MAX(A1:B1)");
|
|
|
|
c=r.createCell((short)4); c.setCellFormula("IF(A1=D1,\"A1\",\"B1\")");
|
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
|
2002-09-05 23:56:47 -04:00
|
|
|
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)4);
|
|
|
|
|
|
|
|
assertTrue("expected: IF(A1=D1,\"A1\",\"B1\") got "+c.getCellFormula(), ("IF(A1=D1,\"A1\",\"B1\")").equals(c.getCellFormula()));
|
|
|
|
in.close();
|
|
|
|
|
2002-09-02 12:13:48 -04:00
|
|
|
|
2002-09-05 23:56:47 -04:00
|
|
|
in = new FileInputStream(readFilename+File.separator+"IfFormulaTest.xls");
|
2002-09-02 12:13:48 -04:00
|
|
|
wb = new HSSFWorkbook(in);
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
r = s.getRow(3);
|
|
|
|
c = r.getCell((short)0);
|
2002-09-05 23:56:47 -04:00
|
|
|
assertTrue("expected: IF(A3=A1,\"A1\",\"A2\") got "+c.getCellFormula(), ("IF(A3=A1,\"A1\",\"A2\")").equals(c.getCellFormula()));
|
2002-09-02 12:13:48 -04:00
|
|
|
//c = r.getCell((short)1);
|
|
|
|
//assertTrue("expected: A!A1+A!B1 got: "+c.getCellFormula(), ("A!A1+A!B1").equals(c.getCellFormula()));
|
2003-03-14 20:57:41 -05:00
|
|
|
in.close();
|
|
|
|
|
2004-10-12 01:49:01 -04:00
|
|
|
File simpleIf = TempFile.createTempFile("testSimpleIfFormulaWrite",".xls");
|
2003-03-14 20:57:41 -05:00
|
|
|
out = new FileOutputStream(simpleIf);
|
|
|
|
wb = new HSSFWorkbook();
|
2005-08-18 03:06:44 -04:00
|
|
|
s = wb.createSheet("testSheet1");
|
2003-03-14 20:57:41 -05:00
|
|
|
r = null;
|
|
|
|
c = null;
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c=r.createCell((short)0); c.setCellFormula("IF(1=1,0,1)");
|
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
assertTrue("file exists", simpleIf.exists());
|
|
|
|
|
|
|
|
assertTrue("length of simpleIf file is zero", (simpleIf.length()>0));
|
|
|
|
|
2004-10-12 01:49:01 -04:00
|
|
|
File nestedIf = TempFile.createTempFile("testNestedIfFormula",".xls");
|
2003-03-14 20:57:41 -05:00
|
|
|
out = new FileOutputStream(nestedIf);
|
|
|
|
wb = new HSSFWorkbook();
|
2005-08-18 03:06:44 -04:00
|
|
|
s = wb.createSheet("testSheet1");
|
2003-03-14 20:57:41 -05:00
|
|
|
r = null;
|
|
|
|
c = null;
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c=r.createCell((short)0);
|
|
|
|
c.setCellValue(1);
|
|
|
|
|
|
|
|
c=r.createCell((short)1);
|
|
|
|
c.setCellValue(3);
|
|
|
|
|
|
|
|
|
|
|
|
HSSFCell formulaCell=r.createCell((short)3);
|
|
|
|
|
|
|
|
r = s.createRow((short)1);
|
|
|
|
c=r.createCell((short)0);
|
|
|
|
c.setCellValue(3);
|
|
|
|
|
|
|
|
c=r.createCell((short)1);
|
|
|
|
c.setCellValue(7);
|
|
|
|
|
|
|
|
formulaCell.setCellFormula("IF(A1=B1,AVERAGE(A1:B1),AVERAGE(A2:B2))");
|
|
|
|
|
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
assertTrue("file exists", nestedIf.exists());
|
|
|
|
|
|
|
|
assertTrue("length of nestedIf file is zero", (nestedIf.length()>0));
|
2002-09-02 12:13:48 -04:00
|
|
|
}
|
2003-03-14 20:57:41 -05:00
|
|
|
|
2003-03-16 23:47:58 -05:00
|
|
|
public void testSumIf()
|
2003-08-31 02:16:57 -04:00
|
|
|
throws IOException
|
2003-03-16 23:47:58 -05:00
|
|
|
{
|
|
|
|
String readFilename = System.getProperty("HSSF.testdata.path");
|
|
|
|
String function ="SUMIF(A1:A5,\">4000\",B1:B5)";
|
|
|
|
|
|
|
|
File inFile = new File(readFilename+"/sumifformula.xls");
|
|
|
|
FileInputStream in = new FileInputStream(inFile);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook(in);
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
HSSFSheet s = wb.getSheetAt(0);
|
|
|
|
HSSFRow r = s.getRow(0);
|
|
|
|
HSSFCell c = r.getCell((short)2);
|
|
|
|
assertEquals(function, c.getCellFormula());
|
|
|
|
|
|
|
|
|
2004-10-12 01:49:01 -04:00
|
|
|
File file = TempFile.createTempFile("testSumIfFormula",".xls");
|
2003-03-16 23:47:58 -05:00
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
wb = new HSSFWorkbook();
|
|
|
|
s = wb.createSheet();
|
|
|
|
|
|
|
|
r = s.createRow((short)0);
|
|
|
|
c=r.createCell((short)0); c.setCellValue((double)1000);
|
|
|
|
c=r.createCell((short)1); c.setCellValue((double)1);
|
|
|
|
|
|
|
|
|
|
|
|
r = s.createRow((short)1);
|
|
|
|
c=r.createCell((short)0); c.setCellValue((double)2000);
|
|
|
|
c=r.createCell((short)1); c.setCellValue((double)2);
|
|
|
|
|
|
|
|
r = s.createRow((short)2);
|
|
|
|
c=r.createCell((short)0); c.setCellValue((double)3000);
|
|
|
|
c=r.createCell((short)1); c.setCellValue((double)3);
|
|
|
|
|
|
|
|
r = s.createRow((short)3);
|
|
|
|
c=r.createCell((short)0); c.setCellValue((double)4000);
|
|
|
|
c=r.createCell((short)1); c.setCellValue((double)4);
|
|
|
|
|
|
|
|
r = s.createRow((short)4);
|
|
|
|
c=r.createCell((short)0); c.setCellValue((double)5000);
|
|
|
|
c=r.createCell((short)1); c.setCellValue((double)5);
|
|
|
|
|
|
|
|
r = s.getRow(0);
|
|
|
|
c=r.createCell((short)2); c.setCellFormula(function);
|
|
|
|
|
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
assertTrue("sumif file doesnt exists", (file.exists()));
|
|
|
|
assertTrue("sumif == 0 bytes", file.length() > 0);
|
|
|
|
}
|
2002-09-02 12:13:48 -04:00
|
|
|
|
2003-08-31 02:16:57 -04:00
|
|
|
public void testSquareMacro() throws IOException {
|
|
|
|
File dir = new File(System.getProperty("HSSF.testdata.path"));
|
|
|
|
File xls = new File(dir, "SquareMacro.xls");
|
|
|
|
FileInputStream in = new FileInputStream(xls);
|
|
|
|
HSSFWorkbook w;
|
|
|
|
try {
|
|
|
|
w = new HSSFWorkbook(in);
|
|
|
|
} finally {
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
HSSFSheet s0 = w.getSheetAt(0);
|
|
|
|
HSSFRow[] r = {s0.getRow(0), s0.getRow(1)};
|
|
|
|
|
|
|
|
HSSFCell a1 = r[0].getCell((short) 0);
|
|
|
|
assertEquals("square(1)", a1.getCellFormula());
|
|
|
|
assertEquals(1d, a1.getNumericCellValue(), 1e-9);
|
|
|
|
|
|
|
|
HSSFCell a2 = r[1].getCell((short) 0);
|
|
|
|
assertEquals("square(2)", a2.getCellFormula());
|
|
|
|
assertEquals(4d, a2.getNumericCellValue(), 1e-9);
|
|
|
|
|
|
|
|
HSSFCell b1 = r[0].getCell((short) 1);
|
|
|
|
assertEquals("IF(TRUE,square(1))", b1.getCellFormula());
|
|
|
|
assertEquals(1d, b1.getNumericCellValue(), 1e-9);
|
|
|
|
|
|
|
|
HSSFCell b2 = r[1].getCell((short) 1);
|
|
|
|
assertEquals("IF(TRUE,square(2))", b2.getCellFormula());
|
|
|
|
assertEquals(4d, b2.getNumericCellValue(), 1e-9);
|
|
|
|
|
|
|
|
HSSFCell c1 = r[0].getCell((short) 2);
|
|
|
|
assertEquals("square(square(1))", c1.getCellFormula());
|
|
|
|
assertEquals(1d, c1.getNumericCellValue(), 1e-9);
|
|
|
|
|
|
|
|
HSSFCell c2 = r[1].getCell((short) 2);
|
|
|
|
assertEquals("square(square(2))", c2.getCellFormula());
|
|
|
|
assertEquals(16d, c2.getNumericCellValue(), 1e-9);
|
|
|
|
|
|
|
|
HSSFCell d1 = r[0].getCell((short) 3);
|
|
|
|
assertEquals("square(one())", d1.getCellFormula());
|
|
|
|
assertEquals(1d, d1.getNumericCellValue(), 1e-9);
|
|
|
|
|
|
|
|
HSSFCell d2 = r[1].getCell((short) 3);
|
|
|
|
assertEquals("square(two())", d2.getCellFormula());
|
|
|
|
assertEquals(4d, d2.getNumericCellValue(), 1e-9);
|
|
|
|
}
|
|
|
|
|
2003-08-31 02:19:41 -04:00
|
|
|
public void testStringFormulaRead() throws IOException {
|
|
|
|
File dir = new File(System.getProperty("HSSF.testdata.path"));
|
|
|
|
File xls = new File(dir, "StringFormulas.xls");
|
|
|
|
FileInputStream in = new FileInputStream(xls);
|
|
|
|
HSSFWorkbook w;
|
|
|
|
try {
|
|
|
|
w = new HSSFWorkbook(in);
|
|
|
|
} finally {
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
HSSFCell c = w.getSheetAt(0).getRow(0).getCell((short)0);
|
|
|
|
assertEquals("String Cell value","XYZ",c.getStringCellValue());
|
|
|
|
}
|
|
|
|
|
2005-04-22 09:12:14 -04:00
|
|
|
/** test for bug 34021*/
|
|
|
|
public void testComplexSheetRefs () throws IOException {
|
|
|
|
HSSFWorkbook sb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s1 = sb.createSheet("Sheet a.1");
|
|
|
|
HSSFSheet s2 = sb.createSheet("Sheet.A");
|
|
|
|
s2.createRow(1).createCell((short) 2).setCellFormula("'Sheet a.1'!A1");
|
|
|
|
s1.createRow(1).createCell((short) 2).setCellFormula("'Sheet.A'!A1");
|
|
|
|
File file = TempFile.createTempFile("testComplexSheetRefs",".xls");
|
|
|
|
sb.write(new FileOutputStream(file));
|
|
|
|
}
|
2005-05-19 10:56:45 -04:00
|
|
|
|
|
|
|
/*Unknown Ptg 3C*/
|
|
|
|
public void test27272_1() throws Exception {
|
|
|
|
String readFilename = System.getProperty("HSSF.testdata.path");
|
|
|
|
File inFile = new File(readFilename+"/27272_1.xls");
|
|
|
|
FileInputStream in = new FileInputStream(inFile);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook(in);
|
|
|
|
wb.getSheetAt(0);
|
|
|
|
assertEquals("Reference for named range ", "#REF!",wb.getNameAt(0).getReference());
|
|
|
|
File outF = File.createTempFile("bug27272_1",".xls");
|
|
|
|
wb.write(new FileOutputStream(outF));
|
|
|
|
System.out.println("Open "+outF.getAbsolutePath()+" in Excel");
|
|
|
|
}
|
|
|
|
/*Unknown Ptg 3D*/
|
|
|
|
public void test27272_2() throws Exception {
|
|
|
|
String readFilename = System.getProperty("HSSF.testdata.path");
|
|
|
|
File inFile = new File(readFilename+"/27272_2.xls");
|
|
|
|
FileInputStream in = new FileInputStream(inFile);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook(in);
|
|
|
|
assertEquals("Reference for named range ", "#REF!",wb.getNameAt(0).getReference());
|
|
|
|
File outF = File.createTempFile("bug27272_2",".xls");
|
|
|
|
wb.write(new FileOutputStream(outF));
|
|
|
|
System.out.println("Open "+outF.getAbsolutePath()+" in Excel");
|
|
|
|
}
|
2006-08-24 12:41:12 -04:00
|
|
|
|
|
|
|
/* MissingArgPtg */
|
|
|
|
public void testMissingArgPtg() throws Exception {
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFCell cell = wb.createSheet("Sheet1").createRow(4).createCell((short) 0);
|
|
|
|
cell.setCellFormula("IF(A1=\"A\",1,)");
|
|
|
|
}
|
2005-05-19 10:56:45 -04:00
|
|
|
|
2008-01-22 09:48:51 -05:00
|
|
|
public void testSharedFormula() throws Exception {
|
|
|
|
String readFilename = System.getProperty("HSSF.testdata.path");
|
|
|
|
File inFile = new File(readFilename+"/SharedFormulaTest.xls");
|
|
|
|
FileInputStream fis = new FileInputStream(inFile);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook(fis);
|
|
|
|
|
|
|
|
assertEquals("A$1*2", wb.getSheetAt(0).getRow(1).getCell((short)1).toString());
|
|
|
|
assertEquals("$A11*2", wb.getSheetAt(0).getRow(11).getCell((short)1).toString());
|
|
|
|
assertEquals("DZ2*2", wb.getSheetAt(0).getRow(1).getCell((short)128).toString());
|
|
|
|
assertEquals("B32770*2", wb.getSheetAt(0).getRow(32768).getCell((short)1).toString());
|
|
|
|
}
|
2005-05-19 10:56:45 -04:00
|
|
|
|
2002-04-27 20:45:10 -04:00
|
|
|
public static void main(String [] args) {
|
2002-04-27 13:26:38 -04:00
|
|
|
System.out
|
2002-04-27 20:45:10 -04:00
|
|
|
.println("Testing org.apache.poi.hssf.usermodel.TestFormulas");
|
2002-04-27 13:26:38 -04:00
|
|
|
junit.textui.TestRunner.run(TestFormulas.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|