testformulas.java
来自「EXCEL read and write」· Java 代码 · 共 888 行 · 第 1/2 页
JAVA
888 行
/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.==================================================================== */package org.apache.poi.hssf.usermodel;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.Date;import junit.framework.TestCase;import org.apache.poi.hssf.HSSFTestDataSamples;import org.apache.poi.hssf.util.CellReference;import org.apache.poi.util.TempFile;/** * @author Andrew C. Oliver (acoliver at apache dot org) * @author Avik Sengupta */public final class TestFormulas extends TestCase { private static HSSFWorkbook openSample(String sampleFileName) { return HSSFTestDataSamples.openSampleWorkbook(sampleFileName); } /** * Add 1+1 -- WHoohoo! */ public void testBasicAddIntegers() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = null; HSSFCell c = null; //get our minimum values r = s.createRow(1); c = r.createCell(1); c.setCellFormula(1 + "+" + 1); wb = HSSFTestDataSamples.writeOutAndReadBack(wb); s = wb.getSheetAt(0); r = s.getRow(1); c = r.getCell(1); assertTrue("Formula is as expected",("1+1".equals(c.getCellFormula()))); } /** * Add various integers */ public void testAddIntegers() { binomialOperator("+"); } /** * Multiply various integers */ public void testMultplyIntegers() { binomialOperator("*"); } /** * Subtract various integers */ public void testSubtractIntegers() { binomialOperator("-"); } /** * Subtract various integers */ public void testDivideIntegers() { binomialOperator("/"); } /** * Exponentialize various integers; */ public void testPowerIntegers() { binomialOperator("^"); } /** * Concatenate two numbers 1&2 = 12 */ public void testConcatIntegers() { binomialOperator("&"); } /** * tests 1*2+3*4 */ public void testOrderOfOperationsMultiply() { orderTest("1*2+3*4"); } /** * tests 1*2+3^4 */ public void testOrderOfOperationsPower() { orderTest("1*2+3^4"); } /** * Tests that parenthesis are obeyed */ public void testParenthesis() { orderTest("(1*3)+2+(1+2)*(3^4)^5"); } public void testReferencesOpr() { String[] operation = new String[] { "+", "-", "*", "/", "^", "&" }; for (int k = 0; k < operation.length; k++) { operationRefTest(operation[k]); } } /** * Tests creating a file with floating point in a formula. * */ public void testFloat() { floatTest("*"); floatTest("/"); } private static void floatTest(String operator) { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = null; HSSFCell c = null; //get our minimum values r = s.createRow(0); c = r.createCell(1); c.setCellFormula(""+Float.MIN_VALUE + operator + Float.MIN_VALUE); for (int x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2) ) { r = s.createRow(x); for (int y = 1; y < 256 && y > 0; y= (short) (y +2)) { c = r.createCell(y); c.setCellFormula("" + x+"."+y + operator + y +"."+x); } } if (s.getLastRowNum() < Short.MAX_VALUE) { r = s.createRow(0); c = r.createCell(0); c.setCellFormula("" + Float.MAX_VALUE + operator + Float.MAX_VALUE); } wb = HSSFTestDataSamples.writeOutAndReadBack(wb); floatVerify(operator, wb); } private static void floatVerify(String operator, HSSFWorkbook wb) { HSSFSheet s = wb.getSheetAt(0); // don't know how to check correct result .. for the moment, we just verify that the file can be read. for (int x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) { HSSFRow r = s.getRow(x); for (int y = 1; y < 256 && y > 0; y=(short)(y+2)) { HSSFCell c = r.getCell(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()) )); } } } public void testAreaSum() { areaFunctionTest("SUM"); } public void testAreaAverage() { areaFunctionTest("AVERAGE"); } public void testRefArraySum() { refArrayFunctionTest("SUM"); } public void testAreaArraySum() { refAreaArrayFunctionTest("SUM"); } private static void operationRefTest(String operator) { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = null; HSSFCell c = null; //get our minimum values r = s.createRow(0); c = r.createCell(1); c.setCellFormula("A2" + operator + "A3"); for (int x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) { r = s.createRow(x); for (int 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(y); CellReference cr= new CellReference(refx1,refy1, false, false); ref=cr.formatAsString(); cr=new CellReference(refx2,refy2, false, false); ref2=cr.formatAsString(); c = r.createCell(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.getRow(0); c = r.createCell(0); c.setCellFormula("" + "B1" + operator + "IV255"); } wb = HSSFTestDataSamples.writeOutAndReadBack(wb); operationalRefVerify(operator, wb); } /** * Opens the sheet we wrote out by binomialOperator and makes sure the formulas * all match what we expect (x operator y) */ private static void operationalRefVerify(String operator, HSSFWorkbook wb) { HSSFSheet s = wb.getSheetAt(0); HSSFRow r = null; HSSFCell c = null; //get our minimum values r = s.getRow(0); c = r.getCell(1); //get our minimum values assertTrue("minval Formula is as expected A2"+operator+"A3 != "+c.getCellFormula(), ( ("A2"+operator+"A3").equals(c.getCellFormula()) )); for (int x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) { r = s.getRow(x); for (int y = 1; y < 256 && y > 0; y++) { int refx1; int refy1; int refx2; int refy2; if (x +50 < Short.MAX_VALUE) { refx1=x+50; refx2=x+46; } else { refx1=x-4; refx2=x-3; } if (y+50 < 255) { refy1=y+50; refy2=y+49; } else { refy1=y-4; refy2=y-3; } c = r.getCell(y); CellReference cr= new CellReference(refx1, refy1, false, false); String ref=cr.formatAsString(); ref=cr.formatAsString(); cr=new CellReference(refx2,refy2, false, false); String ref2=cr.formatAsString(); assertTrue("loop Formula is as expected "+ref+operator+ref2+"!="+c.getCellFormula(),( (""+ref+operator+ref2).equals(c.getCellFormula()) ) ); } } //test our maximum values r = s.getRow(0); c = r.getCell(0); assertEquals("B1"+operator+"IV255", c.getCellFormula()); } /** * tests order wrting out == order writing in for a given formula */ private static void orderTest(String formula) { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = null; HSSFCell c = null; //get our minimum values r = s.createRow(0); c = r.createCell(1); c.setCellFormula(formula); wb = HSSFTestDataSamples.writeOutAndReadBack(wb); s = wb.getSheetAt(0); //get our minimum values r = s.getRow(0); c = r.getCell(1); assertTrue("minval Formula is as expected", formula.equals(c.getCellFormula()) ); } /** * 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 static void binomialOperator(String operator) { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = null; HSSFCell c = null; //get our minimum values r = s.createRow(0); c = r.createCell(1); c.setCellFormula(1 + operator + 1); for (int x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) { r = s.createRow(x); for (int y = 1; y < 256 && y > 0; y++) { c = r.createCell(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.getRow(0); c = r.createCell(0); c.setCellFormula("" + Short.MAX_VALUE + operator + Short.MAX_VALUE); } wb = HSSFTestDataSamples.writeOutAndReadBack(wb); binomialVerify(operator, wb); } /** * Opens the sheet we wrote out by binomialOperator and makes sure the formulas * all match what we expect (x operator y) */ private static void binomialVerify(String operator, HSSFWorkbook wb) { HSSFSheet s = wb.getSheetAt(0); HSSFRow r = null; HSSFCell c = null; //get our minimum values r = s.getRow(0); c = r.getCell(1); assertTrue("minval Formula is as expected 1"+operator+"1 != "+c.getCellFormula(), ( ("1"+operator+"1").equals(c.getCellFormula()) )); for (int x = 1; x < Short.MAX_VALUE && x > 0; x=(short)(x*2)) { r = s.getRow(x); for (int y = 1; y < 256 && y > 0; y++) { c = r.getCell(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(0); c = r.getCell(0); assertTrue("maxval Formula is as expected",( (""+Short.MAX_VALUE+operator+Short.MAX_VALUE).equals(c.getCellFormula()) ) );
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?