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.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;
|
|
|
|
|
2008-04-06 23:02:03 -04:00
|
|
|
import org.apache.poi.hssf.HSSFTestDataSamples;
|
2008-12-23 11:19:07 -05:00
|
|
|
import org.apache.poi.hssf.model.HSSFFormulaParser;
|
|
|
|
import org.apache.poi.hssf.record.formula.Ptg;
|
|
|
|
import org.apache.poi.hssf.record.formula.NamePtg;
|
2003-03-16 23:47:58 -05:00
|
|
|
import org.apache.poi.hssf.util.CellReference;
|
2004-10-12 01:49:01 -04:00
|
|
|
import org.apache.poi.util.TempFile;
|
2008-12-23 11:19:07 -05:00
|
|
|
import org.apache.poi.ss.formula.FormulaType;
|
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
|
|
|
*/
|
2008-04-06 23:02:03 -04:00
|
|
|
public final class TestFormulas extends TestCase {
|
2002-04-27 13:26:38 -04:00
|
|
|
|
2008-04-06 23:02:03 -04:00
|
|
|
private static HSSFWorkbook openSample(String sampleFileName) {
|
|
|
|
return HSSFTestDataSamples.openSampleWorkbook(sampleFileName);
|
2002-04-27 13:26:38 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-27 20:45:10 -04:00
|
|
|
/**
|
|
|
|
* Add 1+1 -- WHoohoo!
|
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testBasicAddIntegers() {
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-27 20:45:10 -04:00
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-27 20:45:10 -04:00
|
|
|
//get our minimum values
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.createRow(1);
|
|
|
|
c = r.createCell(1);
|
2002-04-27 20:45:10 -04:00
|
|
|
c.setCellFormula(1 + "+" + 1);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
|
2002-04-27 22:03:53 -04:00
|
|
|
s = wb.getSheetAt(0);
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.getRow(1);
|
|
|
|
c = r.getCell(1);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
assertTrue("Formula is as expected",("1+1".equals(c.getCellFormula())));
|
2002-04-27 20:45:10 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-27 13:26:38 -04:00
|
|
|
/**
|
|
|
|
* Add various integers
|
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testAddIntegers() {
|
2002-04-27 22:03:53 -04:00
|
|
|
binomialOperator("+");
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
/**
|
|
|
|
* Multiply various integers
|
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testMultplyIntegers() {
|
2002-04-27 22:03:53 -04:00
|
|
|
binomialOperator("*");
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
/**
|
|
|
|
* Subtract various integers
|
2002-04-28 11:42:42 -04:00
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testSubtractIntegers() {
|
2002-04-27 22:03:53 -04:00
|
|
|
binomialOperator("-");
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
/**
|
|
|
|
* Subtract various integers
|
2002-04-28 11:42:42 -04:00
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testDivideIntegers() {
|
2002-04-27 22:03:53 -04:00
|
|
|
binomialOperator("/");
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
/**
|
|
|
|
* Exponentialize various integers;
|
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testPowerIntegers() {
|
2002-04-27 22:03:53 -04:00
|
|
|
binomialOperator("^");
|
|
|
|
}
|
2002-04-28 12:10:00 -04:00
|
|
|
|
|
|
|
/**
|
2008-09-26 16:32:06 -04:00
|
|
|
* Concatenate two numbers 1&2 = 12
|
2002-04-28 12:10:00 -04:00
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testConcatIntegers() {
|
2002-04-28 12:10:00 -04:00
|
|
|
binomialOperator("&");
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 12:10:00 -04:00
|
|
|
/**
|
|
|
|
* tests 1*2+3*4
|
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testOrderOfOperationsMultiply() {
|
2002-04-28 12:10:00 -04:00
|
|
|
orderTest("1*2+3*4");
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 12:10:00 -04:00
|
|
|
/**
|
|
|
|
* tests 1*2+3^4
|
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testOrderOfOperationsPower() {
|
2002-04-28 12:10:00 -04:00
|
|
|
orderTest("1*2+3^4");
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 12:40:18 -04:00
|
|
|
/**
|
|
|
|
* Tests that parenthesis are obeyed
|
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testParenthesis() {
|
2002-04-28 12:40:18 -04:00
|
|
|
orderTest("(1*3)+2+(1+2)*(3^4)^5");
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testReferencesOpr() {
|
2002-04-28 16:51:55 -04:00
|
|
|
String[] operation = new String[] {
|
|
|
|
"+", "-", "*", "/", "^", "&"
|
|
|
|
};
|
|
|
|
for (int k = 0; k < operation.length; k++) {
|
|
|
|
operationRefTest(operation[k]);
|
|
|
|
}
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-29 22:12:25 -04:00
|
|
|
/**
|
|
|
|
* Tests creating a file with floating point in a formula.
|
|
|
|
*
|
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testFloat() {
|
2002-04-30 21:11:05 -04:00
|
|
|
floatTest("*");
|
|
|
|
floatTest("/");
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
private static void floatTest(String operator) {
|
2002-04-30 21:11:05 -04:00
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-30 21:11:05 -04:00
|
|
|
//get our minimum values
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.createRow(0);
|
|
|
|
c = r.createCell(1);
|
2002-04-30 21:11:05 -04:00
|
|
|
c.setCellFormula(""+Float.MIN_VALUE + operator + Float.MIN_VALUE);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
for (int x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2) ) {
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.createRow(x);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
for (int y = 1; y < 256 && y > 0; y= (short) (y +2)) {
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
c = r.createCell(y);
|
2002-04-30 21:11:05 -04:00
|
|
|
c.setCellFormula("" + x+"."+y + operator + y +"."+x);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
|
|
|
|
2002-04-29 22:12:25 -04:00
|
|
|
}
|
2002-04-30 21:11:05 -04:00
|
|
|
}
|
|
|
|
if (s.getLastRowNum() < Short.MAX_VALUE) {
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.createRow(0);
|
|
|
|
c = r.createCell(0);
|
2002-04-30 21:11:05 -04:00
|
|
|
c.setCellFormula("" + Float.MAX_VALUE + operator + Float.MAX_VALUE);
|
|
|
|
}
|
2008-09-26 16:32:06 -04:00
|
|
|
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
floatVerify(operator, wb);
|
2002-04-30 21:11:05 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
private static void floatVerify(String operator, HSSFWorkbook wb) {
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-30 21:11:05 -04:00
|
|
|
HSSFSheet s = wb.getSheetAt(0);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
// don't know how to check correct result .. for the moment, we just verify that the file can be read.
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
for (int x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
|
2008-12-23 11:19:07 -05:00
|
|
|
HSSFRow r = s.getRow(x);
|
2002-04-29 22:12:25 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
for (int y = 1; y < 256 && y > 0; y=(short)(y+2)) {
|
2002-04-29 22:12:25 -04:00
|
|
|
|
2008-12-23 11:19:07 -05:00
|
|
|
HSSFCell c = r.getCell(y);
|
2002-04-30 21:11:05 -04:00
|
|
|
assertTrue("got a formula",c.getCellFormula()!=null);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-30 21:11:05 -04:00
|
|
|
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
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testAreaSum() {
|
2002-04-29 22:12:25 -04:00
|
|
|
areaFunctionTest("SUM");
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testAreaAverage() {
|
2002-04-30 21:11:05 -04:00
|
|
|
areaFunctionTest("AVERAGE");
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testRefArraySum() {
|
2002-04-30 21:41:51 -04:00
|
|
|
refArrayFunctionTest("SUM");
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testAreaArraySum() {
|
2002-04-30 21:41:51 -04:00
|
|
|
refAreaArrayFunctionTest("SUM");
|
|
|
|
}
|
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
private static void operationRefTest(String operator) {
|
2002-04-28 16:51:55 -04:00
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 16:51:55 -04:00
|
|
|
//get our minimum values
|
2008-08-07 19:49:10 -04:00
|
|
|
r = s.createRow(0);
|
|
|
|
c = r.createCell(1);
|
2002-04-28 16:51:55 -04:00
|
|
|
c.setCellFormula("A2" + operator + "A3");
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-08-07 19:49:10 -04:00
|
|
|
for (int x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
|
|
|
|
r = s.createRow(x);
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2008-08-07 19:49:10 -04:00
|
|
|
for (int y = 1; y < 256 && y > 0; y++) {
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 16:51:55 -04:00
|
|
|
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);
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 16:51:55 -04:00
|
|
|
if (y+50 < 255) {
|
|
|
|
refy1=(short)(y+50);
|
|
|
|
refy2=(short)(y+49);
|
|
|
|
} else {
|
|
|
|
refy1=(short)(y-4);
|
|
|
|
refy2=(short)(y-3);
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-08-07 19:49:10 -04:00
|
|
|
c = r.getCell(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
|
|
|
|
2008-08-07 19:49:10 -04:00
|
|
|
c = r.createCell(y);
|
2002-04-28 16:51:55 -04:00
|
|
|
c.setCellFormula("" + ref + operator + ref2);
|
|
|
|
|
2008-04-06 23:02:03 -04:00
|
|
|
|
|
|
|
|
2002-04-28 16:51:55 -04:00
|
|
|
}
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 16:51:55 -04:00
|
|
|
//make sure we do the maximum value of the Int operator
|
|
|
|
if (s.getLastRowNum() < Short.MAX_VALUE) {
|
2008-08-07 19:49:10 -04:00
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.createCell(0);
|
2002-04-28 16:51:55 -04:00
|
|
|
c.setCellFormula("" + "B1" + operator + "IV255");
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
|
|
|
|
operationalRefVerify(operator, wb);
|
2002-04-28 16:51:55 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 16:51:55 -04:00
|
|
|
/**
|
|
|
|
* Opens the sheet we wrote out by binomialOperator and makes sure the formulas
|
|
|
|
* all match what we expect (x operator y)
|
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
private static void operationalRefVerify(String operator, HSSFWorkbook wb) {
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 16:51:55 -04:00
|
|
|
HSSFSheet s = wb.getSheetAt(0);
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 16:51:55 -04:00
|
|
|
//get our minimum values
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(1);
|
2002-04-28 16:51:55 -04:00
|
|
|
//get our minimum values
|
|
|
|
assertTrue("minval Formula is as expected A2"+operator+"A3 != "+c.getCellFormula(),
|
|
|
|
( ("A2"+operator+"A3").equals(c.getCellFormula())
|
|
|
|
));
|
|
|
|
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
for (int x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.getRow(x);
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
for (int y = 1; y < 256 && y > 0; y++) {
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
int refx1;
|
|
|
|
int refy1;
|
|
|
|
int refx2;
|
|
|
|
int refy2;
|
2002-04-28 16:51:55 -04:00
|
|
|
if (x +50 < Short.MAX_VALUE) {
|
2008-09-26 16:32:06 -04:00
|
|
|
refx1=x+50;
|
|
|
|
refx2=x+46;
|
2002-04-28 16:51:55 -04:00
|
|
|
} else {
|
2008-09-26 16:32:06 -04:00
|
|
|
refx1=x-4;
|
|
|
|
refx2=x-3;
|
2002-04-28 16:51:55 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 16:51:55 -04:00
|
|
|
if (y+50 < 255) {
|
2008-09-26 16:32:06 -04:00
|
|
|
refy1=y+50;
|
|
|
|
refy2=y+49;
|
2002-04-28 16:51:55 -04:00
|
|
|
} else {
|
2008-09-26 16:32:06 -04:00
|
|
|
refy1=y-4;
|
|
|
|
refy2=y-3;
|
2002-04-28 16:51:55 -04:00
|
|
|
}
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
c = r.getCell(y);
|
2008-02-15 07:04:42 -05:00
|
|
|
CellReference cr= new CellReference(refx1, refy1, false, false);
|
2008-09-26 16:32:06 -04:00
|
|
|
String ref=cr.formatAsString();
|
2008-02-15 07:04:42 -05:00
|
|
|
ref=cr.formatAsString();
|
|
|
|
cr=new CellReference(refx2,refy2, false, false);
|
2008-09-26 16:32:06 -04:00
|
|
|
String ref2=cr.formatAsString();
|
2008-04-06 23:02:03 -04:00
|
|
|
|
|
|
|
|
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())
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 16:51:55 -04:00
|
|
|
//test our maximum values
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(0);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
assertEquals("B1"+operator+"IV255", c.getCellFormula());
|
2002-04-28 16:51:55 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
|
|
|
|
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
|
2008-04-06 23:02:03 -04:00
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
private static void orderTest(String formula) {
|
2002-04-28 12:10:00 -04:00
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 12:10:00 -04:00
|
|
|
//get our minimum values
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.createRow(0);
|
|
|
|
c = r.createCell(1);
|
2002-04-28 12:10:00 -04:00
|
|
|
c.setCellFormula(formula);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
|
2002-04-28 12:10:00 -04:00
|
|
|
s = wb.getSheetAt(0);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-28 12:10:00 -04:00
|
|
|
//get our minimum values
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(1);
|
2002-04-28 12:10:00 -04:00
|
|
|
assertTrue("minval Formula is as expected",
|
|
|
|
formula.equals(c.getCellFormula())
|
|
|
|
);
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
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.
|
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
private static void binomialOperator(String operator) {
|
2002-04-27 13:26:38 -04:00
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-27 20:45:10 -04:00
|
|
|
//get our minimum values
|
2008-08-07 19:49:10 -04:00
|
|
|
r = s.createRow(0);
|
|
|
|
c = r.createCell(1);
|
2002-04-27 22:03:53 -04:00
|
|
|
c.setCellFormula(1 + operator + 1);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-08-07 19:49:10 -04:00
|
|
|
for (int x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
|
|
|
|
r = s.createRow(x);
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2008-08-07 19:49:10 -04:00
|
|
|
for (int y = 1; y < 256 && y > 0; y++) {
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2008-08-07 19:49:10 -04:00
|
|
|
c = r.createCell(y);
|
2002-04-27 22:03:53 -04:00
|
|
|
c.setCellFormula("" + x + operator + y);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-27 13:26:38 -04:00
|
|
|
}
|
|
|
|
}
|
2008-04-06 23:02:03 -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) {
|
2008-08-07 19:49:10 -04:00
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.createCell(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
|
|
|
}
|
2008-09-26 16:32:06 -04:00
|
|
|
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
|
|
|
|
binomialVerify(operator, wb);
|
2002-04-27 22:03:53 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
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)
|
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
private static void binomialVerify(String operator, HSSFWorkbook wb) {
|
2002-04-27 22:03:53 -04:00
|
|
|
HSSFSheet s = wb.getSheetAt(0);
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
//get our minimum values
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(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())
|
|
|
|
));
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
for (int x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) {
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.getRow(x);
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
for (int y = 1; y < 256 && y > 0; y++) {
|
2002-04-28 16:51:55 -04:00
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
c = r.getCell(y);
|
2002-04-27 22:03:53 -04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-27 22:03:53 -04:00
|
|
|
//test our maximum values
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(0);
|
2008-04-06 23:02:03 -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 13:26:38 -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
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public static void areaFunctionTest(String function) {
|
2002-04-29 20:18:47 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
2002-04-29 20:18:47 -04:00
|
|
|
|
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
r = s.createRow(0);
|
2002-04-29 20:18:47 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
c = r.createCell(0);
|
|
|
|
c.setCellFormula(function+"(A2:A3)");
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(0);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
assertTrue("function ="+function+"(A2:A3)",
|
|
|
|
( (function+"(A2:A3)").equals((function+"(A2:A3)")) )
|
|
|
|
);
|
2002-04-29 22:12:25 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2002-04-30 21:41:51 -04:00
|
|
|
/**
|
|
|
|
* Writes a function then tests to see if its correct
|
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public void refArrayFunctionTest(String function) {
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
2002-04-30 21:41:51 -04:00
|
|
|
|
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
r = s.createRow(0);
|
2002-04-30 21:41:51 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
c = r.createCell(0);
|
|
|
|
c.setCellFormula(function+"(A2,A3)");
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(0);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
assertTrue("function ="+function+"(A2,A3)",
|
|
|
|
( (function+"(A2,A3)").equals(c.getCellFormula()) )
|
|
|
|
);
|
2002-04-30 21:41:51 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
|
|
|
|
2002-04-30 21:41:51 -04:00
|
|
|
/**
|
|
|
|
* Writes a function then tests to see if its correct
|
|
|
|
*
|
|
|
|
*/
|
2008-09-26 16:32:06 -04:00
|
|
|
public void refAreaArrayFunctionTest(String function) {
|
2002-04-30 21:41:51 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
2002-04-30 21:41:51 -04:00
|
|
|
|
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
r = s.createRow(0);
|
2002-04-30 21:41:51 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
c = r.createCell(0);
|
|
|
|
c.setCellFormula(function+"(A2:A4,B2:B4)");
|
|
|
|
c=r.createCell(1);
|
|
|
|
c.setCellFormula(function+"($A$2:$A4,B$2:B4)");
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(0);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
assertTrue("function ="+function+"(A2:A4,B2:B4)",
|
|
|
|
( (function+"(A2:A4,B2:B4)").equals(c.getCellFormula()) )
|
|
|
|
);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
c=r.getCell(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
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testAbsRefs() {
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
2008-12-23 11:19:07 -05:00
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r;
|
|
|
|
HSSFCell c;
|
2002-05-04 11:45:05 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
r = s.createRow(0);
|
|
|
|
c = r.createCell(0);
|
|
|
|
c.setCellFormula("A3+A2");
|
|
|
|
c=r.createCell(1);
|
|
|
|
c.setCellFormula("$A3+$A2");
|
|
|
|
c=r.createCell(2);
|
|
|
|
c.setCellFormula("A$3+A$2");
|
|
|
|
c=r.createCell(3);
|
|
|
|
c.setCellFormula("$A$3+$A$2");
|
|
|
|
c=r.createCell(4);
|
|
|
|
c.setCellFormula("SUM($A$3,$A$2)");
|
|
|
|
|
|
|
|
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(0);
|
|
|
|
assertTrue("A3+A2", ("A3+A2").equals(c.getCellFormula()));
|
|
|
|
c = r.getCell(1);
|
|
|
|
assertTrue("$A3+$A2", ("$A3+$A2").equals(c.getCellFormula()));
|
|
|
|
c = r.getCell(2);
|
|
|
|
assertTrue("A$3+A$2", ("A$3+A$2").equals(c.getCellFormula()));
|
|
|
|
c = r.getCell(3);
|
|
|
|
assertTrue("$A$3+$A$2", ("$A$3+$A$2").equals(c.getCellFormula()));
|
|
|
|
c = r.getCell(4);
|
|
|
|
assertTrue("SUM($A$3,$A$2)", ("SUM($A$3,$A$2)").equals(c.getCellFormula()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testSheetFunctions() {
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet("A");
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
r = s.createRow(0);
|
|
|
|
c = r.createCell(0);c.setCellValue(1);
|
|
|
|
c = r.createCell(1);c.setCellValue(2);
|
2002-05-04 11:45:05 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
s = wb.createSheet("B");
|
|
|
|
r = s.createRow(0);
|
|
|
|
c=r.createCell(0); c.setCellFormula("AVERAGE(A!A1:B1)");
|
|
|
|
c=r.createCell(1); c.setCellFormula("A!A1+A!B1");
|
|
|
|
c=r.createCell(2); c.setCellFormula("A!$A$1+A!$B1");
|
2002-05-04 11:45:05 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
|
|
|
|
|
|
|
|
s = wb.getSheet("B");
|
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(0);
|
|
|
|
assertTrue("expected: AVERAGE(A!A1:B1) got: "+c.getCellFormula(), ("AVERAGE(A!A1:B1)").equals(c.getCellFormula()));
|
|
|
|
c = r.getCell(1);
|
|
|
|
assertTrue("expected: A!A1+A!B1 got: "+c.getCellFormula(), ("A!A1+A!B1").equals(c.getCellFormula()));
|
2002-05-04 11:45:05 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testRVAoperands() throws Exception {
|
|
|
|
File file = TempFile.createTempFile("testFormulaRVA",".xls");
|
|
|
|
FileOutputStream out = new FileOutputStream(file);
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet();
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
2008-04-06 23:02:03 -04:00
|
|
|
|
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
r = s.createRow(0);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
c = r.createCell(0);
|
|
|
|
c.setCellFormula("A3+A2");
|
|
|
|
c=r.createCell(1);
|
|
|
|
c.setCellFormula("AVERAGE(A3,A2)");
|
|
|
|
c=r.createCell(2);
|
|
|
|
c.setCellFormula("ROW(A3)");
|
|
|
|
c=r.createCell(3);
|
|
|
|
c.setCellFormula("AVERAGE(A2:A3)");
|
|
|
|
c=r.createCell(4);
|
|
|
|
c.setCellFormula("POWER(A2,A3)");
|
|
|
|
c=r.createCell(5);
|
|
|
|
c.setCellFormula("SIN(A2)");
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
c=r.createCell(6);
|
|
|
|
c.setCellFormula("SUM(A2:A3)");
|
2002-05-06 14:23:24 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
c=r.createCell(7);
|
|
|
|
c.setCellFormula("SUM(A2,A3)");
|
2002-05-06 14:23:24 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
r = s.createRow(1);c=r.createCell(0); c.setCellValue(2.0);
|
|
|
|
r = s.createRow(2);c=r.createCell(0); c.setCellValue(3.0);
|
2002-05-06 14:23:24 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
wb.write(out);
|
|
|
|
out.close();
|
|
|
|
assertTrue("file exists",file.exists());
|
2002-05-06 14:23:24 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testStringFormulas() {
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet("A");
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
r = s.createRow(0);
|
|
|
|
c=r.createCell(1); c.setCellFormula("UPPER(\"abc\")");
|
|
|
|
c=r.createCell(2); c.setCellFormula("LOWER(\"ABC\")");
|
|
|
|
c=r.createCell(3); c.setCellFormula("CONCATENATE(\" my \",\" name \")");
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
HSSFTestDataSamples.writeOutAndReadBack(wb);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
wb = openSample("StringFormulas.xls");
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(0);
|
|
|
|
assertEquals("UPPER(\"xyz\")", c.getCellFormula());
|
2002-08-03 14:16:54 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testLogicalFormulas() {
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet("A");
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
r = s.createRow(0);
|
|
|
|
c=r.createCell(1); c.setCellFormula("IF(A1<A2,B1,B2)");
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(1);
|
|
|
|
assertEquals("Formula in cell 1 ","IF(A1<A2,B1,B2)",c.getCellFormula());
|
|
|
|
}
|
2003-05-04 14:22:09 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testDateFormulas() {
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet("testSheet1");
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
r = s.createRow(0 );
|
|
|
|
c = r.createCell(0 );
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
HSSFCellStyle cellStyle = wb.createCellStyle();
|
|
|
|
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
|
|
|
|
c.setCellValue(new Date());
|
|
|
|
c.setCellStyle(cellStyle);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
// assertEquals("Checking hour = " + hour, date.getTime().getTime(),
|
|
|
|
// HSSFDateUtil.getJavaDate(excelDate).getTime());
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
for (int k=1; k < 100; k++) {
|
|
|
|
r=s.createRow(k);
|
|
|
|
c=r.createCell(0);
|
|
|
|
c.setCellFormula("A"+(k)+"+1");
|
|
|
|
c.setCellStyle(cellStyle);
|
|
|
|
}
|
2002-10-23 22:14:29 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
HSSFTestDataSamples.writeOutAndReadBack(wb);
|
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testIfFormulas() {
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s = wb.createSheet("testSheet1");
|
|
|
|
HSSFRow r = null;
|
|
|
|
HSSFCell c = null;
|
|
|
|
r = s.createRow(0);
|
|
|
|
c=r.createCell(1); c.setCellValue(1);
|
|
|
|
c=r.createCell(2); c.setCellValue(2);
|
|
|
|
c=r.createCell(3); c.setCellFormula("MAX(A1:B1)");
|
|
|
|
c=r.createCell(4); c.setCellFormula("IF(A1=D1,\"A1\",\"B1\")");
|
2002-10-23 22:14:29 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(4);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
assertTrue("expected: IF(A1=D1,\"A1\",\"B1\") got "+c.getCellFormula(), ("IF(A1=D1,\"A1\",\"B1\")").equals(c.getCellFormula()));
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
wb = openSample("IfFormulaTest.xls");
|
|
|
|
s = wb.getSheetAt(0);
|
|
|
|
r = s.getRow(3);
|
|
|
|
c = r.getCell(0);
|
|
|
|
assertTrue("expected: IF(A3=A1,\"A1\",\"A2\") got "+c.getCellFormula(), ("IF(A3=A1,\"A1\",\"A2\")").equals(c.getCellFormula()));
|
|
|
|
//c = r.getCell((short)1);
|
|
|
|
//assertTrue("expected: A!A1+A!B1 got: "+c.getCellFormula(), ("A!A1+A!B1").equals(c.getCellFormula()));
|
2008-04-06 23:02:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
wb = new HSSFWorkbook();
|
|
|
|
s = wb.createSheet("testSheet1");
|
|
|
|
r = null;
|
|
|
|
c = null;
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.createRow(0);
|
|
|
|
c=r.createCell(0); c.setCellFormula("IF(1=1,0,1)");
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
HSSFTestDataSamples.writeOutAndReadBack(wb);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
|
|
|
wb = new HSSFWorkbook();
|
|
|
|
s = wb.createSheet("testSheet1");
|
|
|
|
r = null;
|
|
|
|
c = null;
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.createRow(0);
|
|
|
|
c=r.createCell(0);
|
2008-04-06 23:02:03 -04:00
|
|
|
c.setCellValue(1);
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
c=r.createCell(1);
|
2008-04-06 23:02:03 -04:00
|
|
|
c.setCellValue(3);
|
|
|
|
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
HSSFCell formulaCell=r.createCell(3);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.createRow(1);
|
|
|
|
c=r.createCell(0);
|
2008-04-06 23:02:03 -04:00
|
|
|
c.setCellValue(3);
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
c=r.createCell(1);
|
2008-04-06 23:02:03 -04:00
|
|
|
c.setCellValue(7);
|
|
|
|
|
|
|
|
formulaCell.setCellFormula("IF(A1=B1,AVERAGE(A1:B1),AVERAGE(A2:B2))");
|
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
HSSFTestDataSamples.writeOutAndReadBack(wb);
|
2002-09-02 12:13:48 -04:00
|
|
|
}
|
2003-03-14 20:57:41 -05:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
public void testSumIf() {
|
2008-04-06 23:02:03 -04:00
|
|
|
String function ="SUMIF(A1:A5,\">4000\",B1:B5)";
|
|
|
|
|
|
|
|
HSSFWorkbook wb = openSample("sumifformula.xls");
|
|
|
|
|
|
|
|
HSSFSheet s = wb.getSheetAt(0);
|
|
|
|
HSSFRow r = s.getRow(0);
|
2008-08-25 18:41:08 -04:00
|
|
|
HSSFCell c = r.getCell(2);
|
2008-04-06 23:02:03 -04:00
|
|
|
assertEquals(function, c.getCellFormula());
|
|
|
|
|
|
|
|
|
|
|
|
wb = new HSSFWorkbook();
|
|
|
|
s = wb.createSheet();
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.createRow(0);
|
2008-09-26 16:32:06 -04:00
|
|
|
c=r.createCell(0); c.setCellValue(1000);
|
|
|
|
c=r.createCell(1); c.setCellValue(1);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.createRow(1);
|
2008-09-26 16:32:06 -04:00
|
|
|
c=r.createCell(0); c.setCellValue(2000);
|
|
|
|
c=r.createCell(1); c.setCellValue(2);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.createRow(2);
|
2008-09-26 16:32:06 -04:00
|
|
|
c=r.createCell(0); c.setCellValue(3000);
|
|
|
|
c=r.createCell(1); c.setCellValue(3);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.createRow(3);
|
2008-09-26 16:32:06 -04:00
|
|
|
c=r.createCell(0); c.setCellValue(4000);
|
|
|
|
c=r.createCell(1); c.setCellValue(4);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
r = s.createRow(4);
|
2008-09-26 16:32:06 -04:00
|
|
|
c=r.createCell(0); c.setCellValue(5000);
|
|
|
|
c=r.createCell(1); c.setCellValue(5);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
|
|
|
r = s.getRow(0);
|
2008-08-25 18:41:08 -04:00
|
|
|
c=r.createCell(2); c.setCellFormula(function);
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-26 16:32:06 -04:00
|
|
|
HSSFTestDataSamples.writeOutAndReadBack(wb);
|
2008-04-06 23:02:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testSquareMacro() {
|
|
|
|
HSSFWorkbook w = openSample("SquareMacro.xls");
|
|
|
|
|
2003-08-31 02:16:57 -04:00
|
|
|
HSSFSheet s0 = w.getSheetAt(0);
|
|
|
|
HSSFRow[] r = {s0.getRow(0), s0.getRow(1)};
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
HSSFCell a1 = r[0].getCell(0);
|
2003-08-31 02:16:57 -04:00
|
|
|
assertEquals("square(1)", a1.getCellFormula());
|
|
|
|
assertEquals(1d, a1.getNumericCellValue(), 1e-9);
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
HSSFCell a2 = r[1].getCell(0);
|
2003-08-31 02:16:57 -04:00
|
|
|
assertEquals("square(2)", a2.getCellFormula());
|
|
|
|
assertEquals(4d, a2.getNumericCellValue(), 1e-9);
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
HSSFCell b1 = r[0].getCell(1);
|
2003-08-31 02:16:57 -04:00
|
|
|
assertEquals("IF(TRUE,square(1))", b1.getCellFormula());
|
|
|
|
assertEquals(1d, b1.getNumericCellValue(), 1e-9);
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
HSSFCell b2 = r[1].getCell(1);
|
2003-08-31 02:16:57 -04:00
|
|
|
assertEquals("IF(TRUE,square(2))", b2.getCellFormula());
|
|
|
|
assertEquals(4d, b2.getNumericCellValue(), 1e-9);
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
HSSFCell c1 = r[0].getCell(2);
|
2003-08-31 02:16:57 -04:00
|
|
|
assertEquals("square(square(1))", c1.getCellFormula());
|
|
|
|
assertEquals(1d, c1.getNumericCellValue(), 1e-9);
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
HSSFCell c2 = r[1].getCell(2);
|
2003-08-31 02:16:57 -04:00
|
|
|
assertEquals("square(square(2))", c2.getCellFormula());
|
|
|
|
assertEquals(16d, c2.getNumericCellValue(), 1e-9);
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
HSSFCell d1 = r[0].getCell(3);
|
2003-08-31 02:16:57 -04:00
|
|
|
assertEquals("square(one())", d1.getCellFormula());
|
|
|
|
assertEquals(1d, d1.getNumericCellValue(), 1e-9);
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
HSSFCell d2 = r[1].getCell(3);
|
2003-08-31 02:16:57 -04:00
|
|
|
assertEquals("square(two())", d2.getCellFormula());
|
|
|
|
assertEquals(4d, d2.getNumericCellValue(), 1e-9);
|
|
|
|
}
|
|
|
|
|
2008-04-06 23:02:03 -04:00
|
|
|
public void testStringFormulaRead() {
|
|
|
|
HSSFWorkbook w = openSample("StringFormulas.xls");
|
2008-08-25 18:41:08 -04:00
|
|
|
HSSFCell c = w.getSheetAt(0).getRow(0).getCell(0);
|
2008-04-06 23:02:03 -04:00
|
|
|
assertEquals("String Cell value","XYZ",c.getRichStringCellValue().getString());
|
2003-08-31 02:19:41 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2005-04-22 09:12:14 -04:00
|
|
|
/** test for bug 34021*/
|
|
|
|
public void testComplexSheetRefs () throws IOException {
|
2008-04-06 23:02:03 -04:00
|
|
|
HSSFWorkbook sb = new HSSFWorkbook();
|
|
|
|
HSSFSheet s1 = sb.createSheet("Sheet a.1");
|
|
|
|
HSSFSheet s2 = sb.createSheet("Sheet.A");
|
2008-08-25 18:41:08 -04:00
|
|
|
s2.createRow(1).createCell(2).setCellFormula("'Sheet a.1'!A1");
|
|
|
|
s1.createRow(1).createCell(2).setCellFormula("'Sheet.A'!A1");
|
2008-04-06 23:02:03 -04:00
|
|
|
File file = TempFile.createTempFile("testComplexSheetRefs",".xls");
|
|
|
|
sb.write(new FileOutputStream(file));
|
|
|
|
}
|
|
|
|
|
2008-09-16 16:17:30 -04:00
|
|
|
/** Unknown Ptg 3C*/
|
2008-04-06 23:02:03 -04:00
|
|
|
public void test27272_1() throws Exception {
|
|
|
|
HSSFWorkbook wb = openSample("27272_1.xls");
|
|
|
|
wb.getSheetAt(0);
|
2009-04-11 10:15:27 -04:00
|
|
|
assertEquals("Reference for named range ", "Compliance!#REF!",wb.getNameAt(0).getRefersToFormula());
|
2008-04-06 23:02:03 -04:00
|
|
|
File outF = File.createTempFile("bug27272_1",".xls");
|
|
|
|
wb.write(new FileOutputStream(outF));
|
|
|
|
System.out.println("Open "+outF.getAbsolutePath()+" in Excel");
|
|
|
|
}
|
2008-09-16 16:17:30 -04:00
|
|
|
/** Unknown Ptg 3D*/
|
2008-04-06 23:02:03 -04:00
|
|
|
public void test27272_2() throws Exception {
|
|
|
|
HSSFWorkbook wb = openSample("27272_2.xls");
|
2009-04-11 10:15:27 -04:00
|
|
|
assertEquals("Reference for named range ", "LOAD.POD_HISTORIES!#REF!",wb.getNameAt(0).getRefersToFormula());
|
2008-04-06 23:02:03 -04:00
|
|
|
File outF = File.createTempFile("bug27272_2",".xls");
|
|
|
|
wb.write(new FileOutputStream(outF));
|
|
|
|
System.out.println("Open "+outF.getAbsolutePath()+" in Excel");
|
2005-04-22 09:12:14 -04:00
|
|
|
}
|
2008-04-06 23:02:03 -04:00
|
|
|
|
2008-09-16 16:17:30 -04:00
|
|
|
/** MissingArgPtg */
|
2008-04-06 23:02:03 -04:00
|
|
|
public void testMissingArgPtg() throws Exception {
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
2008-08-25 18:41:08 -04:00
|
|
|
HSSFCell cell = wb.createSheet("Sheet1").createRow(4).createCell(0);
|
2008-04-06 23:02:03 -04:00
|
|
|
cell.setCellFormula("IF(A1=\"A\",1,)");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testSharedFormula() {
|
|
|
|
HSSFWorkbook wb = openSample("SharedFormulaTest.xls");
|
|
|
|
|
2008-08-25 18:41:08 -04:00
|
|
|
assertEquals("A$1*2", wb.getSheetAt(0).getRow(1).getCell(1).toString());
|
|
|
|
assertEquals("$A11*2", wb.getSheetAt(0).getRow(11).getCell(1).toString());
|
|
|
|
assertEquals("DZ2*2", wb.getSheetAt(0).getRow(1).getCell(128).toString());
|
|
|
|
assertEquals("B32770*2", wb.getSheetAt(0).getRow(32768).getCell(1).toString());
|
2008-04-06 23:02:03 -04:00
|
|
|
}
|
2008-12-23 11:19:07 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test creation / evaluation of formulas with sheet-level names
|
|
|
|
*/
|
|
|
|
public void testSheetLevelFormulas(){
|
|
|
|
HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
|
|
|
|
|
HSSFRow row;
|
|
|
|
HSSFSheet sh1 = wb.createSheet("Sheet1");
|
|
|
|
HSSFName nm1 = wb.createName();
|
|
|
|
nm1.setNameName("sales_1");
|
|
|
|
nm1.setSheetIndex(0);
|
|
|
|
nm1.setRefersToFormula("Sheet1!$A$1");
|
|
|
|
row = sh1.createRow(0);
|
|
|
|
row.createCell(0).setCellValue(3);
|
|
|
|
row.createCell(1).setCellFormula("sales_1");
|
|
|
|
row.createCell(2).setCellFormula("sales_1*2");
|
|
|
|
|
|
|
|
|
|
|
|
HSSFSheet sh2 = wb.createSheet("Sheet2");
|
|
|
|
HSSFName nm2 = wb.createName();
|
|
|
|
nm2.setNameName("sales_1");
|
|
|
|
nm2.setSheetIndex(1);
|
|
|
|
nm2.setRefersToFormula("Sheet2!$A$1");
|
|
|
|
|
|
|
|
row = sh2.createRow(0);
|
|
|
|
row.createCell(0).setCellValue(5);
|
|
|
|
row.createCell(1).setCellFormula("sales_1");
|
|
|
|
row.createCell(2).setCellFormula("sales_1*3");
|
|
|
|
|
|
|
|
//check that NamePtg refers to the correct NameRecord
|
|
|
|
Ptg[] ptgs1 = HSSFFormulaParser.parse("sales_1", wb, FormulaType.CELL, 0);
|
|
|
|
NamePtg nPtg1 = (NamePtg)ptgs1[0];
|
|
|
|
assertSame(nm1, wb.getNameAt(nPtg1.getIndex()));
|
|
|
|
|
|
|
|
Ptg[] ptgs2 = HSSFFormulaParser.parse("sales_1", wb, FormulaType.CELL, 1);
|
|
|
|
NamePtg nPtg2 = (NamePtg)ptgs2[0];
|
|
|
|
assertSame(nm2, wb.getNameAt(nPtg2.getIndex()));
|
|
|
|
|
|
|
|
//check that the formula evaluator returns the correct result
|
|
|
|
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
|
|
|
|
assertEquals(3.0, evaluator.evaluate(sh1.getRow(0).getCell(1)).getNumberValue());
|
|
|
|
assertEquals(6.0, evaluator.evaluate(sh1.getRow(0).getCell(2)).getNumberValue());
|
|
|
|
|
|
|
|
assertEquals(5.0, evaluator.evaluate(sh2.getRow(0).getCell(1)).getNumberValue());
|
|
|
|
assertEquals(15.0, evaluator.evaluate(sh2.getRow(0).getCell(2)).getNumberValue());
|
|
|
|
}
|
2002-04-27 13:26:38 -04:00
|
|
|
}
|