简体   繁体   中英

How can I get excel to update spreadsheet for Excel-DNA RTD?

I am using excel-DNA example as basis for this test.

I want excel to show my updated data in cell B1 every 1 second.

This works fine for about the first 5 secs, then I get a timer and the have to wait for function finish to see only the last value.

How do I force the update to be shown on the spreadsheet at each cycle of the loop?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExcelDna.Integration;
using System.Threading.Tasks;
using System.Diagnostics;    

namespace BTPRTD
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }

    public static class MyFunctions
    {
        public static MouseData MData;

        [ExcelFunction(Description = "My first .NET function")]
        public static string SayHello(string name)
        {
            //Debugger.Launch();
            MData = new MouseData ();

            Task.Factory.StartNew(() =>
            {            
                ExcelAsyncUtil.QueueAsMacro(() =>
                {
                    KeepWritingData();
                });
            }); 
            return "Hello " + name;  
        }

        public static void KeepWritingData()
        {
            var refB1 = new ExcelReference(0, 0, 1, 1, "Sheet1");
            string dataTxt = "";
            for (int i = 0; i < 100; i++)
            {
                try
                {
                    MouseData .CurrentPriceData CPD = MData.GetCurrentPriceNT("White mouse");
                    dataTxt = CPD.AsString();
                }
                catch (Exception)
                {
                    dataTxt = "Data Unavailable";
                }

                refB1.SetValue("Ding: " + i + " " + dataTxt);
                System.Threading.Thread.Sleep(1000);
            }
        }
    }
}

Excel supports a type of worksheet function called RealTimeData (RTD), which is what you should use for your scenario.

Follow the examples in the " Samples " GitHub repository. In special, take a look at the RtdClocks example that uses an Observable Timer:

public static class RtdClock
{
    [ExcelFunction(Description = "Provides a ticking clock")]
    public static object dnaRtdClock_Rx()
    {
        string functionName = "dnaRtdClock_Rx";
        object paramInfo = null; // could be one parameter passed in directly, or an object array of all the parameters: new object[] {param1, param2}
        return ObservableRtdUtil.Observe(functionName, paramInfo, () => GetObservableClock() );
    }

    static IObservable<string> GetObservableClock()
    {
        return Observable.Timer(dueTime: TimeSpan.Zero, period: TimeSpan.FromSeconds(1))
                         .Select(_ => DateTime.Now.ToString("HH:mm:ss"));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM