Banana.Report.ReportElement

Documentation •
In this article

The class Banana.Report.ReportElement represents the report itself and every element in the report, such as sections, paragraphs, tables, texts and the report itself.

Once you create a new report through the method Banana.Report.newReport() you can start adding sections, paragraphs, texts, tables and so on. 

When you add an element with one of the add methods, you get as return value an object of type

Elements as a container of other elements

Banana.Report.ReportElement that represents the added element.  
To this object you can add further elements and by this way construct the structure of the report.

Report  
   +  Paragraph  
   +  Table  
      +  Row  
         + Cell  
         + Cell  
      +  Row  
         + Cell  
         + Cell  
      ...

Even if this interface allows you to add tables to text elements or columns to paragraphs, the result will be undefined.

Formatting text size, text color, margins, and so on are set separately through a Banana.Report.ReportStyleSheet object.

 

Methods

addAttachment(name, content)

Add an attachment to the document. 

  • The param name defines the name of the attachment inclusive extention (.png, .pdf, .xml) that will appear in print preview attachment's list on in the printed pdf.
  • The param content contains the path to the file to attach or the data to attach.
    • The path can be relative to the script's folder, the document's folder, the name of a document attacched to the file or a data uri scheme (for images imbedded in the document).
    • file:script/<relative_path_to_script_folder>/<image_name>
    • file:document/<relative_path_to_file_folder>/<image_name>
    • documents:<document_name>
    • data:[<media type>][;charset=<character set>][;base64],<data>

Example:


//Create the report
var report = Banana.Report.newReport('Report attachments');
//Add a paragraph with some text
report.addParagraph('Report with attachments');
//Attach file on computer path relative to the accounting file  
report.addAttachment('doc1.pdf', 'doc1.pdf'); 
report.addAttachment('doc2.pdf', 'documents/doc2.pdf'); 
//Attach text files created on the fly
//We use the prefix 'data:...' to tell that the string is not an url but is itself the content of the file
report.addAttachment('text file 1.txt', 'data:text/plain;utf8,This is the content of the text file 1.');
report.addAttachment('text file 2.txt', 'data:text/plain;utf8,This is the content of the text file 2.');
report.addAttachment('text file 3.txt', 'data:text/plain;utf8,This is the content of the text file 3.');
//Attach an image stored in the document table
//We use the prefix 'document:...'
report.addAttachment('logo.jpg', 'documents:logo');
//Add an xml element
//We just add the new created Banana.Xml.newDocument
var xmlDocument = Banana.Xml.newDocument("eCH-0217:VATDeclaration");
var rootNode = xmlDocument.addElement("eCH-0217:VATDeclaration");
rootNode.addElement("title").addTextNode("Vat Declaration 2018");
report.addAttachment('vat_declaration.xml', xmlDocument);
//Print the report
var stylesheet = Banana.Report.newStyleSheet();
Banana.Report.preview(report, stylesheet);

Since Banana Accounting 9.0.4

addClass(classes)

Add classes to the node. A class binds the element to the corresponding class style definend in Banana.Report.ReportStyleSheet as used in CSS Stylesheets.


var report = Banana.Report.newReport("Report title");
report.addParagraph("1250.00").addClass("balanceStyle");

addDocInfo(name, value [, description])

Add document's informations to the document. When you generate multiple documents from your script and want to export them as separated pdfs, document's informations are used by the application to build meaningful file names for the exported pdf files, like "Invoice 1234 - Joe Black.pdf". The document's information "name" contains the default file name used by the application. The user when creating the pdf files, can choose to use the default "name", or combine freely the other document's informations for the file names.

For the following names, the application add a default description. For other names, like "doc_status", you can add your own description.

  • name
  • doc_title
  • doc_number
  • doc_date
  • doc_amount
  • doc_currency
  • customer_number
  • customer_name
  • customer_city
  • customer_email
  • account
  • cur_date
  • cur_time

You can add document's informations only to a document object.

report.addDocInfo("name", "Invoice 1234 - Joe Black.pdf");
report.addDocInfo("doc_title", "Invoice 1234");
report.addDocInfo("doc_number", "1234");
report.addDocInfo("customer_name", "Joe Black");
report.addDocInfo("doc_amount", "1023.00");
report.addDocInfo("doc_status", "paid", "Document status");

addSection([classes])

