Macro Examples


The following examples demonstrate possible uses for macros as well as their syntax.

Macros started by a spreadsheet function

Hiding/unhiding a row

In the example below, the two simple functions hide() and unhide() are shown. The hide() function sets the value in A1 to TRUE, while the unhide() function sets the value in A1 to FALSE.

These macros have been assigned to the buttons “Hide row” and “Unhide row”, as shown in the following screenshot:

The HIDEROW(A1) function in A3 reacts to the value in A1, either hiding or unhiding row 3.

Setting a border


Note: only the following border types are possible: top, bottom, left, right, out, ins_horiz, ins_vert, ins, all. The values are case-sensitive.

Setting a target cell

The function setCell() defines C8 as the target cell. If the target cell contains the value “locked”, then the content is deleted. If nothing is in C8, the value “locked” is written into the cell.

function setCell() {
$range = activesheet()->range('C8');
if ($range->value == 'locked') {
$newVal = '';
} else {
$newVal = 'locked';

Getting a cell value from another worksheet

The function getcell() gets the value from Sheet2!B2 and writes it into cell C10 of the active sheet.

function getcell(){
$value = activeworkbook()->sheets('Sheet2')->Range('B2')->value;
$target = activesheet()->range('C10');

Exporting a workbook to the hard drive

The following code exports the workbook to the server’s hard drive.

function exportfile() {
// save the current workbook as xlsx
 activeworkbook()->export('C:\\dump.xlsx', 'xlsx', 0);
// save the current workbook as full xlsx snapshot
 activeworkbook()->export('C:\\dump_snapshot.xlsx', 'xlsx', 1);
// save the current workbook as wss
 activeworkbook()->export('C:\\dump.wss', 'wss', 0);
// save the current workbook as full wss snapshot
 activeworkbook()->export('C:\\dump_snapshot.wss', 'wss', 1);

savepath The file path for the exported workbook
type The file type of the export (XLSX for Excel 2010+ or WSS for Jedox)

The export mode.

0 = no snapshot
1 = full snapshot
2 = snapshot of OLAP formulas

Note: this code will export the file into a local folder on the server, so it may not be accessible to remote users. For this, you have to export into a folder in the Apache docroot, then dynamically create and display a link to the file. Not all folders on the file system are accessible for the Macro Engine, nor should they be; the open_basedir directive of the Macro Engine may prevent you from exporting to a specific folder.

Checking for “error” type

It is possible to check whether some resource (cell reference, named formula, or variable) currently holds a value of “error” type, such as #Value! or #N/A. For example, the following code is executed when a workbook is opened, checks if some variable currently holds an error value, and then initializes the variable to a fixed value:

function __open() {
$varvalue = retrieve_variable("myyear");
if ( $varvalue instanceof variant_error) {
define_variable("myyear", "2011");

Macros started by a form element

The following macro examples will work only if the macro is started by a form element (combo box, check box, or button). They do not work when they are called as a spreadsheet function.

Displaying a popup message

Show_popup() gets the text of C4 and displays it in an info message box titled “Test”. Warning (“warn”) and error (“err”) messages are also available.

function show_popup() {
$value = ActiveSheet()->Range('C4')->value;
// return command to show popup; arguments: message body, message header, message type
return __msgbox($value, 'Test', 'Info');

Executing a hyperlink

The code below will execute a hyperlink to a specific location:

function Button_Hyperlink()
return __hyperlink('/Demo Spreadsheets/Demos/Bikers Best/Navigation/Bikers Best Navigation', 'test link');

Or, if the Hyperlink is present in some cell, you can simply refer to that cell:

function Button_Hyperlink()
return __hyperlink('A1');

Recalculating a spreadsheet

function Button_Recalc()
return __recalc();

Logging out

function logout_btn() {
return __logout();

Alternate example for invoking a connection to OLAP

In the Macro Engine, Jedox functions can be defined using an already-established OLAP session.

Instead of creating the following connection:

$host = '';
$port = '7777';
$usernem = 'admin';
$password = 'adminpass';
$connection = palo_init($host, $port, $username, $password);

You can establish the connection as follows:

$host = '';
$port = '7777';
$connection = palo_init($host, $port, $_JEDOX['OLAP_SESSION_ID']);

Rather than using a static user/role, this macro uses the actual user who is logged in, including their actual rights.