Related articles:
On Excel Development (1) Overview of Excel Development
On Excel Development (II.) Excel menu system
On the development of Excel (III.) Excel object model
The UDF function in Excel is described above, and this article describes the same important RTD function. Starting with Excel 2002, Excel introduces a new mechanism for viewing and updating real-time data, the real-time data abbreviation RTD function, which is a push-pull way, and when data needs to be updated, the RTD gives Excel Push a message says to update the data, Excel takes the initiative to pull pull new data after receiving the message. The first purpose of the RTD function is to update real-time data, such as real-time stock quotes, real-time weather forecasts, team scoring data, and so on.
In the past, to implement these functions, you need to rely on some other such as Dynamic Data Exchange (DDE) technology to access real-time data resources, but DDE and standard Excel function styles are very different and are not designed for Excel to get real-time data, lack of robustness, And the efficiency is not high, the introduction of RTD solves these problems.
This article first introduces some common usage scenarios of RTD, the basic structure of the RTD function, notes, and finally shows how to get real-time quotes from the Google Financial API through the RTD function.
One use scene
The RTD function is useful, and you should consider using the RTD function if you experience the following:
Real-time Data update: This is a very common situation encountered, such as real-time quotes, real-time weather conditions, live game scoring situation.
Some special scenarios, such as securities trading companies trading systems, some models of these companies based on real-time market or index calculation, through the RTD to obtain the basic data real-time data, on this basis can be dynamic modeling and analysis.
Asynchronous, time-consuming data requests: One problem with traditional UDF functions is that he is synchronous, that is, if a function is executed too long, it can lead to the front interface of the cotton, especially in the UDF function to access the database, WebService, or complex computational processes such as uncertainty and time consuming execution environment. Based on RTD technology, asynchronous UDF functions can be implemented, resulting in a better user experience.
Two basic structures
The Excel RTD function is a COM component that implements the IRtdServer interface that Excel interacts with live data through the COM component. To implement the RTD function, you must implement the IRtdServer interface, which is located in the Microsoft.Office.Interop.Excel namespace and jumps to the definition to see the internal of the interface:
<summary>/// represents a interface for a real-time data server.///</summary>[guid ("ec0e6191-db 51-11d3-8f3e-00c04f3651b8 ")] [TypeLibType (4160)] public interface irtdserver2{///<summary> /// Adds new topics from a real-time data server. The ConnectData is called /// Then a file is opened that contains real-time data funct Ions or when a user /// types in a new formula which the RTD function. ///</summary> ///<param name= "TopicID" > ///Required Integer. A unique value, assigned by Microsoft Excel, which identifies the topic.</param> ///<param name= "
Strings ">required Object. A single-dimensional Array of strings identifying topic.</param> ///<param name= "getnewvalues "> Required Boolean. True to determine if new values are to is ACQUIRED.</PARAM> ///<returns></returns> [dispid (one)]dynamic ConnectData (int topicid, ref Array St Rings, ref bool getnewvalues);///<summary> ///notifies a real-time data (RTD) server application That's a topic is no longer in use. ///</summary> ///<param name= "TopicID" > Required Integer. A unique value assigned to the topic assigned by Microsoft excel.</param> [dispid (]void)
Ctdata (int topicid);
[DispId ()]int Heartbeat (); <summary> ///This (is) called by Microsoft Excel to get new data. ///</summary> ///<param name= "TopicCount" >topiccount: /// &N Bsp
Required Integer. The RTD server must change the value of the TopicCount /// to the number of elements in the A Rray returned.</param> ///<returns></returns> [dispid (a)]array refreshdata (ref int topiccount); <summary> ///The ServerStart method are called immediately after a real-time data server & nbsp;/// is instantiated. Negative value or zero indicates failure to start the server; /// Positive value indicates success. ///</summary> ///<param name= "Callbackobject" >required Microsoft.Office.Interop.Excel.IRTDUpdateEvent object. The callback object.</param> ///<returns></returns> [dispid (a)]int
ServerStart (IRTDUpdateEvent callbackobject); <summary> ///Terminates the connection to the real-time data server.
///</summary> [dispid ()]void serverterminate (); }