Export scripts

In this article

Export scripts are used to export data in a custom format. This functionality is available only in Banana Premium or Banana Experimental.

With the dialog "Manage Scripts" from the "Account1" menu, you can install, modify or remove scripts. The script can be located everywhere, usually they are in the same folder as the accounting file.

 

Example to export the cash accounts in a xml file.

// @id = ch.banana.filter.export.accounts
// @version = 1.1
// @publisher = Banana.ch SA
// @description = Export cash accounts
// @task = export.file
// @exportfiletype = xml
// @inputdatasource = none

/**
 * This example export the cash accounts to a xml file
 * Example of result:
 * <accounts>
 *    <account>
 *      <accountnr>1000</accountnr>
 *      <description>Cash on hand</description>
 *      <balance>557.00</balance>
 *    </account>
 *    ...
 * </accounts>
 */
function exec( inData) {
    var exportResult = '<accounts>';
    var tableAccounts = Banana.document.table('Accounts');
    if ( tableAccounts) {
        var cashAccountRows = tableAccounts.findRows( isCashAccount);    
        for (i=0;i<cashAccountRows.length;i++) {
            exportResult += '<account>';
            exportResult += '<accountnr>' + cashAccountRows[i].value('Account') + '</accountnr>';
            exportResult += '<description>' + cashAccountRows[i].value('Description') + '</description>';
            exportResult += '<balance>' + cashAccountRows[i].value('Balance') + '</balance>';
            exportResult += '</account>';
        }
    }
    exportResult += '</accounts>';
    return exportResult;
}

/** Define function String.startsWith */
if (typeof String.prototype.startsWith != 'function') {
  String.prototype.startsWith = function (str){
    return this.slice(0, str.length) == str;
  };
}

/** Return true if the account is a cash account. */
function isCashAccount( row, i, table) {
    var account = row.value('Account');
    if (typeof account === 'undefined' || account === '')
        return false;
    if (account.startsWith('10'))
        return true;
    return false;
}

 

Example to export the addresses in a printable fashion as an address book.

// This script export the addresses in a printable fashion as an address book

// @id = ch.banana.script.addressbook
// @version = 1.0
// @pubdate = 2013-11-21
// @publisher = Banana.ch SA
// @description = Export address book
// @task = app.command
// @outputformat = none
// @inputdatasource = none
// @timeout = -1

function exec( string) {

    var html = '';
    html += '<html>';
    html += '<head>';
    html += '<title>' + 'Indirizzi' + '</title>';
    html += '<head>';
    html += '</head>';
    html += '<body>';
    
    var addresses = Banana.document.table('Addresses').rows;
    addresses = addresses.filter(isAddressNotEmpty);
    addresses = addresses.sort(sortAddresses);

    var sectionStart = 0;
    var sectionEnd = 0;
    while (sectionEnd < addresses.length) {
        sectionEnd = findAddressSectionEnd(addresses, sectionStart);    
        var title = getAddressFirstLetter(addresses[sectionStart]);
        html += renderAddressSection(title, addresses, sectionStart, sectionEnd);
        sectionStart = sectionEnd;
    }

    html += '</body>';
    html += '</html>';
    
    Banana.Ui.showText('',html);
}

/**
* Find the end of a section. An adress section is a group of address
* with the same first  letter (first letter from company, lastname or firstname).
*/
function findAddressSectionEnd(addresses, start) {
    var firstAddressLetter = getAddressFirstLetter(addresses[start]);
    for (var i=start; i < addresses.length; i++) {
        if (firstAddressLetter != getAddressFirstLetter(addresses[i]))
            return i;
    }
    return addresses.length;
}

/**
* Render in html a section.
*/
function renderAddressSection(title, addresses, start, end) {
    var html = '';
    html += '<h2>' + title + '</h2>';
    html += '<table>';
    // Render Addresses
    for (var i=start; i < end; i++) {
        html += renderAddressRow(addresses[i]);
    }
    // Add three empty rows
    for (var i=0; i<3; i++) {
        html += renderAddressRow(emtptyAdress);    
    }
    html += '</table>';
    return html;
}

/**
* Render in html an address.
*/
function renderAddressRow(address)
{
    var html = '';
    html += '<tr style=\'border-bottom:solid 1px black\'>';
    html += '<td width="60%">';
    if (address.value('Company'))
        html += address.value('Company') + '<br />';
    if (address.value('LastName') || address.value('FirstName'))
        html += address.value('LastName') + ' ' + address.value('FirstName') + '<br />';
    if (address.value('CountryCode'))
        html += address.value('CountryCode') + ' - ';
    html += address.value('Zip') + ' ' + address.value('Town') + '<br />';
    html += '</td>';
    html += '<td>';
    html += 'Phone:  ' + address.value('Phone') + '<br />';
    html += 'Mobile: ' + address.value('MobilePhone') + '<br />';
    html += 'Email:  ' + address.value('Email') + '<br />';
    html += '</td>';
    html += '</tr>';    
    return html;
}

/**
* Return the first letter from company, lastname or firstname.
*/
function getAddressFirstLetter(address) {
    var value = address.value('Company');
    if (address.value('Company'))
        return address.value('Company').substr(0,1).toUpperCase();
    if (address.value('LastName'))
        return address.value('LastName').substr(0,1).toUpperCase();
    if (address.value('FirstName'))
        return address.value('FirstName').substr(0,1).toUpperCase();
    return '';
}

/**
* Return true if the address contains data.
*/
function isAddressNotEmpty(address) {
    if (address.value('Company'))
        return true;
    if (address.value('LastName'))
        return true;
    if (address.value('FirstName'))
        return true;
    return false;
}

/**
* This fuction is used to compare and sort addresses.
*/
function sortAddresses(a,b) {
    var compareSequence = [
        ['Company','Company'],
        ['Company','LastName'],
        ['Company','FirstName'],
        ['LastName','Company'],
        ['FirstName','Company'],
        ['LastName','LastName'],
        ['LastName','FirstName'],
        ['FirstName','LastName'],
        ['FirstName','FirstName']
        ];
        
    for (var i=0; i < compareSequence.length; i++) {
        var first = a.value(compareSequence[i][0]);
        var second = b.value(compareSequence[i][1]);
        if (first && second) {
            var cmp = first.localeCompare(second);
            if (cmp != 0)
                return cmp;        
        }
    }
    return 0;
}

/**
* This is a prototype of an empty address, used to render empty address rows.
*/
emtptyAdress = {};
emtptyAdress.value = function(string) {
    return '';
}

This documentation is outdated

Search in the latest documentation

The feature you are looking for is missing? Try the new version! Banana Accounting 9