Tables - EXPERIMENTAL - Select

From Q
Jump to navigation Jump to search

Select from rows or columns of one or more tables via page controls or Object Inspector (GUI) controls.

This QScript allows you to select rows and columns of tables that you wish to highlight in your report.

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

function selectFromTables() {
    includeWeb("QScript Table Functions");     // for recursiveGetAllTablesInGroup()
    includeWeb("QScript R Output Functions");  // for generateUniqueRObjectName()
    const VALID_ROUTPUT_CLASSES = ["matrix","array","data.frame","table"];
    let selections = project.report.selectedRaw();
    let is_displayr = (!!Q.isOnTheWeb && Q.isOnTheWeb());
    if (project.report.selectedRaw().length === 0 || (selections.length === 1 && selections[0].type === "ReportGroup")) {
        let table_array = [];
        recursiveGetAllTablesInGroup(project.report,table_array);
        if (table_array.length === 0) {
            log("Please add Table(s) to your page/document to use this feature.");
            return false;
        }
        let selection_idx = selectMany("Please choose the tables you wish to select rows/columns from", table_array.map(t => t.name));
        var valid_selections = [];
        for (let i = 0; i < selection_idx.length; i++)
            valid_selections.push(table_array[selection_idx[i]]);
        if (is_displayr) {
            var group = project.report.appendPage("Blank");
            group.name = "Select: " + valid_selections.map(t => t.name).join(", ");
        }else
            var group = valid_selections[0].group;
    }else {
        var valid_selections = selections.filter(s => (s.type === "Table" && s.question !== null)
              || (s.type === "R Output"
                  && s.outputClasses.filter(c => VALID_ROUTPUT_CLASSES.includes(c)).length > 0));
        if (valid_selections.length === 0) {
            log("Please select Table(s) and/or R Output(s) containing the tables to be selected from.");
            return false;
        }
        if (valid_selections.length < selections.length)
            log("Some selections were not a Q Table or an R Output (containing a table) and they will be ignored.");
        var group = valid_selections[0].group;
    }
    let output_name = generateUniqueRObjectName('selected.table');
    //let r_expression = rCodeAsString(output_name);
    //let selected_table = group.appendR(r_expression);
    let prefilled_controls;
    //selected_table.setCodeForGuiControls(guiControlsAsString(output_name));
    if (valid_selections.length > 1){
        let table_names = prompt("Please enter names for the " + valid_selections.length + " selected tables, separated by commas.");
        //selected_table.setGuiControlInputRaw("formTableNames",table_names);
        let cb = group.appendControl("Combobox");
        cb.top = 0;
        cb.left = 0;
        cb.placeholderText = "Select table to show...";
        cb.selectionMode = "SingleSelection";
        cb.whenItemListChanges = 'KeepCurrentSelection';
        cb.itemList = table_names.split(",");
        cb.selectedItems = table_names.split(",").slice(0,1);	
        //selected_table.setGuiControlInputRaw("formTableSelection",cb.guid);
        prefilled_controls = {
            "formTables": table_names,
            "formTableSelection": cb.guid
        }
    }else
        prefilled_controls = {};
    for(let i = 0; i < valid_selections.length; i++) {
       valid_selections.hiddenFromExportedViews = true;
       //selected_table.setGuiControlInputRaw("formTables"+(i+1),valid_selections[i].guid);
       prefilled_controls["formTables"+(i+1)] = valid_selections[i].guid;
//       selected_table.update();
    }
   let selected_table = group.appendStandardR("Tables - Select", prefilled_controls);
    project.report.setSelectedRaw([selected_table]);
    log(prefilled_controls.formTables1);
    return true;
}

if (!selectFromTables()) {
    log("QScript cancelled.");
}

See also