QScript HtmlBuilder

From Q
Jump to navigation Jump to search

HtmlBuilder

HTML builder which creates a HTML fragment, which is used as input when writing to text items. The HTML fragment is constructed by appending elements such as paragraphs, bulleted lists and tables to it.

append(content, style)

Appends the text to the end of the HTML fragment, without any spacing afterwards. Text appended by multiple append() calls are combined into paragraphs, so you can use this to build up formatting within a paragraph.
contentPlain text. HTML tags not supported.
styleAn optional style dictionary (see setStyle()) to override formatting for this text.
Example:
var html = Q.htmlBuilder();
html.append('The highest rating brand was ');
html.append('Coca-Cola', { color: 'red' });
html.append('.');

appendBulletted(items, style)

Appends a bulleted list.
itemsAn array of strings. Plain text.
styleAn optional style dictionary (see setStyle()) to override formatting for this list.
Example:
var html = Q.htmlBuilder();
html.appendBulletted(['Coke tastes great', 'Diet drinks becoming more popular']);

appendParagraph(paragraph_text, style)

Appends a paragraph containing text to the end of the HTML fragment. The difference between append() and appendParagraph() is that this will leave a blank line after the text.

You may call this without any text to append a new blank line. For example, html.appendParagraph()

paragraph_textPlain text. HTML tags not supported.
styleAn optional style dictionary (see setStyle()) to override formatting for this text.
Example:
var html = Q.htmlBuilder();
html.appendParagraph('Most interesting results in the survey:');

appendTable(table, column_widths, row_separator, style)

Appends a table of text, whose columns will be aligned using spaces. The text will be formatted in a monospace font.
tableThe 2D array of text to show on the table.
column_widthsAn optional array of numbers. Each number represents the size of each column (in characters), after which the text will wrap onto a new line.
row_separatorAn optional string which can be used to separate rows in the table. If not null, this text will be repeated on a new line between each row.
styleAn optional style dictionary (see setStyle()) to override formatting for this table.
Example:
var html = Q.htmlBuilder();
html.appendTable([
    ['Brand', 'NPS score'],
    ['Coke', '30'],
    ['Diet Coke', '40']
]);
html.appendTable([
    ['Brand', 'NPS score'],
    ['Coke', '30'],
    ['Diet Coke', '40'],
    ['This drink gives you wings', '10']
], [15, 10], '-', { size: 8 });

equals(obj)

Whether two objects are the same.
objThe object to compare against.
Returns:true or false
Example:
data_file.getQuestionByName('Q2').equals(data_file.questions()[0])

htmlFragment

A read-only property containing the HTML fragment string, which is used as input when setting the content of text items (see QScript Text).

setStyle(style)

Set the style for all subsequent appended content.
style

A dictionary of formatting properties and their values:

font: string, name of a font. The font must be installed on your system to appear in exports (print, PDF, PowerPoint, etc.). Only the following fonts are safe to use if this text item is to appear on a web dashboard: Andale Mono, Arial, Arial Black, Century Gothic, Comic Sans MS, Courier New, Georgia, Impact, Times New Roman, Trebuchet MS, Verdana.

size: number, font size in points.

color: See JavaScript Color Table.

bold: true or false. Whether text should be bold.

italic: true or false. Whether text should be italicized.

underline: true or false. Whether text should be underlined.

Example:
var html = Q.htmlBuilder();
html.setStyle({ font: 'Times New Roman', size: 10, color: 'blue' });
html.appendParagraph('Small blue text.');
html.appendParagraph('More blue text.');
// leaving color out will make it reset to default (black)
html.setStyle({ font: 'Arial', size: 14, bold: true });
html.appendParagraph('Larger black bold text.');

type

Returns "HtmlBuilder".

See also