Add a section and return the created section as a Banana.Report.ReportElement object.

You can add sections only to sections, cells, captions, headers or footers.


var report = Banana.Report.newReport("Report title");
//Add a section with a style
var section = report.addSection("sectionStyle");
section.addParagraph("First paragraph");
section.addParagraph("Second paragraph");

addParagraph([text, classes])

Add a paragraph and return the created paragraph as a Banana.Report.ReportElement object.

You can add paragraphs only to sections, cells, captions, headers or footers.


var report = Banana.Report.newReport("Report title");
//Add an empty paragraph
report.addParagraph(" ");
//Add a paragraph with a text
report.addParagraph("Hello World !!!");
//Add a paragraph with a text and a style
report.addParagraph("Hello World !!!", "styleHelloWorld");

addHeader1..6([text])

The methods addHeader1 to addHeader6 add an header and return the created header as a Banana.Report.ReportElement object. The corresponding outline from 1 to 6is set automatically.

You can add headers only to sections, and documents.

var report = Banana.Report.newReport("Report title");
//Add headers
report.addHeader1("Header 1");
report.addHeader2("Header 1.2");
report.addParagraph("Some text.");
report.addHeader1("Header 2"); 
report.addHeader2("Header 2.1");
report.addParagraph("Some text.");
report.addHeader2("Header 2.2");
report.addParagraph("Some text.");

addText(text [, classes])

Add a text node and return the created text node as a Banana.Report.ReportElement object.

You can add texts only to sections, paragraphs, cells, captions, headers or footers.


var report = Banana.Report.newReport("Report title");
//Add a text 
report.addText("Hello world !!!");
//Add a text with a style
report.addText("Hello world !!!", "styleHelloWorld");

addStructuredText(text, format, [, stylesheet])

Add a structured text to the document.

Supported formats are

  • 'md'  
    Structured github markdown.
  • 'html'
  • 'text'

The function returns the new section containing the structured text  as a Banana.Report.ReportElement object.

You can add structured text only to sections, paragraphs, cells, captions, headers or footers.

Open examples in browser

Example for Markdown text

// Report
var report = Banana.Report.newReport("Report title");
// Styles
var stylesheet = Banana.Report.newStyleSheet();
// Add Markdown text
let mdText = "# Header 1\n";
mdText += "## Header 2\n";
mdText += "### Header 3\n";
mdText += "Markdown text with **bold** and *italic*.  \n";
mdText += "[Markdown link](https://www.banana.ch)  \n";
mdText += "* List row 1\n";
mdText += "* List row 2\n";
mdText += "* List row 3\n";
report.addStructuredText(mdText, "md");
// Print preview
Banana.Report.preview(report, stylesheet);

Example for simple Html text

// Report
var report = Banana.Report.newReport("Report title");
// Styles
var stylesheet = Banana.Report.newStyleSheet();
// Add html text
let htmlText = "<h1 style=\"color: red\">Header 1 Red</h1>\n";
htmlText += "<p style=\"color: blue\">Paragraph blue</p>\n";
report.addStructuredText(htmlText, "html");
//Print preview 
Banana.Report.preview(report, stylesheet); 

Example for full Html text

If you add a full html (with head and style element), you should pass a stylesheet parameter. The styles defined in the html style element are added to the report stylesheet. If not the styles defined in the html style element are lost, and like in this example the paragraphs are not coloured with red and blue.

// Report
var report = Banana.Report.newReport("Report title");
// Styles
var stylesheet = Banana.Report.newStyleSheet();
// Add html text
let htmlText = "<html>\n";
htmlText += "<head>\n";
htmlText += "<style>\n";
htmlText += "p.blue {color: blue;}\n";
htmlText += ".red {color: red;}\n";
htmlText += "</style>\n";
htmlText += "</head>\n";
htmlText += "<body>\n";
htmlText += "<h1 class=\"red\">Header 1 Red</h1>\n";
htmlText += "<p class=\"blue\">Paragraph blue</p>\n";
htmlText += "</body>\n";
htmlText += "</html>\n";
report.addStructuredText(htmlText, "html", stylesheet);
//Print preview 
Banana.Report.preview(report, stylesheet); 

Example for Plain text

