XLL+ Class Library (7.0)

CalcEvt Sample

Demonstrates how to trap the OnCalculate event, and add your own data to a worksheet

Overview

This sample add-in shows how to trap the OnCalculate event, which occurs just after a sheet has finished calculating.

It also demonstrates the use of CXlOper to manipulate cell references.

Features

The sample displays the following features:

Notes

The processing at the end of the function CTextFileTask::PopulateDestination is very important.

CopyC++
// Get the current contents of the destination range
xloOldValue.Coerce(m_xloDest);

// Compare the current value with the contents of the file 
// and update the destination range ONLY if they are different. 
if (xloOldValue != xloValue)
    m_xloDest.SetValue(xloValue);

We look at the current contents of the destination range, and compare them to the values we intend to put there. If they are identical then we do not update the range.

Every time we update a cell, we can cause the sheet to be calculated. Since this method is called during OnCalculate, we might go into an endless loop, which is to be avoided.

By making sure that we only update the cells when necessary, we only cause a finite number of cascading calculations.

Classes and functions used

CXlCalculateEventStaticObserver | CXlOper::ChangeType | CXlOper::GetCaller | CXlOper::IsRef | CXlOper::GetSheetName | CXlOper::AllocArray | CXlOper::Cell | CXlOper::Coerce | CXlOper::SetValue | CXlOper::RetError | CXlOper::Ret | CXlMenu::CheckItem | ::XllGetTypedApp

Sample project

Each sample project is located in a sub-directory of the Samples directory of the XLL+ installation. To use the sample project, open the solution file CalcEvt.sln or the project file CalcEvt.vcproj.

You can enable debugging under Excel by using the Setup Debugging command in the XLL+ ToolWindow.

When delivered, the help files are excluded from the build. You can enable the help build by selecting the files CalcEvt.help.xml and CalcEvt.chm in the Solution Explorer, and using the right-click menu to view Properties. Select the page "Configuration Properties/General" and set the "Excluded from build" property to "No". See Generating help in the User Guide for more information.

See Also

List of Sample Projects