Caching and Asynchronous Excel UDFs

This sample shows how the result of an Excel-DNA async UDF call can be cached using the .NET 4 MemoryCache class.

<DnaLibrary Name="CachedAsyncSample" RuntimeVersion="v4.0" Language="C#">

<Reference Name="System.Runtime.Caching" />
<![CDATA[
using System;
using System.Threading;
using System.Runtime.Caching;
using ExcelDna.Integration;

public static class dnaFunctions
{
    public static object dnaCachedAsync(string input)
    {
        // First check the cache, and return immediately 
        // if we found something.
        // (We also need a unique key to identify the cache item)
        string key = "dnaCachedAsync:" + input;
        ObjectCache cache = MemoryCache.Default; 
        string cachedItem = cache[key] as string;
        if (cachedItem != null) 
            return cachedItem;

        // Not in the cache - make the async call 
        // to retrieve the item. (The second parameter here should identify 
        // the function call, so would usually be an array of the input parameters, 
        // but here we have the identifying key already.)
        object asyncResult = ExcelAsyncUtil.Run("dnaCachedAsync", key, () => 
        {
            // Here we fetch the data from far away....
            // This code will run on a ThreadPool thread.

            // To simulate a slow calculation or web service call,
            // Just sleep for a few seconds...
            Thread.Sleep(5000);

            // Then return the result
            return "The calculation with input " 
                    + input + " completed at " 
                    + DateTime.Now.ToString("HH:mm:ss");
        });

        // Check the asyncResult to see if we're still busy
        if (asyncResult.Equals(ExcelError.ExcelErrorNA))
            return "!!! Fetching data";

        // OK, we actually got the result this time.
        // Add to the cache and return
        // (keeping the cached entry valid for 1 minute)
        // Note that the function won't recalc automatically after 
        //    the cache expires. For this we need to go the 
        //    RxExcel route with an IObservable.
        cache.Add(key, asyncResult, DateTime.Now.AddMinutes(1), null);
        return asyncResult;
    }

    public static string dnaTest()
    {
        return "Hello from CachedAsyncSample";
    }
}

public class AddIn : IExcelAddIn
{
    public void AutoOpen() { ExcelAsyncUtil.Initialize(); }
    public void AutoClose() { ExcelAsyncUtil.Uninitialize(); }
}

]]>
</DnaLibrary>
Posted in Samples | Tagged , , , ,

Async and event-streaming Excel UDFs with F#

There have been a some recent posts mentioning the asynchronous and reactive programming features in F#. Since Excel-DNA 0.30 added support for creating async and IObservable-based real-time data functions, I’d like to show how these F# features can be nicely exposed to Excel via Excel-DNA.

IObservable to Excel via Excel-DNA

Excel-DNA 0.30 allows an add-in to expose IObservable sources to Excel as real-time data functions. (Actually Excel-DNA defines an interface called IExcelObservable that matches the semantics of IObservable<object> – this is because we still target .NET 2.0 with the core library.)

Asynchronous function can then be implemented as an IObservable that returns a single value before completing. Cancellation (triggered when the user removes a real-time or async formula) is supported via the standard IDisposable mechanism.

Internally, Excel-DNA implements a thread-safe RTD server and registers the IObservable as an RTD topic. So some aspects of the IObservable support are subject to Excel’s RTD feature works, for example the RTD throttle interval (by default 2 seconds) will also apply to IObservable functions.

The following functions in the ExcelDna.Integration.ExcelAsyncUtil helper class are relevant:

  • ExcelAsyncUtil.Initialize() – this should be called in a macro context before any of the other features are used, typically from the AutoOpen() handler.
  • ExcelAsyncUtil.Observe(...) – registers an IExcelObservable as a real-time data function with Excel. Subsequent OnNext() calls will raise updates via RTD.
  • ExcelAsyncUtil.Run(...) – a helper method that runs a function asynchronously on a .NET threadpool thread.

In addition, we’ll use

  • ExcelObservableSource – a delegate type for functions that return an IExcelObservable.

Some links:

F# helpers for async and IObservable-based events

To initialize the async support in Excel-DNA, we need some code like the following:

namespace FsAsync

open System
open System.Threading
open System.Net
open Microsoft.FSharp.Control.WebExtensions
open ExcelDna.Integration

