This article describes how to use a QScript to add pages, folders, and headings to a document.
Requirements
- A Displayr Enterprise user license which includes the ability to add custom analysis menu items or use the Displayr API.
- The importdata.zip file which includes the Phone data set that we will use for this article.
- You have read How to Create a Custom QScript and How to Work with Variables via QScript.
- You are using one of the methods from How to Use QScripts in Displayr.
Method
In QScript, the document is referenced as the project
object and the Pages section is the project.report
. You can append various objects to your document, such as pages, tables, calculations, and visualizations. However, pages are the most fundamental.
In Displayr there are several key items to understand before working with pages in a QScript:
- A page is required to add objects to it.
- Objects within a page are
subItems
of that page which in turn is asubItem
ofproject.report
. - A folder is essentially a nested page or a page placed within another page.
- Pages can have different layouts as per your Page Master.
- All pages have a title box (with the exception of Blank page layouts).
Appending blank pages and text boxes
In our first example, we will add a page called Page which appears within a folder called Folder. Page will additionally have a title positioned at the top left that includes the page name.
The workflow for this is as follows:
- Create the "folder" page.
- Add a nested page.
- Add the title to the nested page.
Here is the code:
// Add folder
var folder = project.report.appendGroup();
folder.name = "Folder";
// Add page
var pageName = "Page";
var page = folder.appendGroup();
page.name = pageName;
// Add heading
var title = page.appendText();
var html = Q.htmlBuilder();
html.append(pageName, { font: 'Open Sans', size: 18 });
title.content = html;
title.top = 10;
title.left = 10;
- We begin by using
appendGroup()
to add a blank page to our document, that isproject.report
, calledfolder
, and set the name. Alternatively, we can useappendFolder()
to directly set this as a folder. - As we will use the page name more than once, we will first declare it as
pageName
. - We again use
appendGroup()
but instead add a page tofolder
and set its name. - The page's title field is created using the
appendText()
property and its content is set with an HTML builder function calledhtml.append()
that allows us to set the text and styling. - Finally, we set the top and left positions to fix the title in the top left corner.
Appending page layouts
In our second example we will add a specific page layout, Title and Content, and rename the title:
// Add page
var pageName = "Page";
var page = project.report.appendPage("Title and Content");
page.name = pageName;
var titleText = page.subItems[0];
titleText.text = pageName;
- As we will use the page name more than once, we will first declare it as
pageName
. - This time we use
appendPage()
and specify the page layout we want to add by name as per the Page Master. NoteappendGroup()
works the same here asappendPage("Blank")
. - We then set the page name and update the
text
property of our title field which is the firstsubItem
on the page.
Updating page titles
subItem
of report
and set the name
property:var page = project.report.subItems[0];
page.name = "Our report";
Page references can also be based on current selection by using project.currentPage()
:
var page = project.currentPage();
page.name = "Renamed Page";
Next
How to Create Tables and Calculations via QScript
Later
How to Create a Visualization via QScript
How to Modify Tables via QScript
See Also
How to Work with Variables via QScript
How to Create a Custom QScript