Microsoft Excel แสดงใหม่แผ่นงานฟังก์ชัน RTD ซึ่งช่วยให้คุณเรียกใช้เซิร์ฟเวอร์ Automation คอมโพเนนต์วัตถุแบบจำลอง (COM) สำหรับวัตถุประสงค์ในการดึงข้อมูลในเวลาจริง บทความนี้อธิบายวิธีการใช้ Visual Basic เพื่อสร้างเซิร์ฟเวอร์ RealTimeData สำหรับใช้กับฟังก์ชัน RTD ของ Excel
ฟังก์ชันของแผ่นงาน RTD มีไวยากรณ์ต่อไปนี้:
= rtd (progID เซิร์ฟเวอร์ String1[String2], ...)
อาร์กิวเมนต์แรก
progidแสดงถึงตัวที่ด้านโปรแกรมระบุ (ProgID) ของเซิร์ฟเวอร์ RealTimeData กระบวนการ
เซิร์ฟเวอร์:อาร์กิวเมนต์ที่บ่งชี้ชื่อของเครื่องที่รันเซิร์ฟเวอร์ RealTimeData อาร์กิวเมนต์นี้สามารถเป็นสตริงที่ว่าง หรือไม่ถ้าเซิร์ฟเวอร์ RealTimeData ให้ เรียกใช้ภายใน อาร์กิวเมนต์ที่เหลืออยู่เพียงแค่แสดงพารามิเตอร์ที่ส่งไปยังเซิร์ฟเวอร์ RealTimeData แต่ละชุดที่ไม่ซ้ำกันของพารามิเตอร์เหล่านี้แทนหัวหนึ่ง "ข้อ หมายเลขหัวข้อ"ซึ่งมีการเชื่อมโยง" พารามิเตอร์มี case-sensitive แสดงตัวอย่างเช่น ต่อไปนี้แสดงการเรียกไปยังเซิร์ฟเวอร์ RTD ที่จะทำให้รหัสของหัวข้อที่แยกต่างหากสาม:
= RTD("ExcelRTD.RTDFunctions",,"AAA", "10")
= RTD("ExcelRTD.RTDFunctions",,"AAA", "5")
= RTD("ExcelRTD.RTDFunctions",,"aaa", "5")
ในใบสั่งสำหรับเซิร์ฟเวอร์ Automation COM ที่เป็น เซิร์ฟเวอร์ RealTimeData สำหรับใช้กับฟังก์ชัน RTD ของ Excel จะต้องปฏิบัติการ
IRTDServerอินเทอร์เฟซ เซิร์ฟเวอร์ที่ต้องปฏิบัติตามวิธีการทั้งหมด
IRTDServer:
ServerStartเรียกเมื่อ Excel หัวข้อ RTD แรกสำหรับเซิร์ฟเวอร์ที่ร้องขอServerStartควรกลับเป็น 1 สำเร็จ และค่าลบ หรือ 0 บนล้มเหลว พารามิเตอร์แรกของการServerStartวิธีใช้เป็นวัตถุการเรียกกลับที่เซิร์ฟเวอร์ RealTimeData ใช้เพื่อแจ้งให้ทราบของ Excel เมื่อคุณควรรวบรวมการปรับปรุงจาก RealTimeData เซิร์ฟเวอร์
ServerTerminateเรียกเมื่อ Excel RTD หัวข้อจากเซิร์ฟเวอร์ RealTimeData ที่จำเป็นต้องมีอีกต่อไป
ConnectDataเรียกว่าเมื่อใดก็ ตาม Excel ร้องขอหัวข้อ RTD ใหม่จากเซิร์ฟเวอร์ RealTimeData
DisconnectDataเรียกว่าเมื่อใดก็ ตาม Excel หัวข้อเฉพาะที่จำเป็นต้องมีอีกต่อไป
heartBeatเรียก โดย Excel ถ้าช่วงเวลาที่กำหนดได้ล่วงเลยไปนับตั้งแต่ครั้งสุดท้ายที่ Excel ได้รับการแจ้งเตือนของการปรับปรุงจากเซิร์ฟเวอร์ RealTimeData
RefreshDataเรียกเมื่อ Excel กำลังร้องขอการฟื้นฟูการแสดงผลบนหัวข้อRefreshDataถูกเรียกหลังจากที่เซิร์ฟเวอร์แจ้งให้ทราบ Excel ที่มีการปรับปรุง และจะส่งกลับจำนวนของหัวข้อการปรับปรุง ด้วยหมายเลขของหัวข้อและค่าสำหรับแต่ละหัวข้อ
สร้างเซิร์ฟเวอร์ RealTimeData ตัวอย่าง
ตัวอย่างต่อไปนี้อธิบายวิธีการสร้าง และใช้เซิร์ฟเวอร์ RealTimeData กับ Microsoft Excel 2002 เซิร์ฟเวอร์นี้จะแสดงตัวนับที่มีการปรับปรุงทุก ๆ 10 วินาทีบนแผ่นงาน เซิร์ฟเวอร์ที่ยอมรับสายอักขระของหัวข้อคอลัมน์สอง สายอักขระแรกของหัวข้อสามารถ AAA, BBB และ CCC สตริงหัวข้ออื่นใด ๆ จะถือเป็นไม่ถูกต้อง และเซิร์ฟเวอร์ส่งกลับ #VALUE เมื่อต้องการฟังก์ชัน RTD สายอักขระที่สองมีค่าตัวเลขที่แสดงถึงวิธี incremented ค่าส่งคืน ถ้ามีเว้นสายอักขระที่สอง เพิ่มค่าเริ่มต้นที่ 1 ถ้าสายอักขระที่สองไม่ใช่ตัวเลข เซิร์ฟเวอร์ส่งกลับ #NUM เมื่อต้องการฟังก์ชัน RTD
- เริ่มต้นโครงการ ActiveX DLL ใหม่ใน Visual Basic
- ในการProjectเมนู คลิกอ้างอิง:เลือกไลบรารีวัตถุสำหรับรุ่นของ Excel และจากนั้น คลิกตกลง. ตัวอย่างเช่น เลือกหนึ่งต่อไปนี้:
- สำหรับ Microsoft Office Excel 2007 เลือกการไลบรารีวัตถุ Microsoft Excel 12.0.
- สำหรับ Microsoft Office Excel 2003 เลือกการไลบรารีวัตถุ Microsoft Excel 11.0.
- สำหรับ Microsoft Excel 2002 เลือกการไลบรารีวัตถุ Microsoft Excel 10.0.
- ในการProjectเมนู คลิกคุณสมบัติ Project1. การเปลี่ยนแปลงนั้นชื่อของโครงการเมื่อต้องการExcelRTDแล้ว คลิกตกลง.
- การเปลี่ยนแปลงนั้นชื่อ:คุณสมบัติของโมดูลคลาClass1เมื่อต้องการRTDFunctions. เพิ่มรหัสต่อไปนี้เพื่อRTDFunctions:
Option Explicit
Implements IRtdServer 'Interface allows Excel to contact this RealTimeData server
Private m_colTopics As Collection
Private Function IRtdServer_ConnectData(ByVal TopicID As Long, Strings() As Variant, GetNewValues As Boolean) As Variant
'** ConnectData is called whenever a new RTD topic is requested
'Create a new topic class with the given TopicId and string and add it to the
'm_colTopics collection
Dim oTopic As New Topic
m_colTopics.Add oTopic, CStr(TopicID)
oTopic.TopicID = TopicID
oTopic.TopicString = Strings(0)
If UBound(Strings) >= 1 Then oTopic.SetIncrement Strings(1)
'For this example, the initial value for a new topic is always 0
IRtdServer_ConnectData = oTopic.TopicValue
Debug.Print "ConnectData", TopicID
End Function
Private Sub IRtdServer_DisconnectData(ByVal TopicID As Long)
'** DisconnectData is called whenever a specific topic is not longer needed
'Remove the topic from the collection
m_colTopics.Remove CStr(TopicID)
Debug.Print "DisconnectData", TopicID
End Sub
Private Function IRtdServer_Heartbeat() As Long
'** Called by Excel if the heartbeat interval has elapsed since the last time
' Excel was called with UpdateNotify.
Debug.Print "HeartBeat"
End Function
Private Function IRtdServer_RefreshData(TopicCount As Long) As Variant()
'** Called when Excel is requesting a refresh on topics. RefreshData will be called
' after an UpdateNotify has been issued by the server. This event should:
' - supply a value for TopicCount (number of topics to update)
' - return a two dimensional variant array containing the topic ids and the
' new values of each.
Dim oTopic As Topic, n As Integer
ReDim aUpdates(0 To 1, 0 To m_colTopics.Count - 1) As Variant
For Each oTopic In m_colTopics
oTopic.Update
aUpdates(0, n) = oTopic.TopicID
aUpdates(1, n) = oTopic.TopicValue
n = n + 1
Next
TopicCount = m_colTopics.Count
IRtdServer_RefreshData = aUpdates
Debug.Print "RefreshData", TopicCount & " topics updated"
End Function
Private Function IRtdServer_ServerStart(ByVal CallbackObject As Excel.IRTDUpdateEvent) As Long
'** ServerStart is called when the first RTD topic is requested
Set oCallBack = CallbackObject
Set m_colTopics = New Collection
g_TimerID = SetTimer(0, 0, TIMER_INTERVAL, AddressOf TimerCallback)
If g_TimerID > 0 Then IRtdServer_ServerStart = 1 'Any value <1 indicates failure.
Debug.Print "ServerStart"
End Function
Private Sub IRtdServer_ServerTerminate()
'** ServerTerminate is called when no more topics are needed by Excel.
KillTimer 0, g_TimerID
'** Cleanup any remaining topics. This is done here since
' IRtdServer_DisconnectData is only called if a topic is disconnected
' while the book is open. Items left in the collection when we terminate
' are those topics left running when the workbook was closed.
Dim oTopic As Topic
For Each oTopic In m_colTopics
m_colTopics.Remove CStr(oTopic.TopicID)
Set oTopic = Nothing
Next
Debug.Print "ServerTerminate"
End Sub
- ในการProjectเมนู คลิกเพิ่มโมดูลคลาส. เปลี่ยนโมดูลคลาชื่อ:คุณสมบัติการหัวข้อและมีการเปลี่ยนแปลงนั้นinstancingคุณสมบัติการส่วนตัว. เพิ่มรหัสต่อไปนี้ไปหัวข้อโมดูลคลาส:
Option Explicit
Private m_TopicID As Long
Private m_TopicString As String
Private m_Value As Variant
Private m_IncrementVal As Long
Private Sub Class_Initialize()
m_Value = 0
m_IncrementVal = 1
End Sub
Friend Property Let TopicID(ID As Long)
m_TopicID = ID
End Property
Friend Property Get TopicID() As Long
TopicID = m_TopicID
End Property
Friend Property Let TopicString(s As String)
s = UCase(s)
If s = "AAA" Or s = "BBB" Or s = "CCC" Then
m_TopicString = s
Else
m_Value = CVErr(xlErrValue) 'Return #VALUE if not one of the listed topics
End If
End Property
Friend Sub Update()
On Error Resume Next 'the next operation will fail if m_Value is an error (like #NUM or #VALUE)
m_Value = m_Value + m_IncrementVal
End Sub
Friend Sub SetIncrement(v As Variant)
On Error Resume Next
m_IncrementVal = CLng(v)
If Err <> 0 Then
m_Value = CVErr(xlErrNum) 'Return #NUM if Increment value is not numeric
End If
End Sub
Friend Property Get TopicValue() As Variant
If Not (IsError(m_Value)) Then
TopicValue = m_TopicString & ": " & m_Value
Else
TopicValue = m_Value
End If
End Property
- ในการProjectเมนู การเลือกเพิ่มโมดูล. เพิ่มรหัสต่อไปนี้ในโมดูลใหม่:
Public Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, _
ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
Public Const TIMER_INTERVAL = 5000
Public oCallBack As Excel.IRTDUpdateEvent
Public g_TimerID As Long
Public Sub TimerCallback(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
oCallBack.UpdateNotify
End Sub
- ในการแฟ้ม:เมนู คลิกตรวจสอบ ExcelRTD.dllเมื่อต้องสร้างส่วนประกอบ
ใช้เซิร์ฟเวอร์ RTD ใน Excel
- เริ่มต้นสมุดงานใหม่ใน Microsoft Excel
- ในเซลล์ A1 ป้อนสูตรต่อไปนี้ และจากนั้น กดแป้น ENTER:
= RTD("ExcelRTD.RTDFunctions",,"AAA", 5)
คืนค่าเริ่มต้นเป็น " AAA: 0 " หลังจากห้าวินาที ค่าอัพเดการ " AAA: 10 " และหลังจาก 10 วินาที ค่าอัพเด "AAA:15" และอื่น ๆ
- ในเซลล์ A2 ป้อนสูตรต่อไปนี้ แล้วกด ENTER:
= RTD("ExcelRTD.RTDFunctions",,"BBB", 3)
คืนค่าเริ่มต้นเป็น " BBB: 0 " วินาทีที่ห้าทุกค่าในเซลล์ที่เพิ่ม โดย 3
- ในเซลล์ A3 ป้อนสูตรต่อไปนี้ แล้วกด ENTER:
= RTD("ExcelRTD.RTDFunctions",,"AAA", 5)
คืนค่าเริ่มต้นตรงกับเนื้อหาของเซลล์ A1 เนื่องจากเป็นหัวเดียว "ข้อ" ที่ใช้ใน A1
- ในเซลล์ A4 ป้อนสูตรต่อไปนี้ และกด Enter:
= RTD("ExcelRTD.RTDFunctions",,"AAA", 10)
คืนค่าเริ่มต้นเป็น " AAA: 0 " ทุก ๆ ห้าวินาทีเพิ่มค่าของเซลล์กับเซลล์อื่น ๆ ในการทำงาน โปรดสังเกตว่า ค่าที่ส่งคืนไม่ตรงกับเนื้อหาของเซลล์ A1 หรือ A3 ได้เนื่องจากชุดของพารามิเตอร์ที่ส่งผ่านไปยังเซิร์ฟเวอร์อื่น
สำหรับภาพประกอบนี้ เซิร์ฟเวอร์ RTD คอมไพล์ และทำการ Excel ถูกใช้เป็นเวลาเรียกใช้รุ่นของคอมโพเนนต์ For debugging purposes, RTD
servers can be run from the Visual Basic IDE.
To run in debug mode:
- Quit Microsoft Excel and switch to the project in Visual
Basic.
- Press F5 to start the component. ถ้าการคุณสมบัติของโครงการกล่องโต้ตอบปรากฏขึ้น คลิกตกลงto select the default option ofWait for components to be created.
- Make sure that the Immediate window in Visual Basic is
displayed. As you enter formulas in the cells and as the cells are updated,
examine the contents of the Immediate window in Visual Basic to see which
actions are triggering the different events.
Note Regarding the DisconnectData EventWhile Excel is a subscriber to your RTD server, it
triggers the
DisconnectDataevent when it no longer needs a topic (for example, when you
delete or clear an RTD formula in a cell). However, Excel does not call
DisconnectDataon each topic for the RTD server when the workbook is closed or
Excel quits; instead, Excel calls only
ServerTerminate. When you are creating an RTD server, you should code for any
necessary clean-up of topics or other objects when the
ServerTerminateevent fires.
(c) Microsoft Corporation 2001, All Rights
Reserved. contributions โดย Lori B. Turner, Microsoft Corporation
สำหรับข้อมูลเพิ่มเติม ให้คลิกหมายเลขบทความต่อไปนี้ เพื่อดูบทความในฐานความรู้ของ Microsoft::
284883
(http://support.microsoft.com/kb/284883/
)
RTD Server Does Not Send Update Notifications to Multiple Excel Instances
หมายเลขบทความ (Article ID): 285339 - รีวิวครั้งสุดท้าย: 8 มกราคม 2554 - Revision: 2.0
ใช้กับ
- Microsoft Office Excel 2007
- Microsoft Excel 2002 Standard Edition
- Microsoft Visual Basic 6.0 Professional Edition
| kbexpertisebeginner kbautomation kbhowto kbmt KB285339 KbMtth |
แปลโดยคอมพิวเตอร์ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ
ต่อไปนี้เป็นฉบับภาษาอังกฤษของบทความนี้:
285339
(http://support.microsoft.com/kb/285339/en-us/
)