/// This class implements the IExcelAddin which allows us to initialize the ExcelAsyncUtil support.
/// It must not be a nested class (e.g. defined as a type inside a module) but a top-level class (inside a namespace)
type FsAsyncAddIn () =
    interface IExcelAddIn with
        member this.AutoOpen ()  = 
            ExcelAsyncUtil.Initialize ()
        member this.AutoClose () = ExcelAsyncUtil.Uninitialize ()

    // define a regular Excel UDF just to show that the add-in works
    [<ExcelFunction(Description="A test function from F#")>]
    static member fsaAddThem (x:double) (y:double) = x + y

F# supports an asynchronous programming model via ‘async computation expressions’. The result of an async computation expression is a value of type Async<'T>, which we need to convert to an IExcelObservable. We use a standard CancellationTokenSource hooked up to the IDisposable to enable cancellation.

module FsAsyncUtil =

    /// A helper to pass an F# Async computation to Excel-DNA 
    let excelRunAsync functionName parameters async =
        let obsSource =
            ExcelObservableSource(
                fun () -> 
                { new IExcelObservable with
                    member __.Subscribe observer =
                        // make something like CancellationDisposable
                        let cts = new CancellationTokenSource ()
                        let disp = { new IDisposable with member __.Dispose () = cts.Cancel () }
                        // Start the async computation on this thread
                        Async.StartWithContinuations 
                            (   async, 
                                ( fun result -> 
                                    observer.OnNext(result)
                                    observer.OnCompleted () ),
                                ( fun ex -> observer.OnError ex ),
                                ( fun ex ->
                                    observer.OnCompleted () ),
                                cts.Token 
                            )
                        // return the disposable
                        disp
                }) 
        ExcelAsyncUtil.Observe (functionName, parameters, obsSource)

Another neat feature of F# is that events are first-class types that implement IObservable. This means any F# event can serve as a real-time data source in an Excel formula. To bridge the F# events to the IExcelObservable interface is really easy, we just have the following helper:

    /// A helper to pass an F# IObservable to Excel-DNA
    let excelObserve functionName parameters observable = 
        let obsSource =
            ExcelObservableSource(
                fun () -> 
                { new IExcelObservable with
                    member __.Subscribe observer =
                        // Subscribe to the F# observable
                        Observable.subscribe (fun value -> observer.OnNext (value)) observable
                })
        ExcelAsyncUtil.Observe (functionName, parameters, obsSource)

Sample functions

Given the above helpers, we can now explore a few ways to implement async and real-time streaming functions. As examples:

Here is a plain synchronous function to download a url into a string:

let downloadString url = 
    try
        let uri = new System.Uri(url)
        let webClient = new WebClient()
        let html = webClient.DownloadString(uri)
        html
    with
        | ex -> "!!!ERROR: " + ex.Message
  • Async implementation 1: Use Excel-DNA async directly to run downloadString on a ThreadPool thread
    let downloadStringAsyncRunTP1 url = 
        ExcelAsyncUtil.Run ("downloadStringAsyncTP1", url, (fun () -> downloadString url :> obj))
    

Create an F# asynchronous computation for the download (this functions is not exported to Excel)

let downloadStringAsyncImpl url = async {
    try

        // In here we could check for cancellation using 
        // let! ct = Async.CancellationToken
        // if ct.IsCancellationRequested then ...
        let uri = new System.Uri(url)
        let webClient = new WebClient()
        let! html = webClient.AsyncDownloadString(uri)
        return html
    with
        | ex -> return "!!!ERROR: " + ex.Message 
    }
  • Async implementation 2: This function runs the async computation synchronously on a ThreadPool thread because that’s what ExcelAsyncUtil.Run does. Blocking calls will block a ThreadPool thread, eventually limiting the concurrency of the async calls
    let downloadStringAsyncTP2 url = 
        ExcelAsyncUtil.Run ("downloadStringAsyncTP2", url, (fun () -> Async.RunSynchronously (downloadStringAsyncImpl url) :> obj))
    
  • Async implementation 3: Use the helper we defined above. This runs the async computation using true F# async. Should not block ThreadPool threads, and allows cancellation
    let downloadStringAsync url = 
        FsAsyncUtil.excelRunAsync "downloadStringAsync" url (downloadStringAsyncImpl url)
    

