Banana.Report

This documentation is outdated

The most complete and up-to-date documentation is the one of Banana Accounting Plus: Try it now

In this article

The class Banana.Report enable you to create reports, preview and print in Banana Accounting.

Introduction

The report logic is similar to the HTML / CSS logic:

  1. Create a Report object .
    • A report contain a list of ReportEements (paragraphs, texts, tables and other)
    • The element can contains other sub-elements
    • For each element you can add a class that is used for rendering the element
  2. Create a StyleSheet
  3. You preview and print a report by passing the Report and the Stylesheet object.

Each report sturcture has:

  • a ReportElement list
  • a Header Element list
  • a Footer Element list
// Report
var report = Banana.Report.newReport("Report title");
report.addParagraph("Hello World !!!", "styleHelloWorld");

// Styles
var stylesheet = Banana.Report.newStyleSheet();
var style = stylesheet.addStyle(".styleHelloWorld");
style.setAttribute("font-size", "96pt");
style.setAttribute("text-align", "center");
style.setAttribute("margin-top", "50mm");

var style2 = stylesheet.addStyle("@page");
style2.setAttribute("size", "landscape");

// Print preview
Banana.Report.preview(report, stylesheet);

 

Methods

newReport(title)

Creates a report with title 'title'. The returned object is of type Banana.Report.ReportElement.

To the report you can then add the desired elements, like paragraphs, texts, tables, and so on that construct the structure of the report.

var report = Banana.Report.newReport("Report title");

 

newStyleSheet()

Creates an empty stylesheet. The returned object is of type Banana.Report.ReportStyleSheet

To the stylesheet you can add the styles that format the report.

var stylesheet = Banana.Report.newStyleSheet();

 

newStyleSheet(fileName)

Creates a stylesheet from a file. The file has the same syntax as CSS stylesheets. The file path is relative to the script's path. The path can't contain a '..'' (parent directory).

The returned object is of type Banana.Report.ReportStyleSheet.

You can add further styles to the returned stylesheet.

var reportStyles = Banana.Report.newStyleSheet("styles.css");

*** Content of file styles.css ***
.helloWorldStyle
{
font-size: 96pt;
text-align: center;
margin-top: 50mm;
}

@page
{
size: landscape;
}
*** End of file styles.css ***

 

preview(report, stylesheet)

Opens a print preview Dialog and shows the report with the given stylesheet. 

The page orientation is given by the stylesheet. The default size and orientation is taken from the default printer, or can be set through the stylesheet.

// Set landscape orientation
stylesheet.addStyle("@page {size: landscape}");

// Set page size and orientation
stylesheet.addStyle("@page {size: A5 lanscape}");

// Displays the report
Banana.Report.preview(report, stylesheet);

 

Example: Hello world

// Simple test script using Banana.Report
//
// @id = ch.banana.script.report.helloworld
// @api = 1.0
// @pubdate = 2017-01-02
// @publisher = Banana.ch SA
// @description = Report Hello world
// @task = app.command
// @doctype = *
// @inputdatasource = none
// @timeout = -1
//

function exec(string) {

   // Create the report
   var report = Banana.Report.newReport("Report title");
   
   // Add a paragraph to the report
   report.addParagraph("Hello World !!!", "helloWorldStyle");

   // Define some styles
   var stylesheet = Banana.Report.newStyleSheet();
   
   var style = stylesheet.addStyle(".helloWorldStyle");
   style.setAttribute("font-size", "96pt");
   style.setAttribute("text-align", "center");
   style.setAttribute("margin-top", "50mm");

   var style2 = stylesheet.addStyle("@page");
   style2.setAttribute("size", "landscape");

   // Open Preview
   Banana.Report.preview(report, stylesheet);
}

 

Exemple: an example with tables, page breaks and differents styles

Result

Script

// Test script using Banana.Report
//
// @id = ch.banana.script.report.report
// @api = 1.0
// @pubdate = 2017-01-02
// @publisher = Banana.ch SA
// @description = Test report api
// @task = app.command
// @doctype = *
// @outputformat = none
// @inputdatasource = none
// @timeout = -1
//