// Report
var report = Banana.Report.newReport("Report title");
// Styles
var stylesheet = Banana.Report.newStyleSheet();
// Plain Text
let plainText = "Testo riga 1\n";
plainText += "Testo riga 2\n";
plainText += "Testo riga 3\n";
// Add plain text
report.addStructuredText(plainText, "text");	
	
// Print preview
Banana.Report.preview(report, stylesheet);

 

addTable([classes])

Add a table and return the created table as a Banana.Report.ReportElement object.

You can add tables only to the report or sections.


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

addColumn([classes])

Add a column and return the created column as a Banana.Report.ReportElement object.

You can add columns only to tables.


var column1 = myTable.addColumn("column1");
var column2 = myTable.addColumn("column2");
var column3 = myTable.addColumn("column3");

addRow([classes])

Add a row and return the created row as a Banana.Report.ReportElement object.

You can add rows only to tables, table headers or table footers.


var tableRow = myTable.addRow();
...

addCell([span])

Add an empty cell and return the created cell as a Banana.Report.ReportElement object.

You can add cells only to rows. You can span cells over columns but not over rows.


tableRow.addCell();      //span empty cell over 1 column (default value)
tableRow.addCell("", 3); //span empty cell over 3 columns
...

addCell(text [,classes, span])

Add a cell to the node and return the created cell as a Banana.Report.ReportElement object.

You can add cells only to rows.You can span cells over columns but not over rows.


