Introduction
There are many things you can automate in Q to speed up your analyses. This is the way to use QScripts to add folders to your report tree and customized text outputs.
Requirements
- 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 Q.
Method
In QScript, the project is referenced as the project
object and the Report tree is the project.report
. You can append various objects to your project, such as folders, tables, R calculations, and charts. However, folders are the most fundamental.
In Q there are several key items to understand before working with folders in a QScript:
- A folder is not required to add objects to it. By default, any new object will be added directly to the Report tree.
- Objects within a folder are
subItems
of that folder which in turn is asubItem
ofproject.report
. - A folder can be nested within another folder.
In our first example we will add a folder called Nested Folder which appears within a folder called Folder. Nested Folder will additionally have a text output that includes the folder name.
The workflow for this is as follows:
- Create the "folder" page.
- Add a nested folder.
- Add the text output to the nested folder.
Here is the code:
// Add folder
var folder = project.report.appendGroup();
folder.name = "Folder";
// Add nested folder
var folderName = "Nested Folder";
var nestedFolder = folder.appendGroup();
nestedFolder.name = folderName;
// Add text output
var text = nestedFolder.appendText();
var html = Q.htmlBuilder();
html.append(folderName, { font: 'Open Sans', color: 'red', size: 18 });
text.content = html;
- We begin by using
appendGroup()
to add a blank folder to our project, that isproject.report
, calledfolder
, and set the name. - As we will use the folder name more than once, we will first declare it as
folderName
. - We again use
appendGroup()
but instead, add a folder tofolder
and set its name. - The nested folder's text output is then 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.
Instead of only appending the folder name, we can also append other items, such as paragraphs and bullet points:
// Add text output
var text = nestedFolder.appendText();
var html = Q.htmlBuilder();
html.append(folderName, { font: 'Open Sans', color: 'red', size: 18 });
html.appendParagraph('Most interesting results in the survey:');
html.appendBulletted(['Coke tastes great', 'Diet drinks becoming more popular']);
text.content = html;
Updating folder names
subItem
of report
and set the name
property:var folder = project.report.subItems[0];
folder.name = "Our report";
Page references can also be based on current selection by using project.currentPage()
:
var report = project.currentPage();
report.name = "Renamed Page";
Next
How to Create Tables and R Calculations via QScript
Later
How to Create a Chart via QScript
How to Modify Tables via QScript
See Also
How to Work with Variables via QScript