Test relating to bug #44636

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@639231 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-03-20 10:06:59 +00:00
parent b43afc9428
commit 50af1f9ad4
3 changed files with 118 additions and 0 deletions

View File

@ -19,6 +19,7 @@ package org.apache.poi.hssf.usermodel;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Stack; import java.util.Stack;
@ -252,6 +253,35 @@ public class HSSFFormulaEvaluator {
return cell; return cell;
} }
/**
* Loops over all cells in all sheets of the supplied
* workbook.
* For cells that contain formulas, their formulas are
* evaluated, and the results are saved. These cells
* remain as formula cells.
* For cells that do not contain formulas, no changes
* are made.
* This is a helpful wrapper around looping over all
* cells, and calling evaluateFormulaCell on each one.
*/
public static void evaluateAllFormulaCells(HSSFWorkbook wb) {
for(int i=0; i<wb.getNumberOfSheets(); i++) {
HSSFSheet sheet = wb.getSheetAt(i);
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(sheet, wb);
for (Iterator rit = sheet.rowIterator(); rit.hasNext();) {
HSSFRow r = (HSSFRow)rit.next();
evaluator.setCurrentRow(r);
for (Iterator cit = r.cellIterator(); cit.hasNext();) {
HSSFCell c = (HSSFCell)cit.next();
if (c.getCellType() == HSSFCell.CELL_TYPE_FORMULA)
evaluator.evaluateFormulaCell(c);
}
}
}
}
/** /**
* Returns a CellValue wrapper around the supplied ValueEval instance. * Returns a CellValue wrapper around the supplied ValueEval instance.

View File

@ -0,0 +1,88 @@
/* ====================================================================
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.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import junit.framework.TestCase;
public final class TestFormulaEvaluatorBugs extends TestCase {
private String dirName;
private String tmpDirName;
protected void setUp() throws Exception {
super.setUp();
dirName = System.getProperty("HSSF.testdata.path");
tmpDirName = System.getProperty("java.io.tmpdir");
}
/**
* An odd problem with evaluateFormulaCell giving the
* right values when file is opened, but changes
* to the source data in some versions of excel
* doesn't cause them to be updated. However, other
* versions of excel, and gnumeric, work just fine
* WARNING - tedious bug where you actually have to
* open up excel
*/
public void test44636() throws Exception {
// Open the existing file, tweak one value and
// re-calculate
FileInputStream in = new FileInputStream(new File(dirName,"44636.xls"));
HSSFWorkbook wb = new HSSFWorkbook(in);
HSSFSheet sheet = wb.getSheetAt (0);
HSSFRow row = sheet.getRow (0);
row.getCell((short)0).setCellValue(4.2);
row.getCell((short)2).setCellValue(25);
HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
assertEquals(4.2*25, row.getCell((short)3).getNumericCellValue());
// Save
File existing = new File(tmpDirName,"44636-existing.xls");
FileOutputStream out = new FileOutputStream(existing);
wb.write(out);
out.close();
System.err.println("Existing file for bug #44636 written to " + existing.toString());
// Now, do a new file from scratch
wb = new HSSFWorkbook();
sheet = wb.createSheet();
row = sheet.createRow(0);
row.createCell((short)0).setCellValue(1.2);
row.createCell((short)1).setCellValue(4.2);
row = sheet.createRow(1);
row.createCell((short)0).setCellFormula("SUM(A1:B1)");
HSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
assertEquals(5.4, row.getCell((short)0).getNumericCellValue());
// Save
File scratch = new File(tmpDirName,"44636-scratch.xls");
out = new FileOutputStream(scratch);
wb.write(out);
out.close();
System.err.println("New file for bug #44636 written to " + scratch.toString());
}
}

Binary file not shown.