//create a new workbook var workbook = new GrapeCity.Documents.Excel.Workbook(); try { Excel.Workbook.AddCustomFunction(new BigIntegerPowFunction()); } catch (Exception) { // Function was added } // End Try try { Excel.Workbook.AddCustomFunction(new BigIntegerMultiplyFunction()); } catch (Exception) { // Function was added } // End Try try { Excel.Workbook.AddCustomFunction(new GreatestCommonDivisionFunction()); } catch (Exception) { // Function was added } // End Try // Use BigInteger to calculate results IWorksheet worksheet = workbook.ActiveSheet; worksheet.Range["A1"].Value = "154382190 ^ 3 = "; worksheet.Range["A2"].Value = "1643590 * 166935 = "; worksheet.Range["A3"].Value = "Greatest common division = "; worksheet.Range["B1"].Formula = "=BIG.INTEGER.POW(\"154382190\", 3)"; worksheet.Range["B2"].Formula = "=BIG.INTEGER.MULT(\"1643590\", \"166935\")"; worksheet.Range["B3"].Formula = "=BIG.INTEGER.GCD(B1,B2)"; // Arrange worksheet.Columns[0].AutoFit(); worksheet.Columns[1].ColumnWidth = worksheet.Range["B1"].Text.Length + 1; #region Formula implementation // internal class BigIntegerMultiplyFunction : CustomFunction // { // public BigIntegerMultiplyFunction() : base("BIG.INTEGER.MULT", FunctionValueType.Object, new[] // { // new Parameter(FunctionValueType.Text), // new Parameter(FunctionValueType.Text) // }) // { // } // public override object Evaluate(object[] arguments, ICalcContext context) // { // if (!(arguments[0] is string) || !(arguments[1] is string)) // { // return CalcError.Value; // } // var leftNumber = (string)arguments[0]; // var rightNumber = (string)arguments[1]; // try // { // return BigInteger.Parse(leftNumber) * BigInteger.Parse(rightNumber); // } // catch (FormatException) // { // return CalcError.Value; // } // catch (ArgumentException) // { // return CalcError.Value; // } // } // } // internal class BigIntegerPowFunction : CustomFunction // { // public BigIntegerPowFunction() : base("BIG.INTEGER.POW", FunctionValueType.Object, new[] // { // new Parameter(FunctionValueType.Text), // new Parameter(FunctionValueType.Number) // }) // { // } // public override object Evaluate(object[] arguments, ICalcContext context) // { // if (!(arguments[0] is string) || !(arguments[1] is double)) // { // return CalcError.Value; // } // var number = (string)arguments[0]; // var exp = (double)arguments[1]; // if (exp > int.MaxValue || exp < int.MinValue) // { // return CalcError.Value; // } // var iExp = Convert.ToInt32(exp); // try // { // return BigInteger.Pow(BigInteger.Parse(number), iExp); // } // catch (FormatException) // { // return CalcError.Value; // } // catch (ArgumentException) // { // return CalcError.Value; // } // } // } // internal class GreatestCommonDivisionFunction : CustomFunction // { // public GreatestCommonDivisionFunction() : base("BIG.INTEGER.GCD", FunctionValueType.Object, new[] { // new Parameter(FunctionValueType.Object, false, true), // new Parameter(FunctionValueType.Object, false, true) // }) // { // } // public override object Evaluate(object[] arguments, ICalcContext context) // { // if (!(arguments[0] is BigInteger) || !(arguments[1] is BigInteger)) // { // return CalcError.Value; // } // var leftNumber = (BigInteger)arguments[0]; // var rightNumber = (BigInteger)arguments[1]; // try // { // return BigInteger.GreatestCommonDivisor(leftNumber, rightNumber); // } // catch (ArgumentException) // { // return CalcError.Value; // } // } // } #endregion // Save to a pdf file workbook.Save("CustomObjectInCustomFunction.pdf");
' Create a new Workbook Dim workbook As New Workbook Try Excel.Workbook.AddCustomFunction(New BigIntegerPowFunction) Catch ex As Exception ' Function was added End Try Try Excel.Workbook.AddCustomFunction(New BigIntegerMultiplyFunction) Catch ex As Exception ' Function was added End Try Try Excel.Workbook.AddCustomFunction(New GreatestCommonDivisionFunction) Catch ex As Exception ' Function was added End Try ' Use BigInteger to calculate results Dim worksheet As IWorksheet = workbook.ActiveSheet With worksheet.Range !A1.Value = "154382190 ^ 3 = " !A2.Value = "1643590 * 166935 = " !A3.Value = "Greatest common division = " !B1.Formula = "=BIG.INTEGER.POW(""154382190"", 3)" !B2.Formula = "=BIG.INTEGER.MULT(""1643590"", ""166935"")" !B3.Formula = "=BIG.INTEGER.GCD(B1,B2)" End With ' Arrange worksheet.Columns(0).AutoFit() worksheet.Columns(1).ColumnWidth = worksheet.Range!B1.Text.Length + 1 #Region "Formula implementation" ' Friend Class BigIntegerMultiplyFunction ' Inherits CustomFunction ' Sub New() ' MyBase.New("BIG.INTEGER.MULT", FunctionValueType.Object, ' {New Parameter(FunctionValueType.Text), ' New Parameter(FunctionValueType.Text)}) ' End Sub ' Public Overrides Function Evaluate(arguments() As Object, context As ICalcContext) As Object ' If TypeOf arguments(0) IsNot String OrElse ' TypeOf arguments(1) IsNot String Then ' Return CalcError.Value ' End If ' Dim leftNumber = CStr(arguments(0)) ' Dim rightNumber = CStr(arguments(1)) ' Try ' Return BigInteger.Parse(leftNumber) * BigInteger.Parse(rightNumber) ' Catch ex As FormatException ' Return CalcError.Value ' Catch ex As ArgumentException ' Return CalcError.Value ' End Try ' End Function ' End Class ' Friend Class BigIntegerPowFunction ' Inherits CustomFunction ' Sub New() ' MyBase.New("BIG.INTEGER.POW", FunctionValueType.Object, ' {New Parameter(FunctionValueType.Text), ' New Parameter(FunctionValueType.Number)}) ' End Sub ' Public Overrides Function Evaluate(arguments() As Object, context As ICalcContext) As Object ' If TypeOf arguments(0) IsNot String OrElse ' TypeOf arguments(1) IsNot Double Then ' Return CalcError.Value ' End If ' Dim number = CStr(arguments(0)) ' Dim exp = CDbl(arguments(1)) ' If exp > Integer.MaxValue OrElse exp < Integer.MinValue Then ' Return CalcError.Value ' End If ' Dim iExp = CInt(exp) ' Try ' Return BigInteger.Pow(BigInteger.Parse(number), iExp) ' Catch ex As FormatException ' Return CalcError.Value ' Catch ex As ArgumentException ' Return CalcError.Value ' End Try ' End Function ' End Class ' Friend Class GreatestCommonDivisionFunction ' Inherits CustomFunction ' Sub New() ' MyBase.New("BIG.INTEGER.GCD", ' FunctionValueType.Object, ' {New Parameter(FunctionValueType.Object, False, True), ' New Parameter(FunctionValueType.Object, False, True)}) ' End Sub ' Public Overrides Function Evaluate(arguments As Object(), context As ICalcContext) As Object ' If TypeOf arguments(0) IsNot BigInteger OrElse ' TypeOf arguments(1) IsNot BigInteger Then ' Return CalcError.Value ' End If ' Dim leftNumber = DirectCast(arguments(0), BigInteger) ' Dim rightNumber = DirectCast(arguments(1), BigInteger) ' Try ' Return BigInteger.GreatestCommonDivisor(leftNumber, rightNumber) ' Catch ex As ArgumentException ' Return CalcError.Value ' End Try ' End Function ' End Class #End Region ' save to a pdf file workbook.Save("CustomObjectInCustomFunction.pdf")