簡體   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