• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
      • Introduction to CSPro Language
      • CSPro Program Structure
      • Programming Standards
      • Change Code Properties
      • Code Folding
      • Debugging CSPro Applications
      • Declaration Section
      • Procedural Sections
      • Logic
      • Language Elements
      • JavaScript Use in CSPro
        • Overview
        • JS Namespace
        • JS.eval Function
        • JS.invoke Function
        • JS.getValue Function
        • JS.getValueJson Function
        • JS.hasValue Function
        • JS.setValue Function
        • JS.setValueFromJson Function
        • JavaScript User-Defined Functions
        • CSPro ⇄ JavaScript Value Conversions
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
    • Data Sources
    • CSPro Statements and Functions
    • Text Templates
    • Templated Reporting System
    • HTML, Markdown, and JavaScript Integration
    • Action Invoker
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataManager>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

JS Namespace

The JS namespace is a set of functions that facilitate interactions between CSPro logic and JavaScript. For detailed information about using JavaScript in CSPro, see: JavaScript Use in CSPro.
Functionality
FunctionDescription
JS.evalEvaluates JavaScript in the global execution context.
JS.getValueConverts a JavaScript variable or property to its CSPro equivalent.
JS.getValueJsonReturns the JSON representation of a JavaScript variable or property.
JS.hasValueReturns whether a JavaScript variable or property is defined (not undefined).
JS.invokeExecutes a JavaScript function that exists in the global execution context with any CSPro arguments converted to their JavaScript equivalents.
JS.setValueConverts a CSPro variable to its JavaScript equivalent and adds this as a variable to the global scope, or sets an object's property.
JS.setValueFromJsonCreate a JavaScript variable using JSON and adds it as a variable to the global scope, or sets an object's property.
When executing JavaScript from CSPro logic, any uncaught exceptions will be displayed as error messages.
Example 1
JS.eval("const artist = 'Roberta Flack';"); // undefined
JS.eval("artist.toUpperCase();");           // ROBERTA FLACK
JS.eval("[ ...artist ];");                  // R,o,b,e,r,t,a, ,F,l,a,c,k
Example 2
With the following JavaScript script added to the application...
function sumArray(values) {
   
let sum = 0;
   
// use forEach to sum the values of an array
    values.forEach(value => {
        sum += value;
    });
   
return sum;
}
...it is possible to pass Array or List objects due to CSPro to JavaScript conversions:
Array someNumbersArray(4) = 10, 20, 30, 40;
List someNumbersList = 4, 6, 7, 79, 81, 82;

JS.invoke("sumArray", someNumbersArray); // 100
JS.invoke("sumArray", someNumbersList);  // 259
See also: JavaScript Use in CSPro