Repository navigation
Calling Excel API Functions
BERT is an XLL Excel Add-in, which means it can use the Excel C API. This is a powerful, and potentially dangerous, way to interact with Excel. It is generally faster than using COM (like VBA) because it does not have any abstraction.
The file ExcelFunctions.R included in the BERT install directory includes a number of helper functions which call various Excel functions. These should be easier to use than using the Excel API directly. Excel has a lot of historical weirdness which makes it tricky to do things by hand - for example, you cannot set a cell formula using A1 notation, you have to use R1C1 notation. The wrapper functions will take care of all this for you.
To include the Excel helper functions, source the ExcelFunctions.R file in the BERT console or in your startup file:
source( file.path( BERT$HOME, "ExcelFunctions.R" ));See the source file for the details. Here are the basic functions:
- Set.Cell( reference, formula )
Sets a cell's formula. Only a single cell can be set at a time.
Set.Cell( "A2", "=EXP(A1)" )
```
* Get.Range( reference )
Get a cell or range of cells, as a matrix.
```R
mat <- Get.Range( "A2:C4" )
```
#### Internal Details ####
BERT installs a function `Excel`, in the `BERT` environment, which wraps the Excel API callback function. It also installs a wrapper class `xlRefClass` for Excel References. When R code calls the Excel callback function, any Excel references are returned as the **xlRefClass** type. Similarly if you pass this class to the Excel function, BERT will construct Excel References.
To call the Excel API directly, use the function `Excel` in the `BERT` environment. The first parameter is the Excel API function number. The second parameter is a list containing any arguments. For each argument, type conversion between Excel and R is handled as elsewhere in BERT, except that Excel references are translated into the `xlRefClass` type and vice-versa.