Modify Tables or Plots - Replace Question in All Selected Tables and Plots
Jump to navigation
Jump to search
Q Technical Reference
Q Technical Reference
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Updating and Automation > Automation Online Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library > QScript Online Library
This QScript replaces one question for another in all selected tables (or charts) in the report without changing the names of those tables (or charts). The question to replace and the new question are chosen when the QScript is run.
Technical details
- The tables or charts to be changed should be selected before the QScript is run.
- This requires Q 4.8.9 or later.
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
JavaScript
includeWeb('QScript Selection Functions');
var questions_in_selected_items = [];
var question_names = [];
if (!project.report.selectedItems)
log('This script is not able to run on this version of Q. Please upgrade to the latest version of Q.');
else {
var selected_items = project.report.selectedItems();
if (selected_items.length == 0)
alert('No items selected.');
else {
// Get questions in selected items
selected_items.forEach(function (item) {
if (item.primary && item.primary.name && question_names.indexOf(item.primary.name) == -1) {
questions_in_selected_items.push(item.primary);
question_names.push(item.primary.name);
}
if (item.secondary && item.secondary.name && question_names.indexOf(item.secondary.name) == -1) {
questions_in_selected_items.push(item.secondary);
question_names.push(item.secondary.name);
}
if (item.tertiary in item && item.tertiary.name && question_names.indexOf(item.tertiary.name) == -1) {
questions_in_selected_items.push(item.tertiary);
question_names.push(item.tertiary.name);
}
});
var replacement_questions = [];
project.dataFiles.forEach(function (data_file) {
replacement_questions = replacement_questions.concat(data_file.questions.filter(function (question) {
return !question.isHidden && question.isValid;
}));
});
// Selection of questions
var question_to_replace = selectOneQuestion('Which question would you like to replace in your selected tables/plots?', questions_in_selected_items, false);
var question_to_replace_with = selectOneQuestion('Which question would you like use instead?', replacement_questions, false);
var change_names = askYesNo('Would you like the names of the tables/plots to change?');
selected_items.forEach(function (item) {
// This is an item - table/plot/whatever.
// Look through the possible three axes for the question we want to replace.
// This will either replace the question or return nothing (null).
var replace_question = function(question) {
if (question != null && question.name == question_to_replace.name) {
// Yes! So replace it.
return question_to_replace_with;
}
// No - return nothing.
return null;
}
var original_name = item.name;
var prefix = question_to_replace.name + '" has been replaced with "'
+ question_to_replace_with.name + '" in "';
if ('primary' in item) { // this protects us against segment trees and text items
var replace = replace_question(item.primary);
if (replace != null) {
item.primary = replace;
log(prefix + original_name + '"');
}
}
if ('secondary' in item) {
var replace = replace_question(item.secondary);
if (replace != null) {
item.secondary = replace;
log(prefix + original_name + '"');
}
}
if ('tertiary' in item) {
var replace = replace_question(item.tertiary);
if (replace != null) {
item.tertiary = replace;
log(prefix + original_name + '"');
}
}
if (!change_names)
item.name = original_name;
});
log('Script finished!');
}
}
See also
- QScript for more general information about QScripts.
- QScript Examples Library for other examples.
- Online JavaScript Libraries for the libraries of functions that can be used when writing QScripts.
- QScript Reference for information about how QScript can manipulate the different elements of a project.
- JavaScript for information about the JavaScript programming language.
- Table JavaScript and Plot JavaScript for tools for using JavaScript to modify the appearance of tables and charts.
Q Technical Reference
Q Technical Reference
Q Technical Reference
Q Technical Reference
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Setting Up Data > Creating New Variables
Q Technical Reference > Updating and Automation > Automation Online Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library
Q Technical Reference > Updating and Automation > JavaScript > QScript > QScript Examples Library > QScript Online Library