Step 4: Testing and Calling UDFs from Cells
In this step, you will test the SampleUdf.dll assembly you created, deployed, and enabled in the previous steps. To test the user-defined function (UDF), you will:
Create a workbook with a named range and formulas that call the functions in SampleUdf.dll.
Save the workbook to a SharePoint document library that is a trusted location.
It is assumed that you have already created a SharePoint document library and made it a trusted location. For information about how to trust a location, see the "Trusting a Location" section in Step 3: Deploying and Enabling UDFs.
Change parameters to recalculate the workbook.
To call UDFs from cells
Start Microsoft Office Excel 2007.
In cell A1, type the formula to call the
MyDoublefunction in SampleUdf.dll. The
MyDoublefunction takes an argument of type double. In this example, you will take the argument from cell B1. In cell A1, type =MyDouble(B1).
The formula will evaluate to "#NAME?" in Excel. The formula will be evaluated only when the workbook is displayed in Excel Services.
You can run UDFs on both the client and server. A future article published on MSDN will explain the details. They are omitted here for the sake of simplicity.
In cell B1, type the number 8.
Make cell B1 a named range. First click the Formulas tab. Then click cell B1 to select it. On the Formulas tab, in the Named Cells group, click Name a Range. In the New Name dialog box, in the Name box, type MyDoubleParam.
In cell A2, type the formula to call the
In cell A3, type the formula to call the
ReturnDateTimeTodayfunction. Type=ReturnDateTimeToday(). Next, right-click cell A3 to display the menu. Click Format Cells.
In the Format Cells dialog box, on the Number tab, select Date. Select a date format type from the Type list—for example, *3/4/2001.
Save the workbook to a location of your choice on the local drive. Name the workbook "TestSampleUdf.xlsx".
To save to Excel Services
Click the Microsoft Office Button, point to Save As, and click Save for Excel Services.
In the Save As dialog box, click Excel Services Options.
In the Excel Services Options dialog box, on the Show tab, make sure that Entire Workbook is selected.
In the Add Parameters list, select the MyDoubleParam check box.
Click OK. You should now see "MyDoubleParam" listed in the Parameters list.
In the Save As dialog box, make sure that the Open this workbook in my browser after I save check box is selected.
In the File name box, type the path to the trusted SharePoint document library where you want to store this workbook. For example, http://MyServer002/Shared%20Documents/TestSampleUdf.xlsx.
Click Save. You should see TestSampleUdf.xlsx in Excel Web Access. In cell A1, you should see the number "72" because cell B1 * 9 = 8 * 9, which is 72. In cell A2 you should see a number. In cell A3, you should see the current date.
In cell A2, the number represents the number of days since 1/1/1900 (or 1/1/1904 if you have "Use 1904 Date System" turned on). It's how Excel represents dates internally.
To change parameters to test the UDF
In the Parameters pane, you should see the named range for cell B1, that is, "MyDoubleParam".
You can change the value in cell B1 by typing a number in the box next to "MyDoubleParam". For example, if you type 3 and then click Apply, Excel Services will recalculate the workbook. Cell A1 will contain "27" instead of "72".