Helper that will create a timer that ticks at timerInterval for timerDuration, and is then done. Also not exported to Excel (incompatible signature). Notive that the from F#, the timer.Elapsed event of the BCL Timer class implements IObservable, so can be used directly with the transformations in the F# Observable module.

let createTimer timerInterval timerDuration =
    // setup a timer
    let timer = new System.Timers.Timer(float timerInterval)
    timer.AutoReset <- true
    // return an async task for stopping it after the duration
    let timerStop = async {
        timer.Start()
        do! Async.Sleep timerDuration
        timer.Stop() 
        }
    Async.Start timerStop
    // Make sure that the type we actually observe in the event is supported by Excel
    // by converting the events to timestamps
    timer.Elapsed |> Observable.map (fun elapsed -> DateTime.Now) 
  • Event implementation: Finally this is the Excel function that will tick away in a cell. Entered into a cell (and formatted as a Time value), the formula =startTimer(5000, 60000) will show a clock that ticks every 5 seconds for a minute.
    let startTimer timerInterval timerDuration =
        FsAsyncUtil.excelObserve "startTimer" [|float timerInterval; float timerDuration|] (createTimer timerInterval timerDuration)
    

Putting everything together in an Excel add-in

A complete .dna script file with the above code can be found in the Excel-DNA distribution, under Distribution\Samples\Async\FsAsync.dna.

Alternatively, the following steps would build an add-in in Visual Studio:

  • Create a new F# library in Visual Studio.
  • Install the Excel-DNA package from NuGet (Install-Package Excel-DNA from the NuGet console).
  • Set up the Debug path:
    1. Select “Start External Program” and browse to find Excel.exe, e.g. for Excel 2010 the path might be: C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE.
    2. Enter the full path to the .xll file in the output as the Command line arguments,
      e.g. C:\MyProjects\TestDnaFs\bin\Debug\TestDnaFs-addin.xll.
  • Place the following code in Library1.fs, compile and run:
    
    namespace FsAsync
    
    open System
    open System.Threading
    open System.Net
    open Microsoft.FSharp.Control.WebExtensions
    open ExcelDna.Integration
    
    /// This class implements the IExcelAddin which allows us to initialize the ExcelAsyncUtil support.
    /// It must not be a nested class (e.g. defined as a type inside a module) but a top-level class (inside a namespace)
    type FsAsyncAddIn () =
        interface IExcelAddIn with
            member this.AutoOpen ()  = 
                ExcelAsyncUtil.Initialize ()
            member this.AutoClose () = ExcelAsyncUtil.Uninitialize ()
    
        // a regular Excel UDF just to show that the add-in works
        static member fsaAddThem (x:double) (y:double) = x + y
    
    /// Some utility functions for connecting Excel-DNA async with F#
    module FsAsyncUtil =
        /// A helper to pass an F# Async computation to Excel-DNA 
        let excelRunAsync functionName parameters async =
            let obsSource =
                ExcelObservableSource(
                    fun () -> 
                    { new IExcelObservable with
                        member __.Subscribe observer =
                            // make something like CancellationDisposable
                            let cts = new CancellationTokenSource ()
                            let disp = { new IDisposable with member __.Dispose () = cts.Cancel () }
                            // Start the async computation on this thread
                            Async.StartWithContinuations 
                                (   async, 
                                    ( fun result -> 
                                        observer.OnNext(result)
                                        observer.OnCompleted () ),
                                    ( fun ex -> observer.OnError ex ),
                                    ( fun ex ->
                                        observer.OnCompleted () ),
                                    cts.Token 
                                )
                            // return the disposable
                            disp
                    }) 
            ExcelAsyncUtil.Observe (functionName, parameters, obsSource)
    
        /// A helper to pass an F# IObservable to Excel-DNA
        let excelObserve functionName parameters observable = 
            let obsSource =
                ExcelObservableSource(
                    fun () -> 
                    { new IExcelObservable with
                        member __.Subscribe observer =
                            // Subscribe to the F# observable
                            Observable.subscribe (fun value -> observer.OnNext (value)) observable
                    })
            ExcelAsyncUtil.Observe (functionName, parameters, obsSource)
    
    // Some test functions
    module TestFunctions =
        /// Plain synchronous download function
        /// can be called from Excel
        let downloadString url = 
            try
                let uri = new System.Uri(url)
                let webClient = new WebClient()
                let html = webClient.DownloadString(uri)
                html
            with
                | ex -> "!!!ERROR: " + ex.Message
    
        /// Uses Excel-DNA async to run download on a ThreadPool thread
        let downloadStringAsyncTP1 url = 
            ExcelAsyncUtil.Run ("downloadStringAsyncTP1", url, (fun () -> downloadString url :> obj))
    
        /// Create an F# asynchronous computation for the download
        /// Not exported to Excel
        let downloadStringAsyncImpl url = async {
            try
    
                // In here we could check for cancellation using 
                // let! ct = Async.CancellationToken
                // if ct.IsCancellationRequested then ...
                let uri = new System.Uri(url)
                let webClient = new WebClient()
                let! html = webClient.AsyncDownloadString(uri)
                return html
            with
                | ex -> return "!!!ERROR: " + ex.Message 
            }
    
        /// This function runs the async computation synchronously on a ThreadPool thread
        /// because that's what ExcelAsyncUtil.Run does
        /// Blocking calls will block a ThreadPool thread, eventually limiting the concurrency of the async calls
        let downloadStringAsyncTP2 url = 
            ExcelAsyncUtil.Run ("downloadStringAsyncTP2", url, (fun () -> Async.RunSynchronously (downloadStringAsyncImpl url) :> obj))
    
        /// This runs the async computation using true F# async
        /// Should not block ThreadPool threads, and allows cancellation
        let downloadStringAsync url = 
            FsAsyncUtil.excelRunAsync "downloadStringAsync" url (downloadStringAsyncImpl url)
    
        // Helper that will create a timer that ticks at timerInterval for timerDuration, then stops
        // Not exported to Excel (incompatible type)
        let createTimer timerInterval timerDuration =
            // setup a timer
            let timer = new System.Timers.Timer(float timerInterval)
            timer.AutoReset  Observable.map (fun elapsed -> DateTime.Now) 
    
        // Excel function to start the timer - using the fact that F# events implement IObservable
        let startTimer timerInterval timerDuration =
            FsAsyncUtil.excelObserve "startTimer" [|float timerInterval; float timerDuration|] (createTimer timerInterval timerDuration)
    

