[转]NPOI TestFunctionRegistry.cs

时间:2021-11-17 08:26:58

本文转自:https://github.com/tonyqus/npoi/blob/master/testcases/main/SS/Formula/TestFunctionRegistry.cs

  /*
  *  ====================================================================
  *    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.
  * ====================================================================
  */
   
  using System;
  using NPOI.HSSF.UserModel;
  using NPOI.SS.Formula;
  using NPOI.SS.Formula.Atp;
  using NPOI.SS.Formula.Eval;
  using NPOI.SS.Formula.Functions;
  using NPOI.SS.UserModel;
  using NUnit.Framework;
   
  namespace TestCases.SS.Formula
  {
      /**
       *
       * @author Yegor Kozlov
       */
      [TestFixture]
      public class TestFunctionRegistry
      {
          [Test]
          public void TestRegisterInRuntime()
          {
              HSSFWorkbook wb = new HSSFWorkbook();
              HSSFSheet sheet = (HSSFSheet)wb.CreateSheet("Sheet1");
              HSSFRow row = (HSSFRow)sheet.CreateRow(0);
              HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
   
              HSSFCell cellA = (HSSFCell)row.CreateCell(0);
              cellA.CellFormula = ("FISHER(A5)");
              CellValue cv;
              try
              {
                  //NPOI
                  //Run it twice in NUnit Gui Window, the first passed but the second failed.
                  //Maybe the function was cached. Ignore it.
                  cv = fe.Evaluate(cellA);
                  Assert.Fail("expectecd exception");
              }
              catch (NotImplementedException)
              {
                  ;
              }
   
              FunctionEval.RegisterFunction("FISHER", new Function1());/*Function() {
              public ValueEval Evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
                  return ErrorEval.NA;
              }
          });*/
   
              cv = fe.Evaluate(cellA);
              Assert.AreEqual(ErrorEval.NA.ErrorCode, cv.ErrorValue);
   
              HSSFCell cellB = (HSSFCell)row.CreateCell(1);
              cellB.CellFormula = ("CUBEMEMBERPROPERTY(A5)");
              try
              {
                  cv = fe.Evaluate(cellB);
                  Assert.Fail("expectecd exception");
              }
              catch (NotImplementedException)
              {
                  ;
              }
   
              AnalysisToolPak.RegisterFunction("CUBEMEMBERPROPERTY", new FreeRefFunction1());/*FreeRefFunction() {
              public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec) {
                  return ErrorEval.NUM_ERROR;
              }
          });*/
   
              cv = fe.Evaluate(cellB);
              Assert.AreEqual(ErrorEval.NUM_ERROR.ErrorCode, cv.ErrorValue);
          }
   
          private class Function1 : NPOI.SS.Formula.Functions.Function
          {
              public ValueEval Evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex)
              {
                  return ErrorEval.NA;
              }
          }
   
          private class FreeRefFunction1 : FreeRefFunction
          {
              public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec)
              {
                  return ErrorEval.NUM_ERROR;
              }
          }
   
          class Function2 : NPOI.SS.Formula.Functions.Function
          {
              public ValueEval Evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex)
              {
                  return ErrorEval.NA;
              }
          }
          [Test]
          public void TestExceptions()
          {
              NPOI.SS.Formula.Functions.Function func = new Function2();
              try
              {
                  FunctionEval.RegisterFunction("SUM", func);
                  Assert.Fail("expectecd exception");
              }
              catch (ArgumentException e)
              {
                  Assert.AreEqual("POI already implememts SUM" +
                          ". You cannot override POI's implementations of Excel functions", e.Message);
              }
              try
              {
                  FunctionEval.RegisterFunction("SUMXXX", func);
                  Assert.Fail("expectecd exception");
              }
              catch (ArgumentException e)
              {
                  Assert.AreEqual("Unknown function: SUMXXX", e.Message);
              }
              try
              {
                  FunctionEval.RegisterFunction("ISODD", func);
                  Assert.Fail("expectecd exception");
              }
              catch (ArgumentException e)
              {
                  Assert.AreEqual("ISODD is a function from the Excel Analysis Toolpack. " +
                          "Use AnalysisToolpack.RegisterFunction(String name, FreeRefFunction func) instead.", e.Message);
              }
   
              FreeRefFunction atpFunc = new FreeRefFunction2();/*FreeRefFunction() {
              public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec) {
                  return ErrorEval.NUM_ERROR;
              }
          };*/
              try
              {
                  AnalysisToolPak.RegisterFunction("ISODD", atpFunc);
                  Assert.Fail("expectecd exception");
              }
              catch (ArgumentException e)
              {
                  Assert.AreEqual("POI already implememts ISODD" +
                          ". You cannot override POI's implementations of Excel functions", e.Message);
              }
              try
              {
                  AnalysisToolPak.RegisterFunction("ISODDXXX", atpFunc);
                  Assert.Fail("expectecd exception");
              }
              catch (ArgumentException e)
              {
                  Assert.AreEqual("ISODDXXX is not a function from the Excel Analysis Toolpack.", e.Message);
              }
              try
              {
                  AnalysisToolPak.RegisterFunction("SUM", atpFunc);
                  Assert.Fail("expectecd exception");
              }
              catch (ArgumentException e)
              {
                  Assert.AreEqual("SUM is a built-in Excel function. " +
                          "Use FunctoinEval.RegisterFunction(String name, Function func) instead.", e.Message);
              }
          }
          class FreeRefFunction2 : FreeRefFunction
          {
              public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec)
              {
                  return ErrorEval.NUM_ERROR;
              }
          }
      }
   
  }

