12.07.2015 Views

Excel Add-in Development in C/C++: Applications in ... - F9

Excel Add-in Development in C/C++: Applications in ... - F9

Excel Add-in Development in C/C++: Applications in ... - F9

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

26 <strong>Excel</strong> <strong>Add</strong>-<strong>in</strong> <strong>Development</strong> <strong>in</strong> C/<strong>C++</strong>2.11.2 Trigger<strong>in</strong>g functions to be called by <strong>Excel</strong> – the trigger argumentThere are times when you want th<strong>in</strong>gs to be calculated <strong>in</strong> a very specific order, or forsometh<strong>in</strong>g to be triggered by the change <strong>in</strong> value of some cell or other. Of course, <strong>Excel</strong>does this automatically, you might say. True, but the trigger is the change <strong>in</strong> value ofsome <strong>in</strong>put to the calculation. This is f<strong>in</strong>e as long as you only want that to be the trigger.What if you want someth<strong>in</strong>g else to be the trigger? What if the function you want totrigger doesn’t need any arguments? For example, what if you want to have a cell thatshows the time that another cell’s value last changed so that an observer can see howfresh the <strong>in</strong>formation is?The solution is simple: the trigger argument. This is a dummy argument that is ofabsolutely no use to the function be<strong>in</strong>g triggered other than to force <strong>Excel</strong> to call it.(Section 9.1 Tim<strong>in</strong>g function execution <strong>in</strong> VB and C/<strong>C++</strong> on page 285 relies heavily onthis idea.) The VB function NumCalls_1() <strong>in</strong> the above section uses the argument solelyto trigger <strong>Excel</strong> to call the code.In the case of want<strong>in</strong>g to record the time a static numeric cell’s value changes, a simpleVB function like this would have the desired effect:Function Get_Time(trigger As Double) As DoubleGet_Time = NowEnd FunctionThe argument trigger is not used <strong>in</strong> the calculation which simply returns the currentdate and time as the number of days from 1st January 1900 <strong>in</strong>clusive by call<strong>in</strong>g VB’sNow function. It just ensures the calculation is done whenever the static trigger changesvalue (or when <strong>Excel</strong> decides it needs to do a brute-force recalculation of everyth<strong>in</strong>g onthe sheet). 3The concept of a trigger argument can, of course, usefully be applied to C/<strong>C++</strong> add-<strong>in</strong>functions too, and is extensively used <strong>in</strong> later sections of this book.2.11.3 Volatile functions<strong>Excel</strong> supports the concept of a volatile function, one whose value cannot be assumed tobe the same from one moment to the next even if none of its arguments (if it takes any) haschanged. <strong>Excel</strong> re-evaluates cells conta<strong>in</strong><strong>in</strong>g volatile functions, along with all dependents,every time it recalculates, usually any time anyth<strong>in</strong>g <strong>in</strong> the workbook changes, or whenthe user presses {<strong>F9</strong>}.It is easy to create user-def<strong>in</strong>ed functions that are optionally volatile (see the VB macroNumCalls_1() <strong>in</strong> the above section), by us<strong>in</strong>g a built-<strong>in</strong> volatile function as a triggerargument. <strong>Add</strong>itionally, VB and the C API both support ways to tell <strong>Excel</strong> that an add-<strong>in</strong>function should be treated as volatile. With VB, <strong>Excel</strong> only learns this when it first callsthe function. With the C API a function can be registered as volatile before its first call.Among the standard worksheet functions, there are five volatile functions:• NOW();• TODAY();3 If the trigger were itself the result of a formula, this function might be called even when the value of the triggerhad not changed. See section 2.11.5 User-def<strong>in</strong>ed functions (VB Macros) and add-<strong>in</strong> functions on page 25.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!