I am making use of the RTD functionality awesomely provided by ExcelDNA in the following manner:
[ExcelFunction(Description = "Observable GetMetric stub", IsHidden = true, IsVolatile = false)]
public static object MyFunction(object p1, object p2, object p3, object p4)
{
string sender = xlapp.Caller.Address(External: true);
List<object> parameters = PrepareParameterList(p1, p2, p3, p4);
object[] asyncParams = new object[] { sender, parameters.ToArray() };
IProductSolutionsComServer currentInterface = CurrentAddinComInterface; // Interface into the VSTO addin where the work will take place
string key = currentInterface?.BeginMyFunction(sender, parameters.ToArray());
IObservableShim observableShim = currentInterface?.WatchKey(key); // Returns an observable-ish provided by the rest of the addin that will return the value
IExcelObservable observableWrapper = new ExcelObservable(observableShim); // More or less the example that's provided in the RTD samples
return ExcelAsyncUtil.Observe(nameof(MyFunction), asyncParams, () => observableWrapper);
}
private static List<object> PrepareParameterList(object p1, object p2, object p3, object p4)
{
var parameters = new List<object>();
if (!(p1 is ExcelMissing)) parameters.Add(p1);
if (!(p2 is ExcelMissing)) parameters.Add(p2);
if (!(p3 is ExcelMissing)) parameters.Add(p3);
if (!(p4 is ExcelMissing)) parameters.Add(p4);
return parameters;
}
This works well in that in the workbook, I see an #N/A returned until the observer returns a value. The observable then (behind the scenes) has OnCompleted called, which I believe should mean Excel gets told "this no longer needs to be watched", and hopefully the cell is calculated and stays that way.
I've noticed, though, that when an input parameter to ONE of my functions changes, all cells containing MyFunction also recompute even though they might not depend on that cell. Ideally I'd like to retain the standard levels of volatility in Excel - i.e. unless an argument changed, or one of its dependencies changed, the cell is not marked as requiring recalculation. Any idea what I'm doing that would mean this is not what I would get?