Editing JavaScript in Q

From Q
Jump to navigation Jump to search

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:

Action Icon Shortcut Description
New* New.PNG Ctrl + N Replace the current script with a blank script.
Open* Open.png Ctrl + O Open a .QScript file from the directory.
Save* Save Blue 16 n p.png Ctrl + S Save the script to its corresponding file, prompting for the file if necessary.
Save As* none none Save the script in a file chosen by the user.
Recent* none none Open recently loaded scripts.
Exit none Alt + F4 Close the editor.
Undo Undo2.PNG Ctrl + Z Revert the last change to the code.
Redo Redo2.PNG Ctrl + Y Reverse the last undo.
Cut Cut2.PNG Ctrl + X Cut to the clipboard.
Copy Copy2.PNG Ctrl + C Copy to the clipboard.
Paste Paste2.PNG Ctrl + V Paste from the clipboard.
Find Search.jpg Ctrl + F Find occurrences of text in the script.
Replace Replace.PNG Ctrl + H Replace occurrences text in the script, either selectively or all at once.
Select All none Ctrl + A Select all text in the script.
Increase Indentation Indent.PNG Ctrl + ] Increase the indentation of the selected text (by 4 spaces).
Decrease Indentation Outdent.PNG Ctrl + [ Decrease the indentation of the selected text (by 4 spaces).
Toggle Comment Comment.PNG Ctrl + / Uncomment the selected text if it is completely commented, otherwise comment it.
Run QScript/Apply Rule** Play.PNG F5 Run the QScript (QScript editor) or apply the Rule (Rule editor).

*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 (Green Checkmark 24 n p.PNG) will be displayed in the bottom left-hand corner of the editor. If there were any errors in the script, a cross (Red Delete 16 n p.PNG) 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.

Screenshots

JavaScript Variable Editor

JavascriptVariableEditor.PNG

QScript Editor

QScriptEditor3.PNG

See Also