Using Reactive Extensions for Excel to subscribe to variable

May 25, 2015 at 2:16 AM
Hi would it be possible to use Reactive Extensions for Excel to subscribe to a variable in a class?For example I'd like to use this for an excel function called GetError() that returns any error states from other ExcelDNA calls.
May 25, 2015 at 11:32 PM
To hook up a real-time function with Excel-DNA, you need some function that returns an IObservable<T>. Using the Reactive Extensions library, it's quite easy to build an IObservable<T> from an event (there's a helper on the Observable class in the Rx library).

I'm not sure what you mean by 'subscribe to a variable in a class'. If you mean a property of a class, then you'd need some way of being notified that the property has changed. The standard way of doing that is for your class to implement the INotifyPropertyChanged interface. Then you can build a Reactive Extensions IObservable from that property change notification using something like this:

Once you have the function returning IObservable<T>, the rest is straight-forward. A slightly updated version of the RxExcel helper is the ObservableRtdUtil.cs file that you can find here: Just include that file in your project, and use it in a wrapper function as:

// You have to figure out how to make this function
public IObservable<object> GetVariableObservable(string arg1, double arg2) {...}

// Then this is the Excel-DNA wrapper to make a worksheet UDF
public static object WatchVariable(string arg1, double arg2)
    return ObservableRtdUtil.Observe("WatchVariable", new object[] {arg1, arg2}, () => GetVariableObservable(arg1, arg2));