Macro Examples

The following examples demonstrate possible uses for macros as well as their syntax. Macro Engine API documentation is available online and as a zip download.

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.

Macro editor screenshot

These macros have been assigned to the buttons "Hide Row" and "Unhide Row", as shown in the following screenshot:

Macro buttons table

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

Setting a border

Macro editor screenshot

or

Macro editor screenshot

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.

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

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.

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

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:

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

Wrapper function for sending an e-mail.

The function will use the e-mail server configured in the "Tasks" settings group in the Jedox Web Settings panel.

mixed mailer_send_mail(mixed to, string subject, string body, string from, mixed cc, mixed bcc, string attachment)

Parameter Input Description
$to string/array A single e-mail address as string, or an array of e-mail addresses, defining the recipient(s)
$subject string The e-mail subject.
$body string A string representig plaintext, or HTML body of the e-mail message
$from string Optional; a string specifying a single e-mail address of the sender, different from the account used for sending the e-mail. Note that usage of $from parameter requires the SMTP server to accept such e-mails ("spoofing").
Additionally, it has to be enabled in the Settings in Jedox Web, by adding a new key of type "boolean", with the name tasks.smtp.allow_spoofing
$cc string/array Optional; a single e-mail address as string, or an array of e-mail addresses, defining the CC recipient(s)
$bcc string/array Optional; a single e-mail address as string, or an array of e-mail addresses, defining the BCC recipient(s)
$attachment string Optional; a path to a single file, to be sent as attachment. The file must be readable on the file system for Jedox Spreadsheet Server. Use a forward slash as path separator.

To use the function, you have to require the mailer library in your macro; see example below. Note that the mailer does not support unencrypted sending of mails. Depending on the starttls_enable setting value, it will send e-mails either using TLS, or SSL.

Copy
//load the integrator macro library
require library('mailer');

function mailer_example()
{
  //define several parameters  
  $to = 'recipient@example.com';
  $subject = 'This is a test';
  $body = 'I hope this test e-mail finds you well. There is no need to reply, as this is really just a test.';
  
  //send the e-mail
  $mailer_result = mailer_send_mail($to, $subject, $body);
  
  //show the result in a popup dialog
  return __msgbox('Number of e-mails sent: '.$mailer_result);

Macros started by a form element

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

Displaying a pop-up message

Show_popup() gets the input of C4 to validate it and provides the validation in an info message box titled "Test". Warning (warn) and error (err) messages are also available.

Copy
function show_popup() {
  // receive value
  $value = ActiveSheet()->;range('C4')->;value;
  // validate value
  if ($value > 0 and $value < 100) {
    $msg = "Value meets requirements";
  } else {
    $msg = "Value doesn't meet requirements";
  }
  // return command to show popup; arguments: message body, message header, message type
  return __msgbox($msg, 'Test', 'Info');
}

Executing a hyperlink

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

Copy
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:

Copy
function Button_Hyperlink() 
{
return __hyperlink('A1'); 
}

Recalculating a spreadsheet

Copy
function Button_Recalc() { return __recalc(); }

Logging out

Copy
function logout_btn() { 
return __logout();
}

Using System variables to connect to OLAP

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

Using the dynamic values for the In-Memory DB host and port allows for portability of the code between various Jedox environments:

Copy
$host = $_JEDOX['OLAP_HOST'];
$port = $_JEDOX['OLAP_PORT'];
$connection = palo_init($host, $port, $_JEDOX['OLAP_SESSION_ID']);

This macro uses the actual user who is logged in, including their actual rights.

Closing the connection to OLAP

At the end of your session, close the connection to OLAP. If the connection is not closed, unwanted side effects like performance issues might occur. To close the connection, use this code:

Copy
palo_disconnect($connection);

Updated November 4, 2024