|
下面的自定义函数是计算300*400*500mm 这种立方体的体积或者侧面积的,函数中加入了自定义类,但自定义类的代码怎么改都在Excel里计算还是显示#value。
- public static object MsBase(string dim, UInt16 opt = 0)
- {
- string pattern = @"\d+\*\d+\*\d+";
- if (Regex.IsMatch(dim,pattern))
- {
- string tmp = Regex.Match(dim, pattern).Value;
- string[] arg = Regex.Split(tmp, @"/*");
- double L = Convert.ToDouble(arg[0]) / 1000;
- double W = Convert.ToDouble(arg[1]) / 1000;
- double H = Convert.ToDouble(arg[2]) / 1000;
- Box myclass = new Box(L,W,H);
- if (opt == 0) { return myclass.Cube(); }
- if (opt == 1) { return myclass.SideArea(); }
- else { return ExcelError.ExcelErrorNA; }
- }
- else { return ExcelError.ExcelErrorNA; }
- }
- }
复制代码- public class Box
- {
- public double L { get; set; }
- public double W { get; set; }
- public double H
- {
- get; set;
- }
- public bool IsValid { get; set; }
-
- // Constructor
- public Box(double param1,double param2,double param3)
- {
- L = param1;
- W = param2;
- H = param3;
-
- }
- //Function
- public double Cube() { return L * W * H; }
- public double SideArea() { return 2 * (L + W) * H; }
- public double BottomArea() { return L * W; }
-
- }
- }
复制代码
|
|