Support and feedback

The best place to ask any questions related to Excel-DNA is the Excel-DNA Google group. Any feedback from F# users trying out Excel-DNA or the features discussed here will be very welcome. I can also be contacted directly at govert@icon.co.za.

Posted in Features, Samples | Tagged , , ,

Excel-DNA NuGet Package Updated

I’ve updated and improved the Excel-DNA NuGet package. (NuGet is the Visual Studio package manager that makes it easy to download and install external libraries into your projects.)

To turn your Class Library project into an Excel add-in, just open Tools -> Library Package Manager -> Package Manager Console, and enter

PM> Install-Package Excel-DNA

The Excel-DNA package now has an install script that creates the required .dna file, and a post-build step to copy the .xll and run the packing tool, and even configures debugging. The package should work for C#, Visual Basic and F# Class Library projects on:

  • Visual Studio 2010 Professional and higher
  • Visual Studio 2012 Professional and higher
  • Visual Studio 2012 Express for Windows Desktop

Please post any feedback – bugs, good or bad comments – to the Excel-DNA Google group.

Posted in Uncategorized | Tagged

Excel-DNA Version 0.30 Released

Excel-DNA Version 0.30 has now been released on CodePlex. The download
is available from http://exceldna.codeplex.com/releases/view/95861.

This version implements support for RTD-based asynchronous worksheet
functions based on a thread-safe RTD base class – ExcelRtdServer. The
asynchronous support is designed to (optionally) integrate with
the .NET 4.0 Task-based operations, as well as the Reactive Extensions
library, allowing IObservables to be exposed as ‘live’ worksheet UDFs
- (thus ‘RxExcel’). The language-specific support for asynchronous
functions found in C# 5, Visual Basic 11 and F# 2.0 can be easily
integrated with the Excel-DNA asynchronous interfaces. Some examples
experimenting with the new async features are available in the
download.

Various bug fixes have also accumulated over the last 18 months, and
are collected in this release.

As always, I greatly appreciate any feedback on this version, and on
Excel-DNA in general. Any comments or questions are welcome on the
Google group or by contacting me directly.

Changelist – Version 0.30 (12 December 2012)
Continue reading

Posted in Uncategorized | Tagged

Patrick O’Beirne’s guide for Excel VBA to VB.NET migration

