CPF/CPF.ReoGrid/Formula/FormulaValue.cs
2024-06-24 10:15:59 +08:00

100 lines
2.3 KiB
C#

using System;
namespace CPF.ReoGrid.Formula
{
internal struct FormulaValue
{
public FormulaValue(FormulaValueType type, object value)
{
this.type = type;
this.value = value;
}
public override string ToString()
{
return string.Format("Value[{0}: {1}]", this.type, this.value);
}
public static implicit operator string(FormulaValue value)
{
return Convert.ToString(value.value);
}
public static implicit operator FormulaValue(string text)
{
return new FormulaValue(FormulaValueType.String, text);
}
public static implicit operator double(FormulaValue value)
{
return (value.type != FormulaValueType.Number) ? 0.0 : ((double)value.value);
}
public static implicit operator FormulaValue(double num)
{
return new FormulaValue(FormulaValueType.Number, num);
}
public static implicit operator bool(FormulaValue value)
{
return value.type == FormulaValueType.Boolean && (bool)value.value;
}
public static implicit operator FormulaValue(bool b)
{
return new FormulaValue(FormulaValueType.Boolean, b);
}
public static implicit operator DateTime(FormulaValue value)
{
return (value.type != FormulaValueType.DateTime) ? new DateTime(1900, 1, 1) : ((DateTime)value.value);
}
public static implicit operator FormulaValue(DateTime b)
{
return new FormulaValue(FormulaValueType.DateTime, b);
}
public static implicit operator CellPosition(FormulaValue value)
{
return (value.type != FormulaValueType.Cell) ? CellPosition.Empty : ((CellPosition)value.value);
}
public static implicit operator FormulaValue(CellPosition pos)
{
return new FormulaValue(FormulaValueType.Cell, pos);
}
public static implicit operator RangePosition(FormulaValue value)
{
return (value.type != FormulaValueType.Range) ? RangePosition.Empty : ((RangePosition)value.value);
}
public static implicit operator FormulaValue(RangePosition range)
{
return new FormulaValue(FormulaValueType.Range, range);
}
public FormulaValueType type;
public object value;
public static FormulaValue Nil = new FormulaValue
{
type = FormulaValueType.Nil
};
public static FormulaValue True = new FormulaValue
{
type = FormulaValueType.Boolean,
value = true
};
public static FormulaValue False = new FormulaValue
{
type = FormulaValueType.Boolean,
value = false
};
}
}