Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p><table class="wysiwyg-macro" data-macro-name="excerpt" data-macro-parameters="atlassian-macro-output-type=BLOCK|hidden=true" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2V4Y2VycHQ6aGlkZGVuPXRydWV8YXRsYXNzaWFuLW1hY3JvLW91dHB1dC10eXBlPUJMT0NLfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"><p>support for COM events </p></td></tr></table><br /> <strong>Scriptom</strong> lets you subscribe to COM events on an <strong>ActiveXObject</strong> using the <strong>.events</strong> metaproperty. The event handler is a <strong>Closure</strong>. The arguments in the <strong>Closure</strong> are passed through an instance of <strong>EventArguments</strong>. <br /> In this example, we're subscribing to the <strong>Change</strong> event of an Excel <em>Worksheet</em>. The <strong>Change</strong> event passes one argument - an Excel <em>Range</em>. In the context of this code, we're assuming that the <em>Range</em> object contains just one cell. In the general case, that would be a bad assumption. Once the event handler is defined this way, every change to a cell in the worksheet is going to print a line to standard out with the column, row, and new value.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> worksheet.events.Change = {args -> def range = args[0] println "\tEVENT Change (${range.Column},${range.Row}) = ${range.Value}" } </pre></td></tr></table> <h1>Byref Arguments</h1> <p>As with other COM method types, events support passing values by reference. This is particularly important in light of the fact that events don't return a value. If you want to be able to send data back to whoever raised the event, you have to pass values back to the sender using byref arguments. Fortunately, this is easy to do using the <strong>EventArguments</strong> instance.</p> <p>Take, for example, the following class written in <strong>Visual Basic 2005</strong>. When you call <strong>RaisePassBooleanByref</strong>, it will raise the <strong>OnPassBooleanByref</strong> event, by reference. This gives us an opportunity to change the value of the boolean in the event handler!</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> Option Explicit On Option Strict On Option Compare Binary <Microsoft.VisualBasic.ComClass()> Public Class TestEvents Public Event OnPassBooleanByref(ByRef Value As Boolean) Public Function RaisePassBooleanByref(ByVal Value As Boolean) As Boolean RaiseEvent OnPassBooleanByref(Value) Return Value End Function End Class </pre></td></tr></table> <p>In the following <strong>Groovy</strong> code, note how the return value changes after we define the event handler, which simply inverts the value of the first argument.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> assert activeX.RaisePassBooleanByref(false) == false activeX.events.OnPassBooleanByref = {args -> args[0] = !args[0]} assert activeX.RaisePassBooleanByref(false) == true </pre></td></tr></table> <p>Note that you may only change a value if it has been passed by reference. Otherwise you get an exception.</p> <h1>Event Handlers for Office and Internet Explorer</h1> <p>Back in the first example, I defined and event handler for an Excel <em>Worksheet</em>. There is another piece of information you need to make that example actually work. For many <strong>COM</strong> objects (and all true <strong>ActiveX</strong> objects), the underlying <strong>COM</strong> event handler is defined in a standard way that's easy to find and work with. However, for some <strong>COM</strong> objects, like those in <strong>Microsoft Office</strong> and <strong>Internet Explorer</strong>, there isn't enough information available for <strong>Scriptom</strong> to find the associated event interface. When this happens, you must manually define the prog-id of the object before you define any event handler. To help you out as much as possible, these are available as constants in the <strong>Scriptom</strong> library for the <strong>Office</strong> suite and for <strong>Internet Explorer</strong>. Here is how you define a prog-id for an <strong>ActiveXObject</strong>. <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import org.codehaus.groovy.scriptom.tlb.office.excel.Excel; worksheet.events.useProgId Excel.progIds.Worksheet //Equivalent to worksheet.events.useProgId 'Excel.Sheet' </pre></td></tr></table> <p>Additionally, there are some objects in <strong>Office</strong> and other applications where the event interface exists, but it cannot be discovered, and the object does not have an associated prog-id. The Excel <em>Workbook</em> object is one example of this. We're hoping to address this limitation in a future version of <strong>Scriptom</strong>.</p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced