Modify Labels - Remove Variable Names and Values from Labels

From Q
Jump to navigation Jump to search

This QScript removes all changes made by Modifying Labels - Add Variable Names And Values To Labels to the labels in your project and is only intended for this purpose. This script looks for a special invisible character in the labels and will have no affect in the absence of this character (i.e., if you manually modify labels and inadvertently remove the hidden character, then this QScript will not work). For examples and technical details please see Modifying Labels - Add Variable Names And Values To Labels.

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 Functions to Generate Outputs");

if (!main())
    log("QScript Cancelled.");
else
    conditionallyEmptyLog("QScript Finished.");
 
function main() {
    includeWeb("QScript Utility Functions");
 
    var hidden_char = "\u200b ";
 
    project.dataFiles.forEach(function (data_file) {
        data_file.questions.forEach(function (question) {
                        if (!question.isBanner && question.isValid) 
                    undoVariableNameAndValueAddition(question, hidden_char);
        })
    });
    return true;
}
 
// Looks for hidden_char in labels and removes averything that comes 
// before it. This includes:
// 1. Question names.
// 2. Variable Labels.
// 3. Data reduction labels.
function undoVariableNameAndValueAddition(question, hidden_char) {
    var variables = question.variables;
    var data_reduction = question.dataReduction;
    var question_type = question.questionType;
 
    // Remove Variable Name from Question Name
    var split_q_name = question.name.split(hidden_char);
    if (split_q_name.length == 2)
        question.name = preventDuplicateQuestionName(question.dataFile, split_q_name[1]);
 
    // Values to Category Labels (Pick One, Pick One - Multi)
    if (question_type.indexOf("Pick One") == 0) {
        var codes = (question_type == "Pick One" || (question_type == "Pick One - Multi" && !data_reduction.transposed)) ? data_reduction.rowLabels : data_reduction.columnLabels;
        codes.forEach(function (code) {
            var split_code = code.split(hidden_char);
            if (split_code.length == 2)
                data_reduction.rename(code, split_code[1]);
        });
    }
 
    // Remove Variable Names from Variable Labels (and corresponding data
    // reduction labels where relevant)
    variables.forEach(function (v) {
        var split_label = v.label.split(hidden_char);
        if (split_label.length == 2)
            v.label = split_label[1];
    });
 
    var mr_types = ["Pick One - Multi", "Number - Multi", "Pick Any"];
    if (mr_types.indexOf(question_type) > -1) {
        var by_columns = (question_type == "Pick One - Multi" && !data_reduction.transposed);
        var codes = by_columns ? data_reduction.columnLabels : data_reduction.rowLabels;
        codes.forEach(function (code) {
            var split_code = code.split(hidden_char);
            if (split_code.length > 1)
                data_reduction.rename(code, split_code[split_code.length - 1]);
        });
    }
}

See also