/* ====================================================================
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.
==================================================================== */
namespace TestCases.HSSF.UserModel{
using System;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestCases.HSSF;
/**
*
* @author Josh Micich
*/
[TestClass]
public class TestHSSFFormulaEvaluator
{
/**
* Test that the HSSFFormulaEvaluator can Evaluate simple named ranges
* (single cells and rectangular areas)
*/
[TestMethod]
public void TestEvaluateSimple()
{
HSSFWorkbook wb = HSSFTestDataSamples.OpenSampleWorkbook("TestNames.xls");
NPOI.SS.UserModel.Sheet sheet = wb.GetSheetAt(0);
Cell cell = sheet.GetRow(8).GetCell(0);
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
NPOI.SS.UserModel.CellValue cv = fe.Evaluate(cell);
Assert.AreEqual(NPOI.SS.UserModel.CellType.NUMERIC, cv.CellType);
Assert.AreEqual(3.72, cv.NumberValue, 0.0);
}
[TestMethod]
public void TestFullColumnRefs()
{
HSSFWorkbook wb = new HSSFWorkbook();
NPOI.SS.UserModel.Sheet sheet = wb.CreateSheet("Sheet1");
Row row = sheet.CreateRow(0);
Cell cell0 = row.CreateCell(0);
cell0.CellFormula = ("sum(D:D)");
Cell cell1 = row.CreateCell(1);
cell1.CellFormula = ("sum(D:E)");
// some values in column D
setValue(sheet, 1, 3, 5.0);
setValue(sheet, 2, 3, 6.0);
setValue(sheet, 5, 3, 7.0);
setValue(sheet, 50, 3, 8.0);
// some values in column E
setValue(sheet, 1, 4, 9.0);
setValue(sheet, 2, 4, 10.0);
setValue(sheet, 30000, 4, 11.0);
// some other values
setValue(sheet, 1, 2, 100.0);
setValue(sheet, 2, 5, 100.0);
setValue(sheet, 3, 6, 100.0);
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
Assert.AreEqual(26.0, fe.Evaluate(cell0).NumberValue, 0.0);
Assert.AreEqual(56.0, fe.Evaluate(cell1).NumberValue, 0.0);
}
private static void setValue(NPOI.SS.UserModel.Sheet sheet, int rowIndex, int colIndex, double value)
{
Row row = sheet.GetRow(rowIndex);
if (row == null)
{
row = sheet.CreateRow(rowIndex);
}
row.CreateCell(colIndex).SetCellValue(value);
}
/**
* {@link HSSFFormulaEvaluator#Evaluate(Cell)} should behave the same whether the cell
* is <c>null</c> or blank.
*/
[TestMethod]
public void TestEvaluateBlank()
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
Assert.IsNull(fe.Evaluate(null));
NPOI.SS.UserModel.Sheet sheet = wb.CreateSheet("Sheet1");
Cell cell = sheet.CreateRow(0).CreateCell(0);
Assert.IsNull(fe.Evaluate(cell));
}
}
}
|