Вопросы и ответы о UDF служб Excel

Здесь приведены вопросы и ответы, касающиеся пользовательских функций (UDF) в службах Excel.

Создание UDF с использованием управляемого кода

Что такое поддерживаемый класс UDF?

The UDF class in a UDF assembly must be public. It can be sealed. It cannot be abstract, internal, or private. It must have a parameterless, public constructor. For languages that automatically generate a parameterless, public constructor (for example, C#), you can have no constructor at all.

Что такое поддерживаемый метод UDF?

The UDF method in a UDF assembly must be public. The UDF method must be thread-safe.

У метода UDF не может быть следующего:

  • параметров ref или out;

  • атрибутов retval;

  • аргументов Optional;

  • неподдерживаемых типов данных.

The UDF method must also have a supported return type. Список поддерживаемых типов данных см. в подразделе "Типы данных" этого раздела.

Можно ли вызывать веб-службу из сборки UDF?

Да. For an example, see the following UDF sample code. См. также практическое руководство. Создание определяемой пользователем функции, которая вызывает веб-службу.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Excel.Server.Udf;
using UdfWS.dk.iter.webservices;

namespace UdfWS
{
    [UdfClass]
    public class MyUdfClass
    {
        // Instantiate the Web service. The Web service used is at   
        // http://webservices.iter.dk/calculator.asmx
        Calculator calc = new Calculator();

        [UdfMethod]
        public int MyFunction()
        {
            int i;
            i = (i + 88) * 2;
            return i;
        }

        [UdfMethod(IsVolatile = true)]
        public double MyDouble(double d)
        {
            return d * 9;
        }

        [UdfMethod]
        public int AddMe(int a, int b)
        {
            int c;
            // Call the Web service Add method
            c = calc.Add(a, b);
            return c;
        }        
    }
}

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.Office.Excel.Server.Udf
Imports UdfWS.dk.iter.webservices

Namespace UdfWS
    <UdfClass> _
    Public Class MyUdfClass
        ' Instantiate the Web service. The Web service used is at   
        ' http://webservices.iter.dk/calculator.asmx
        Private calc As New Calculator()

        <UdfMethod> _
        Public Function MyFunction() As Integer
            Dim i As Integer
            i = (i + 88) * 2
            Return i
        End Function

        <UdfMethod(IsVolatile := True)> _
        Public Function MyDouble(ByVal d As Double) As Double
            Return d * 9
        End Function

        <UdfMethod> _
        Public Function AddMe(ByVal a As Integer, ByVal b As Integer) As Integer
            Dim c As Integer
            ' Call the Web service Add method
            c = calc.Add(a, b)
            Return c
        End Function
    End Class
End Namespace

Типы данных

Какие типы данных можно использовать как параметры UDF?

Вот поддерживаемые типы данных:

  • Числовые типы: Double, Single, Int32, UInt32, Int16, UInt16, Byte, Sbyte.

  • String.

  • Boolean.

  • Массивы объектов: одномерные или двумерные массивы, то есть объект [] и объект [,].

  • DateTime.

Каковы поддерживаемые типы возвращаемого значения?

Вот поддерживаемые типы возвращаемого значения:

  • Числовые типы: Double, Single, Int32, UInt32, Int16, UInt16, Byte, Sbyte.

  • String.

  • Boolean.

  • Массивы объектов: одномерные или двумерные массивы, то есть объект [], объект [,], int[] и int[,]).

  • DateTime.

  • Object.

XLL.

Поддерживаются ли XLL?

Not directly. Службы Excel will load and call only managed-code UDFs. However, you can write a managed-code wrapper to call the XLLs and deploy the XLLs to the server, together with the managed-code wrapper assembly.