[转]NPOI TestFunctionRegistry.cs的更多相关文章

  1. NPOI 在指定单元格导入导出图片

    NPOI 在指定单元格导入导出图片 Intro 我维护了一个 NPOI 的扩展,主要用来导入导出 Excel 数据,最近有网友提出了导入 Excel 的时候解析图片的需求,于是就有了本文的探索 导入E ...

  2. NPOIHelper.cs (NPOI 2.1.1)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  3. C#利用NPOI处理excel的类 NPOIHelper.cs

    个人的NPOIHelp类,包括datatable导出到excel,dataset导出到excel,excel导入到datatable,excel导入到dataset, 更新excel中的数据,验证导入 ...

  4. (C#)使用NPOI导出Excel

    在做业务型的软件时,经常需要将某些数据导出,本文介绍了在Winform或Asp.net中使用NPOI(POI 项目的 .NET 版本)来操作Excel文件,而无需安装Office. 首先,需要获取NP ...

  5. 使用NPOI从Excel中提取图片及图片位置信息

    问题背景: 话说,在ExcelReport的开发过程中,有一个比较棘手的问题:怎么复制图片呢? 当然,解决这个问题的第一步是:能使用NPOI提取到图片及图片的位置信息.到这里,一切想法都很顺利.但NP ...

  6. 分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility

    1. ExcelUtility功能:  1.将数据导出到EXCEL(支持XLS,XLSX,支持多种类型模板,支持列宽自适应)  类名:ExcelUtility. Export  2.将EXCEL ...

  7. ASP.NET使用NPOI加载Excel模板并导出下载

    1.为什么要使用NPOI导出Excel? 一.解决传统操作Excel遇到的问题: 如果是.NET,需要在服务器端装Office,且及时更新它,以防漏洞,还需要设定权限允许.NET访问COM+,如果在导 ...

  8. 使用NPOI将TABLE内容导出到EXCEL

    项目中需要将页面中的table内容导出到EXCEL,在用了几种方法后发现NPO是最快&最好的 需要应用 NPOI.dll 还有个Ionic.Zip.dll不知道有用没,没去研究,两个DLL都放 ...

  9. NPOI 读写Excel

    实例功能概述: 1.支持Excel2003以及2007 2.支持Excel读取到DataTable(TableToExcel) 3.支持DataTable导出到Excel(TableToExcel) ...

随机推荐

  1. children和childNodes的区别

    children和childNodes 1,childNodes 属性,标准的,它返回指定元素的子元素集合,包括HTML节点,所有属性,文本.可以通过nodeType来判断是哪种类型的节点,只有当no ...

  2. Centos7升级gcc学习笔记

    概述 最近在学习<深入应用C++11-代码与优化与工程级应用>,我的gcc版本是gcc-4.8.5是支持C++11的,但是我在作者的github上看了一些C++例子,其中有些是C++14的 ...

  3. 【Xamarin挖墙脚系列:移动设备应用的开发周期及准则】

    原文:[Xamarin挖墙脚系列:移动设备应用的开发周期及准则] 原文地址:https://developer.xamarin.com/guides/cross-platform/getting_st ...

  4. javaIO流小结&lpar;1&rpar;

    UTF-8的字节占多少个字节? 常用中文字符用utf-8编码占用3个字节(大约2万多字),超大字符集中要占4个字节.在内存中是2个字节,真正写到硬盘上面的是3个字节. GBK.GB2312汉字占2个字 ...

  5. 动态面板——axure线框图部件库介绍

    1.什么是Axure的动态面板 按照Axure官方网站的解释 :动态面板控件(Dynamic Panel)可以让你实现高级的交互功能,实现原型的高保真度.动态面板包含有多个状态(states),每个状 ...

  6. 四、Snapman多人协作电子表格之——Exprtk脚本

    Snapman多人协作电子表格是一个即时工作系统. Snapman中嵌入了Exprtk脚本进行公式数据运算.Exprtk是一种高性能的脚本,经测试它的数据运算性能只比C#和java底20%. 一.Ex ...

  7. UVA 1627 Team them up&excl;

    https://cn.vjudge.net/problem/UVA-1627 题目 有n(n≤100)个人,把他们分成非空的两组,使得每个人都被分到一组,且同组中的人相互认识.要求两组的成员人数尽量接 ...

  8. Log4Net 常见错误提示(不断更新中)

    1. 无法识别log4中的节点,如:<section>等 解决办法:在configrition中直接申明log4 <configSections><!--必须为第一个节点 ...

  9. OP社区相关

    ●相关网站官网: http://openstack.org Wiki: http://wiki.openstack.org 代码贡献统计:http://stackalytics.com/ Bug跟踪: ...

  10. 修改placeholder的值---input-placeholder

    input-placeholder .box::input-placeholder 目前所有的浏览器都要加前缀 .box::-webkit-input-placeholder{ color: agba ...