QScript Variable

From Q
Jump to navigation Jump to search

Variable

A variable from one of your data files, as in each row in the Variables and Questions tab. Every variable has an associated Question object.

comment

Get/set the variable's comment (free text). Any formatting seen in Q will be lost - this is plain text only.

countUniqueValues

Returns the number of unique values contained in this variable's data.

deleteVariable()

Deletes this variable. This does not work with variables from the raw data, only copied or constructed variables.

duplicate()

Duplicates this variable and puts it in a single-variable question. This is the same as using "Copy and Paste Variable(s) | Exact" in Q. The new variable will be positioned directly below the variable it is copied from.
Returns:The new Variable object.

duplicateAs(variable_type)

Creates a new variable from this variable, but changes the type of the new variable to variable_type. This functions as in Q, so a Categorical variable created from a Text variable will automatically code the text variable, a Date variable created from a Text varible will try to automatically determine the date format, etc.
variable_typeThe the same text you see in the Variable Type column in Q. e.g 'Money', 'Categorical', 'Text'.
Returns:A new Variable object.

equals(obj)

Whether two objects are the same.
objThe object to compare against.
Returns:true or false
Example:
data_file.getQuestionByName('Q2').equals(data_file.questions()[0])

guid

Get the question's internal unique identifier.

label

Get/set the variable's label. This will not automatically rename the question too, as it does in Q desktop for single-variable questions.

name

Get/set the variable's name.

needsCheck

Get or set whether the variable has a Status of CHECK or not
Example:
v.needsCheck = false

periods

Gets the periods that result from the date configuration in a Date variable. These are the periods that would be used as an axis on a chart, and take into account the date range of the data, as well as the aggregation options configured for the variable. Example calculation: If the data is in the range midnight 22 Jun 2007 to midnight 25 Jun 2007, set up with a 1 day aggregation and period type of "All Data", then the text of the periods returned by this property would be: Period 1: '6/22/2007 12:00:00 AM - 6/22/2007 11:59:59 AM' Period 2: '6/23/2007 12:00:00 AM - 6/23/2007 11:59:59 AM' Period 3: '6/24/2007 12:00:00 AM - 6/24/2007 11:59:59 AM' Period 4: '6/25/2007 12:00:00 AM - 6/25/2007 11:59:59 AM'
Returns:An array of QScriptPeriodInfo objects for a date question. Returns null for any other type of question.
Example:
var periods = variable.periods;
 var first_period_text = periods[0].labelText;
 var first_period_start = periods[0].start;
 var first_period_end = periods[0].end;

question

Get the Question object that this variable is part of.

rawValues

Returns the list of raw values contained in this variable's data. These are the "source" values from the raw data, and no recoding will be applied (i.e. the 'valueAttributes' have no affect on these values). If you require the current recoded values, you must use the 'valueAttributes' property and manually recode these source values.

sourceLabel

Get the variable's source label that was specified in the raw data file. This will return a null value or empty string for Q-created variables (such as JavaScript variables).

type

Returns 'Variable'.

uniqueValues

Returns the list of unique values contained in this variable's data. These are the "source" values from the raw data, and no recoding will be applied (i.e. the 'valueAttributes' have no affect on these values). If you require the current recoded values, you must use the 'valueAttributes' property and manually recode the unique source values.

valueAttributes

Gets the ValueAttributes object for just this variable. If this variable is part of a question containing multiple values then you should probably be using question.valueAttributes() instead.

variableType

Get or sets the variable's type. This can be one of 'Numeric', 'Text', 'Categorical', 'Ordered Categorical', 'Money' or 'Date'. Be aware that if the original variable type is 'Text' then you can attempt changing it to 'Categorical' or 'Ordered Categorical' directly, but if the variable is being referenced by another variable (e.g. a binary filter) then an error may be thrown and you should use the function duplicateAs() to convert it to a categorical instead. If you want to change a 'Text' variable to 'Numeric', 'Money', or 'Date' then you should use the function duplicateAs() instead as these cannot be changed to directly.
Example:
v.variableType = "Categorical"
Example:
if (v.variableType == 'Text') {
    try {
        v.variableType = "Categorical";
    } catch (e) {
        // The variable is referenced elsewhere.  Create a duplicate.
        v.duplicateAs('Categorical');
    }
}