Editing JavaScript in Q
In Q4.10, the JavaScript Variable expression editor and Rule script editor have been significantly updated. An editor for QScripts has also been added, which can be accessed via Automate > Open QScript (Macro) Editor. These editors share many common features, which are described on this page.
Actions
The following actions are accessible from the the menu, toolbar and context menu (which is displayed when right-clicking in the text area), and should be familiar to users of other code editors:
*QScript editor only.
**QScript and Rule editors only.
Other Features
Syntax Highlighting
In order to improve code readability, sections of the script are colored according to their type as determined by JavaScript syntax. The colors used for each type are displayed in the table below. The JavaScript Variable editor additionally highlights names of Q Variables in the project, by changing the background color of the variable name, as shown in the JavaScript Variable editor screenshot.
Type | Example |
---|---|
Strings | 'example string' |
Numbers | 2.71828 |
Miscellaneous values*** | null |
Comments | // example comment |
JavaScript keywords | function |
Regular expressions | /[\da-f]/i |
***These include true, false, null, NaN, undefined, Infinity.
Bracket Highlighting
Matching brackets (i.e.: ( ),{ },[ ]) are highlighted with a grey background when the keyboard cursor is next to either the left or right bracket. Unclosed brackets are highlighted with a red background when the keyboard cursor is next to it.
Autocomplete
The editor offers word suggestions while text is being typed into the editor. The word suggestions are based upon keywords, property names, variable and function names and words in strings that appear in the script. The suggestions appear in a dropdown near the keyboard cursor. Use the up and down keys to highlight the correct suggestion and then press enter to insert it into the script. To close the dropdown, press escape, in order to restore normal use of the up, down and enter keys.
Common Methods
A list containing the methods and properties of the objects Q, Math and some other methods is displayed to the right of the text area. For the Rule editor, methods and properties of the objects table, form are also included. Double-click on a method or property from the list to insert it into the script. Alternatively, the method or property may be copied to the clipboard using the keyboard shortcut Ctrl + C or right-clicking it and selecting Copy in the context menu.
Error Reporting
If the script was executed successfully (i.e. QScript ran without errors, Rule applied to the table without errors or JavaScript variable expression was evaluated without errors), a check mark () will be displayed in the bottom left-hand corner of the editor. If there were any errors in the script, a cross () will be displayed instead, with the error message and row and column numbers of the error displayed next to the cross. Additionally, the location in the script at which the error occurred will be underlined in red.
Editing Copies of QScripts and Rules from the Online Library
Copies of the script from QScripts and Rules in the Online Library can be edited in the QScript and Rule editors, by clicking on the Edit a Copy button after selecting the QScript or Rule in the Online Library dialog. Rules from the Online Library that have been applied to a table or chart can be edited by clicking on the Edit JavaScript... button in the Edit Formatting Rule dialog. Note that since only a copy of the script from the Online Library is being edited, any changes will not affect the version in the Online Library.