Tables - Table from Data Set

From Q
Jump to navigation Jump to search

This QScript inserts a Table with row and column variable setsquestions prefilled based on the selected data.

Usage

To use this feature, begin by selecting the variable setsquestions you wish to create tables for and then select Table > Table from Data SetCreate > Tables > Table from Data Set. The first selected variable setquestion will be placed in the rows and the second (if one is provided) will be placed in the columns.

If more than two variable setsquestions are selected, additional tables will be created for the extra variable setsquestions.

How to apply this QScript

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Click on the QScript when it appears in the QScripts and Rules section of the search results.

OR

  • Select Automate > Browse Online Library.
  • Select this QScript from the list.

Customizing the QScript

This QScript is written in JavaScript and can be customized by copying and modifying the JavaScript.

Customizing QScripts in Q4.11 and more recent versions

  • Start typing the name of the QScript into the Search features and data box in the top right of the Q window.
  • Hover your mouse over the QScript when it appears in the QScripts and Rules section of the search results.
  • Press Edit a Copy (bottom-left corner of the preview).
  • Modify the JavaScript (see QScripts for more detail on this).
  • Either:
    • Run the QScript, by pressing the blue triangle button.
    • Save the QScript and run it at a later time, using Automate > Run QScript (Macro) from File.

Customizing QScripts in older versions

  • Copy the JavaScript shown on this page.
  • Create a new text file, giving it a file extension of .QScript. See here for more information about how to do this.
  • Modify the JavaScript (see QScripts for more detail on this).
  • Run the file using Automate > Run QScript (Macro) from File.

JavaScript

includeWeb('QScript Utility Functions');  // for inDisplayr()
includeWeb('QScript Selection Functions');// for getAllUserSelections()

let displayr = inDisplayr();
let selected = getAllUserSelections().selected_questions;
let page;
const as_output = !displayr || (Q.singleOutputsAreEnabled && Q.singleOutputsAreEnabled());

if (as_output)
    page = project.report;
else {
    page = project.currentPage();
    if (page == null)
        page = project.report.appendPage('Blank');
}

let table;
if (selected.length === 0) {
    table = page.appendTable();
} else {
    let outputs;
    if (selected.every(obj => obj.hasOwnProperty("type") && obj.type === "Question"))
        outputs = selected.map(q => ({
            primary: q,
            secondary: /^Text/.test(q.questionType) ? "RAW DATA" : "SUMMARY"
        }));

    outputs = outputs.filter(obj => obj.primary.isValid && (typeof obj.secondary === 'string' || !!obj.secondary.isValid))

    const n_outputs = outputs.length;

    let top_offset = 0;
    let left_offset = 0;
    for (let i = 0; i < n_outputs; i++) {
        table = page.appendTable(as_output);
        table.primary = outputs[i].primary;
        table.secondary = outputs[i].secondary;

        if (!as_output) {
            table.left += left_offset;
            table.top += top_offset;
            left_offset += 15;
            top_offset += 15;
        }
    }
}

if (Q.fileFormatVersion() > 8.65)
    project.report.setSelectedRaw([table]);

See also

See Also

* Basic Tables