function exec(string) {

   // Report
   var report = Banana.Report.newReport("Report title");

   var pageHeader = report.getHeader()
   pageHeader.addClass("header");
   pageHeader.addText("Page header");
   report.getFooter().addFieldPageNr();

   var watermark = report.getWatermark();
   watermark.addParagraph("Sample built with Script Report API");
 
   report.addParagraph("Report title", "titleStyle");
   report.addParagraph("1. Text", "chapterStyle").setOutline(1);

   report.addParagraph("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do " +
    "eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
    "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip " +
    "ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit " +
    "esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non " +
    "proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
 
   var paragraph2 = report.addParagraph();
   paragraph2.addText("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do ");
   paragraph2.addText("eiusmod tempor incididunt ut labore et dolore magna aliqua. ", "blueStyle");
   paragraph2.addText("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ", "boldStlyle");
   paragraph2.addText("ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit ", "underlineStyle boldStyle");
   paragraph2.addText("esse cillum dolore eu fugiat nulla pariatur.");
   paragraph2.addLineBreak();
   paragraph2.addText("Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.", "italicStyle");

   report.addParagraph("2. Table", "chapterStyle").setOutline(1);

   var table = report.addTable();
   table.getCaption().addText("Table caption");
       
   var tableHeader = table.getHeader();
   var tableHeaderRow = tableHeader.addRow();
   tableHeaderRow.addCell("Description", "", 2);
   tableHeaderRow.addCell("Income");
   tableHeaderRow.addCell("Expense");
   tableHeaderRow.addCell("Balance");
 
   var tableRow = table.addRow();
   tableRow.addCell();
   tableRow.addCell("Initial balance");
   tableRow.addCell();
   tableRow.addCell();
   tableRow.addCell(Banana.Converter.toLocaleNumberFormat("157.00")).addClass("balanceStyle");

   var tableRow = table.addRow();
   tableRow.addCell(Banana.Converter.toLocaleDateFormat("2014-02-11"));
   tableRow.addCell("Transfer from post office account");
   tableRow.addCell(Banana.Converter.toLocaleNumberFormat("500.00"));
   tableRow.addCell();
   tableRow.addCell(Banana.Converter.toLocaleNumberFormat("657.00")).addClass("balanceStyle");
 
   var tableRow = table.addRow();
   tableRow.addCell(Banana.Converter.toLocaleDateFormat("2014-02-20"));
   tableRow.addCell("Various payments");
   tableRow.addCell();
   tableRow.addCell(Banana.Converter.toLocaleNumberFormat("7250.00"));
   tableRow.addCell(Banana.Converter.toLocaleNumberFormat("-6593.00")).addClass("balanceStyle negativeStyle");

   var tableRow = table.addRow("totalrowStyle");
   tableRow.addCell();
   tableRow.addCell("Total transactions");
   tableRow.addCell(Banana.Converter.toLocaleNumberFormat("500.00"));
   tableRow.addCell(Banana.Converter.toLocaleNumberFormat("7250.00"));
   tableRow.addCell(Banana.Converter.toLocaleNumberFormat("-6593.00")).addClass("balanceStyle negativeStyle");
 
   report.addParagraph("3. Bookmarks and links", "chapterStyle").setOutline(1);

   report.addParagraph("3.1 Internal links", "chapter2Style").setOutline(2);
   report.addParagraph("-> link to bookmark on page 2").setLink("bookmarkpage2");

   report.addParagraph("3.2 External links", "chapter2Style").setOutline(2);
   report.addParagraph("-> link to Banana.ch web page").setUrlLink("http://www.banana.ch");
 
   report.addPageBreak();
 
   var chapter4 = report.addParagraph("4. Pages", "chapterStyle");
   chapter4.setOutline(1);
 
   report.addParagraph("Bookmark on page 2").setBookmark("bookmarkpage2");
 

   // Styles
   var docStyles = Banana.Report.newStyleSheet();
 
   var pageStyle = docStyles.addStyle("@page");
   pageStyle.setAttribute("margin", "20mm 20mm 20mm 20mm");
 
   var headerStyle = docStyles.addStyle("phead");
   headerStyle.setAttribute("padding-bottom", "1em");
   headerStyle.setAttribute("margin-bottom", "1em");
   headerStyle.setAttribute("border-bottom", "solid black 1px");
    
   var footerStyle = docStyles.addStyle("pfoot");
   footerStyle.setAttribute("text-align", "right");
 
   var paragraphStyle = docStyles.addStyle("p");
   paragraphStyle.setAttribute("margin-top", "0.5em");

   var captionStyle = docStyles.addStyle("caption");
   captionStyle.setAttribute("margin-top", "1em");

   var titleStyle = docStyles.addStyle(".titleStyle");
   titleStyle.setAttribute("font-size", "24");
   titleStyle.setAttribute("text-align", "center");
   titleStyle.setAttribute("margin-bottom", "1.2em");

   docStyles.addStyle(".chapterStyle", "font-size:16; margin-top:2em; margin-bottom:0.2em");
   docStyles.addStyle(".chapter2Style", "font-size:12; margin-top:1.4em; margin-bottom:0.2em");
 
   var tableStyle = docStyles.addStyle("table");
   tableStyle.setAttribute("border", "2px solid red");
 
   docStyles.addStyle("td", "border: 1px dashed black; padding: 2px;");

   var tableColStyle = docStyles.addStyle(".balanceStyle");
   tableColStyle.setAttribute("background-color", "#E0EFF6");
   tableColStyle.setAttribute("text-align", "right");

   var totalRowStyle = docStyles.addStyle(".totalrowStyle");
   totalRowStyle.setAttribute("font-weight", "bold");

   var totalBalanceStyle = docStyles.addStyle(".totalrowStyle td.balanceStyle");
   totalBalanceStyle.setAttribute("text-decoration", "double-underline");

   docStyles.addStyle(".blueStyle", "color:blue");
   docStyles.addStyle(".underlineStyle", "text-decoration:underline;");
   docStyles.addStyle(".italicStyle", "font-style:italic;");
   docStyles.addStyle(".boldStyle", "font-weight:bold");

   // Open Preview
   Banana.Report.preview(report, docStyles);
}

Share this article: Twitter | Facebook | LinkedIn | Email