The snippet below can be used in a Custom QScript to create new JavaScript variables programmatically. See JavaScript Variables for an introduction and explanation of how to create them via the menus.
Technical details
The function newJavaScriptVariable()
used to create the new variable has arguments:
newJavaScriptVariable(source_code, is_text, name, label, after, options)
You can read more in our JavaScript function documentation.
Method
// Working on Phone.sav, this creates a new JavaScript variable called weight.
project.dataFiles[0].newJavaScriptVariable("q1 == 1 ? 0.5 : 1.5", false, "weight", "Weight", null);
// Working on Phone.sav, the following creates a new JavaScript Variable called weight, positioned below Q1.
var _qBelow = project.dataFiles[0].getVariableByName("q1");
project.dataFiles[0].newJavaScriptVariable("q1 == 1 ? 0.5 : 1.5", false, "weight", "Weight", _qBelow);
See also
- QScript for an explanation of how to run this code.
- QScript Reference for technical information.
- JavaScript for information about the JavaScript programming language.