繁体   English   中英

如何获得Excel更新Excel-DNA RTD电子表格?

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

我使用excel-DNA示例作为此测试的基础。

我希望excel每1秒钟在单元格B1中显示更新的数据。

这在最初的5秒钟内运行良好,然后我得到了一个计时器,必须等待功能完成才能看到最后一个值。

如何在循环的每个循环中强制将更新显示在电子表格上?

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支持一种称为RealTimeData(RTD)的工作表功能,这是您在方案中应使用的功能。

请遵循“ Samples ” GitHub存储库中的示例 特别地,看一下使用Observable Timer的RtdClocks示例

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"));
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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