Step 2: Creating a Managed-Code UDF
After you have added a reference to Microsoft.Office.Excel.Server.Udf.dll to your project, the next step is to create some custom functions and mark them with the Excel Services user-defined function (UDF) attributes.
You must mark your UDF class with the Microsoft.Office.Excel.Server.Udf.UdfClass attribute, and mark the UDF methods with the Microsoft.Office.Excel.Server.Udf.UdfMethod attribute.
Any methods that are not marked with the Microsoft.Office.Excel.Server.Udf.UdfMethod attribute in the UDF assembly will be ignored, because they will not be considered UDF methods.
The Microsoft.Office.Excel.Server.Udf.UdfMethod attribute has an IsVolatile property. You use the IsVolatile property to specify a UDF method as volatile or nonvolatile. The IsVolatile property takes a Boolean value. The default value is false, which means that particular UDF method is nonvolatile.
Creating UDFs
To add directives
The types to use are defined in the Microsoft.Office.Excel.Server.Udf namespace. Adding a using (or Imports) directive at the top of the Class1.cs file will allow you to use the types in Microsoft.Office.Excel.Server.Udf without having to fully qualify the types in the namespace.
To add this directive, add the following code to the beginning of your code in the Class1.cs file, after
using System.Text:
using Microsoft.Office.Excel.Server.Udf;
To mark a UDF class and methods
Mark
Class1
as a UDF class by adding the following attribute just abovepublic class Class1
:[UdfClass]
Create a function that takes a number (of type double), and in the function, multiply the number by 9. The function is a UDF method that is nonvolatile. Add the following code to
Class1
:[UdfMethod] public double MyDouble(double d) { return d * 9; }
Note
The default value for the IsVolatile property is false, which means that particular UDF method is nonvolatile. Therefore, it is sufficient to mark a nonvolatile UDF method as
[UdfMethod]
. It is not necessary to mark it as[UdfMethod(IsVolatile = false)]
.Create another function that returns the current date using the System.DateTime.Today property. The function is a UDF method that is volatile. Add the following code to
Class1
:[UdfMethod(IsVolatile = true)] public DateTime ReturnDateTimeToday() { return (DateTime.Today); }
To build the project
On the Build menu, click Build Solution.
You should find SampleUdf.dll assembly in the directory where you have saved your project.
Complete Code
The following code sample is the complete code in the Class1.cs example file described in the previous procedures.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Excel.Server.Udf;
namespace SampleUdf
{
[UdfClass]
public class Class1
{
[UdfMethod]
public double MyDouble(double d)
{
return d * 9;
}
[UdfMethod(IsVolatile = true)]
public DateTime ReturnDateTimeToday()
{
return (DateTime.Today);
}
}
}
See Also
Tasks
Step 1: Creating a Project and Adding a UDF Reference
Step 3: Deploying and Enabling UDFs
Step 4: Testing and Calling UDFs from Cells
How to: Create a UDF that Calls a Web Service
Concepts
Walkthrough: Developing a Managed-Code UDF
Understanding Excel Services UDFs