Patrick O’Beirne is an Excel guru who has recently been migrating some add-ins from VBA to .NET using Excel-DNA. He has some posts on the migration, including a great list of the language and interop differences to be aware of when migrating. Worth a look…

Posted in Uncategorized

Excel VBA to VB.NET with Excel-DNA and NetOffice

Excel-DNA is a great library to help ease the path from Excel VBA to VB.NET. Last year another part of the puzzle fell in place: I discovered NetOffice, a version-independent set of Office interop assemblies put together by Sebastian Lange. By referencing the NetOffice assemblies instead of the official Primary Interop Assemblies (PIA) for Office, an Excel-DNA add-in can target various Excel versions with a single add-in, and also ease distribution of the required interop assemblies, even packing them into the .xll add-in itself.

To explore how Excel-DNA and NetOffice can combine to convert a VBA add-in to VB.NET, I picked a small add-in made by Robert del Vicario that does a risk analysis simulation inspired by the Pallisade @RISK add-in. I took Robert’s original RiskGen VBA add-in , and created a new Excel-DNA add-in in VB.NET (I used Visual Studio, but the free SharpDevelop IDE should work fine too). I documented the steps along the way of creating the VB.NET project, making an add-in based on Excel-DNA and using NetOffice to help port the VBA code to VB.NET. The resulting document (RiskGen Port Log.docx) outlining exactly how I ported the add-in, with the new VB.NET-based  RiskGen.NET is also on Robert’s site.

I’m also looking for some more examples of free/open source VBA add-ins to port to Excel-DNA. The best add-ins will contain a mix of user-defined functions and macros which use the Excel object model. Please post to the Google group or mail me directly if you have any suggestions.

And as always, if you need any support porting your Excel VBA add-ins to .NET using Excel-DNA, I’m happy to help on the Excel-DNA Google group.

Posted in Samples

Excel-DNA version 0.29 – RC available

I have posted a release candidate of Excel-DNA version 0.29 to the CodePlex site. The download is available at http://exceldna.codeplex.com/releases/view/66405. I will wait a week or two for some confirmation that this version works correctly before setting this release to ‘recommended’ status and updating the NuGet package. Any results from your testing with this version would be very helpful.

Excel-DNA version 0.29 adds support for a number of specialized Excel features. The 64-bit version of Excel 2010 is fully supported, registration-free Custom Task Panes can be created under Excel 2007 and later, direct COM server integration can improve integration with legacy VBA code, and macros with parameters are registered. In addition, there are some features to improve the development and debugging workflow, and a few minor bugfixes. The complete change list is included below.

More information about the new features will be posted on the Excel-DNA website in the coming weeks. Any comments or questions are welcome on the Google group – http://groups.google.com/group/exceldna – or by contacting me directly.

As always, I greatly appreciate any feedback on this version, and on Excel-DNA in general.

-Govert

Complete change list

  • BREAKING CHANGE! Changed SheetId in the ExcelReference type to an IntPtr for 64-bit compatibility.
  • Changed initialization – only create sandboxed AppDomain under .NET 4 (or if explicitly requested with CreateSandboxedAppDomain=’true’ attribute on DnaLibrary tag in .dna file).
  • Fixed memory leak when getting SheetId for ExcelReference parameters.
  • Fixed Ribbon RunTagMacro when no Workbook open.
  • Added support for the 64-bit version of Excel 2010 with the .Net 4 runtime.
  • Added Cluster-safe function support for Excel 2010 HPC Cluster Connector – mark functions as IsClusterSafe=true.
  • Added CustomTaskPane support and sample.
  • Added COM server support for RTD servers and other ComVisible classes. Mark ExternalLibraries and Projects as ComServer=”true” in the .dna file. Supports Regsvr32 registration or by calling ComServer.DllRegisterServer. Allows direct RTD and VBA object instantiation. Includes TypeLib registration and packing support.
  • Added support for macros with parameters.
  • Added ArrayResizer sample.
  • Added C# 4 dynamic type sample.
  • Added Path attribute to SourceItem tag to allow external source.
  • Added LoadFromBytes attribute to ExternalLibrary tag to prevent locking of .dll.
  • Added /O output path option to ExcelDnaPack.
  • Added ‘before’ option to CommandBars xml.
  • Added Int64 support for parameters and return values.
Posted in Uncategorized