Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

testMSOfficeEvents.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # OfficeEvents - test/demonstrate events with Word and Excel.
  2. import msvcrt
  3. import sys
  4. import threading
  5. import time
  6. import types
  7. import pythoncom
  8. from win32com.client import Dispatch, DispatchWithEvents
  9. stopEvent = threading.Event()
  10. def TestExcel():
  11. class ExcelEvents:
  12. def OnNewWorkbook(self, wb):
  13. if type(wb) != types.InstanceType:
  14. raise RuntimeError(
  15. "The transformer doesnt appear to have translated this for us!"
  16. )
  17. self.seen_events["OnNewWorkbook"] = None
  18. def OnWindowActivate(self, wb, wn):
  19. if type(wb) != types.InstanceType or type(wn) != types.InstanceType:
  20. raise RuntimeError(
  21. "The transformer doesnt appear to have translated this for us!"
  22. )
  23. self.seen_events["OnWindowActivate"] = None
  24. def OnWindowDeactivate(self, wb, wn):
  25. self.seen_events["OnWindowDeactivate"] = None
  26. def OnSheetDeactivate(self, sh):
  27. self.seen_events["OnSheetDeactivate"] = None
  28. def OnSheetBeforeDoubleClick(self, Sh, Target, Cancel):
  29. if Target.Column % 2 == 0:
  30. print("You can double-click there...")
  31. else:
  32. print("You can not double-click there...")
  33. # This function is a void, so the result ends up in
  34. # the only ByRef - Cancel.
  35. return 1
  36. class WorkbookEvents:
  37. def OnActivate(self):
  38. print("workbook OnActivate")
  39. def OnBeforeRightClick(self, Target, Cancel):
  40. print("It's a Worksheet Event")
  41. e = DispatchWithEvents("Excel.Application", ExcelEvents)
  42. e.seen_events = {}
  43. e.Visible = 1
  44. book = e.Workbooks.Add()
  45. book = DispatchWithEvents(book, WorkbookEvents)
  46. print("Have book", book)
  47. # sheet = e.Worksheets(1)
  48. # sheet = DispatchWithEvents(sheet, WorksheetEvents)
  49. print("Double-click in a few of the Excel cells...")
  50. print("Press any key when finished with Excel, or wait 10 seconds...")
  51. if not _WaitForFinish(e, 10):
  52. e.Quit()
  53. if not _CheckSeenEvents(e, ["OnNewWorkbook", "OnWindowActivate"]):
  54. sys.exit(1)
  55. def TestWord():
  56. class WordEvents:
  57. def OnDocumentChange(self):
  58. self.seen_events["OnDocumentChange"] = None
  59. def OnWindowActivate(self, doc, wn):
  60. self.seen_events["OnWindowActivate"] = None
  61. def OnQuit(self):
  62. self.seen_events["OnQuit"] = None
  63. stopEvent.set()
  64. w = DispatchWithEvents("Word.Application", WordEvents)
  65. w.seen_events = {}
  66. w.Visible = 1
  67. w.Documents.Add()
  68. print("Press any key when finished with Word, or wait 10 seconds...")
  69. if not _WaitForFinish(w, 10):
  70. w.Quit()
  71. if not _CheckSeenEvents(w, ["OnDocumentChange", "OnWindowActivate"]):
  72. sys.exit(1)
  73. def _WaitForFinish(ob, timeout):
  74. end = time.time() + timeout
  75. while 1:
  76. if msvcrt.kbhit():
  77. msvcrt.getch()
  78. break
  79. pythoncom.PumpWaitingMessages()
  80. stopEvent.wait(0.2)
  81. if stopEvent.isSet():
  82. stopEvent.clear()
  83. break
  84. try:
  85. if not ob.Visible:
  86. # Gone invisible - we need to pretend we timed
  87. # out, so the app is quit.
  88. return 0
  89. except pythoncom.com_error:
  90. # Excel is busy (eg, editing the cell) - ignore
  91. pass
  92. if time.time() > end:
  93. return 0
  94. return 1
  95. def _CheckSeenEvents(o, events):
  96. rc = 1
  97. for e in events:
  98. if e not in o.seen_events:
  99. print("ERROR: Expected event did not trigger", e)
  100. rc = 0
  101. return rc
  102. def test():
  103. import sys
  104. if "noword" not in sys.argv[1:]:
  105. TestWord()
  106. if "noexcel" not in sys.argv[1:]:
  107. TestExcel()
  108. print("Word and Excel event tests passed.")
  109. if __name__ == "__main__":
  110. test()