tableRow.addCell("Bank", "firstCellStyle", 3);      //span cell over 3 columns
tableRow.addCell("1200.65", "secondCellStyle, 1);   //span cell over 1 column
...

addLineBreak()

Add a line break and return the created line break as a Banana.Report.ReportElement object.

You can add line breaks only to paragraphs or cells.


// Add a line break to a paragraph
var p = report.addParagraph(" ");
p.addLineBreak();
// Add a line break to a cell
var c = tableRow.addCell();
c.addLineBreak();

addPageBreak()

Add a page break node and return the created page beak as a Banana.Report.ReportElement object.

You can add page breaks only to the report or sections.


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

addImage(path [,classes])

Add an image and return the created image as a Banana.Report.ReportElement object. Supported formats are:

  • png
  • jpg
  • svg

The path can be relative to the script's folder, the document's folder, the name of a document attacched to the file or a data uri scheme (for images imbedded in the document).  
- file:script/<relative_path_to_script_folder>/<image_name>  
- file:document/<relative_path_to_file_folder>/<image_name>  
- documents:<document_name>  
- data:[<media type>][;charset=<character set>][;base64],<data>

You can add images only to sections, paragraphs, cells, captions, headers or footers.

The parameter path can be absolute or relative to the script path.


var report = Banana.Report.newReport("Report title");
// Add an image located in the script folder
report.addImage("file:script/logo_abc.jpg");
// Add an image located in the dcoument folder
report.addImage("file:document/logo_mnp.jpg");
// Add an image saved in the table documents
report.addImage("documents:logo_xyz.jpg");
// Add an image (a red dot) included in the document
report.addImage("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO 9TXL0Y4OHwAAAABJRU5ErkJggg==");
// Add a SVG base64 image
report.addImage("data:image/svg+xml;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO 9TXL0Y4OHwAAAABJRU5ErkJggg==");

addImage(path, widht, height [,classes])

Overloaded method to add an image and return the created image as a Banana.Report.ReportElement object.

The parameters width and height have the same syntax as css length values. They can be absolute (ex.: "30px", "3cm", ... ) or relative (ex.: "50%", "3em", ...).


        var report = Banana.Report.newReport("Report title");
        report.addImage("documents:image_logo", "3cm", "5cm");
        

addFieldPageNr([classes])

Add a field containg the page number and return the created field as a Banana.Report.ReportElement object.

You can add this field only to sections, paragraphs, cells, captions, headers or footers.


var report = Banana.Report.newReport("Report title");
...
// Add the page number to the paragraph
report.addParagraph("Page ").addFieldPageNr();
// Add a page number to the footer
var footer = report.getFooter();
footer.addText("Page ");
footer.addFieldPageNr();

excludeFromTest()

Mark the field to not be tested during a test case. The value is in any case outputed to the test results, it is just ignored during the comparison of the test results.

This is useful, for example, for text fields containing the current date.


var currentDate = Banana.Converter.toLocaleDateFormat(new Date());
var textfield = report.getFooter().addText(currentDate);
textfield.excludeFromTest();

getWatermark()

Return the watermark element.

Only the report has a watermak element.


var watermark = report.getWatermark();
watermark.addParagraph("Watermark text");

getHeader()

Return the header of the element. 

Only tables and the report have an header element.


var report = Banana.Report.newReport("Report title");
//Report
var reportHeader = report.getHeader();
reportHeader.addClass("header");
reportHeader.addText("Header text");
//Table
var table = report.addTable("myTable");
var tableHeader = table.getHeader();
tableRow = tableHeader.addRow();
tableRow.addCell("Description");
tableRow.addCell("Amount");

getFooter()

Return the footer of the element. 

Only tables and the report have a footer element.

//Report
var footer = report.getFooter();
footer.addText("Footer text");

getCaption()

Return the caption of the element. 

Only tables have a caption element.

var table = report.addTable("MyTable");  
var caption = table.getCaption();
caption.addText("Table caption text", "captionStyle");

getChildren()

Return the children of the element as an array.

var report = Banana.Report.newReport("Report title");
let section = report.addSection(); 
section.addParagraph("some text 1"); 
section.addParagraph("some text 2"); 
let count = section.getChildren().length; // count == 2

This method was introduced in BananaPlus 10.0.12.151.

getParent()

Return the parent of the element, or null if the element is the root element.

var report = Banana.Report.newReport("Report title");
let section = report.addSection(); 
let paragraph = section.addParagraph("some text"); 
let root = paragraph.getRoot(); // root == report 

This method was introduced in BananaPlus 10.0.12.151.

getRoot()

Return the root element.

var report = Banana.Report.newReport("Report title");
let section = report.addSection();
let paragraph = section.addParagraph("some text");
let parent = paragraph.getParent(); // root == paragraph

This method was introduced in BananaPlus 10.0.12.151.

getTag()

Return the tag of the element, like 'body', 'p', 'table', 'td' and so on.

var report = Banana.Report.newReport("Report title");
...
report.getTag(); // returns 'body'
footer.getTag(); // returns 'pfoot'
...

getTitle()

Return the title of the element.  
Only a document element have a title.


var report = Banana.Report.newReport("My Report Title");
var title = report.getTitle(); // return 'My Report Title'

setOutline(level)

Set the outline level, this is used to create the index when exporting to pdf.


report.addParagraph("1. Text").setOutline(1);

setBookmark(bookmark)

Set a bookmark (or anchor), this is used in conjunction with setLink().


report.addParagraph("Bookmark on page 2").setBookmark("bookmarkpage2");

setLink(bookmark)

Set a link to a bookmark. See setBookmark().


report.addParagraph("-> link to bookmark on page 2").setLink("bookmarkpage2");

setPageBreakBefore()

Set to insert a page break before the element.


// Insert a page break before a paragraph
report.addParagraph("Hello world!!!").setPageBreakBefore();
// Insert a page break before a table
   /* first create a table then... */
myTable.setPageBreakBefore();

setSize(width, height)

Set the size of the element.

The parameters width and height have the same syntax as css length values. They can be absolute (ex.: "30px", "3cm", ... ) or relative (ex.: "50%", "3em", ...).

You can only set the size of an image element.


var image = report.addImage("C:/Documents/Images/img.jpg");
image.setSize("3cm", "6cm");

setStyleAttribute(attr_name, attr_value)

Set a style attribute to the element. Attributes ids and values follow the CSS specification. This attibute correspont to the inline styles in Css.


paragraph.setAttribute("font-size", "24pt");

setStyleAttributes(attributes)

Set style attributes to the element. Attributes ids and values follow the CSS specification. Those attributes correspond to the inline styles in Css.


        paragraph.setAttribute("font-size:24pt;font-weight:bold;");
        

setTitle(title)

Set the title of the element. 

Title can be only set to a document element.


        document.setTitle("Annual report");
        

setUrlLink(link)

Set a link to an external file (file://...) or a web page (http://....).

To the element the class "link" is automatically added.


report.addParagraph("Link to Banana.ch web page").setUrlLink("http://www.banana.ch");
 

Help us improve the documentation

We welcome feedback on how to improve this page.

Tell us what theme needs a better explanation or how to clarify a topic.

Share this article: Twitter | Facebook | LinkedIn | Email