Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
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 Servicios de Excel 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.
Creación de UDF
Para agregar directivas
Los tipos que se van a usar se definen en el espacio de nombres Microsoft.Office.Excel.Server.Udf . 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.
Para agregar esta directiva, agregue el código siguiente al principio del código en el archivo Class1.cs, después de
using System.Text:using Microsoft.Office.Excel.Server.Udf;Imports Microsoft.Office.Excel.Server.Udf
Para marcar una clase UDF y los métodos UDF
Marque
Class1como una clase UDF agregando el atributo siguiente justo encimapublic class Class1de :[UdfClass]<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. Agregue el siguiente código a
Class1:[UdfMethod] public double MyDouble(double d) { return d * 9; }<UdfMethod> _ Public Function MyDouble(ByVal d As Double) As Double Return d * 9 End FunctionNota:
[!NOTA] The default value for the IsVolatile property is false, which means that particular UDF method is nonvolatile. Por lo tanto, basta con marcar un método UDF no volátil como
[UdfMethod]. No es necesario marcarlo como[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. Agregue el siguiente código a
Class1:[UdfMethod(IsVolatile = true)] public DateTime ReturnDateTimeToday() { return (DateTime.Today); }<UdfMethod(IsVolatile := True)> _ Public Function ReturnDateTimeToday() As Date Return (Date.Today) End Function
Para generar el proyecto
- En el menú Generar, haga clic en Generar solución.
- You should find SampleUdf.dll assembly in the directory where you have saved your project.
Código completo
El ejemplo de código siguiente es el código completo del archivo de ejemplo Class1.cs que se describe en los procedimientos anteriores.
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);
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.Office.Excel.Server.Udf
Namespace SampleUdf
<UdfClass> _
Public Class Class1
<UdfMethod> _
Public Function MyDouble(ByVal d As Double) As Double
Return d * 9
End Function
<UdfMethod(IsVolatile := True)> _
Public Function ReturnDateTimeToday() As Date
Return (Date.Today)
End Function
End Class
End Namespace
Vea también
Tareas
- Step 1: Creating a Project and Adding a UDF Reference
- Step 3: Deploying and Enabling UDFs
- Step 4: Testing and Calling UDFs from Cells
- Crear una UDF que llame al servicio web