JavaScript: Modify and Describe methods for properties

In JavaScript, you can get and set DataWindow properties with the Describe and Modify methods. Property expressions and DWObject variables are not supported.

These sections describe how to use Modify and Describe in JavaScript:

Advantage and drawbacks of the Modify and Describe methods in JavaScript

In JavaScript, using the Describe and Modify methods to access DataWindow property values has advantages and drawbacks. The examples here use Modify as illustrations, but similar considerations apply to Describe.

Advantage

You can specify column and property names dynamically

In your script, you can build a string that specifies the column and property names.

For example, the following code builds a string in which the default color value and the two color values in the If function are determined in the script. Notice how the single quotes around the expression are included in the first and last pieces of the string:

red_amount = parseInt(text_1.value);
if (red_amount >= 0 and red_amount < 256) {
      modstring = "emp_id.Color='" 
      + text_1.value 
      + "\t If(emp_status=~'A~'," 
      + 255 
      + "," 
      + text_1.value 
      + ")'";
dw_1.Modify(modstring)

The resulting string when red_amount is set to 128 is:

emp_id.Color='128\tIf(emp_status=~'A~',255,128)'

The following is a simpler example without the If function. The Color property for the column specified in ls_columnname is set to a constant value. You do not need quotes around the value if you are not specifying an expression:

dw_1.Modify(ls_columnname + ".Color=255");

Drawbacks

Setting several properties at once is possible but hard to debug

Although you can set several properties in a single method call, it is harder to understand and debug scripts that do so.

For example, the code for setting three properties is not too complex because there are no nested strings:

rtn = dw_1.Modify("emp_id.Font.Italic=0
oval_1.Background.Mode=0
oval_1.Background.Color=255");

Complex quoted strings are sometimes required

When you specify an expression for a property value, it is difficult to specify nested quotes correctly -- the code is hard to understand and prone to error. For Describe, this is less of a drawback -- strings will not become as complex because they do not include an expression.

For example, this string entered on a single line in a script assigns a DataWindow expression to the Color property:

Modify("emp_id.Color=\"16777215 \t
If(emp_status=~~\"A~~\",255,16777215)\"");

For more information about quoted strings, see the section called “Standard datatypes” in PowerScript Reference.

Handling errors for Modify and Describe methods in JavaScript

In all environments, including JavaScript, no runtime error occurs when Describe and Modify try to access invalid controls or properties in the DataWindow object. The validity of the argument string is evaluated before the controls are accessed.

Modify

When the string that specifies the control and property to be accessed is invalid, Modify returns an error string, instead of the expected value, such as:

Line 1 Column 12: incorrect syntax.

You can use the error message to figure out what part of the string is incorrect. This is most useful when you are testing your scripts. The error message, which names the line and column number after which the string was not recognized, may not be helpful after your application is deployed.

Describe

When the string for Describe has an unrecognized property, Describe's return value ends with an exclamation point (!). It will return as many values as it recognizes up to the incorrect one.

When you specify a valid property but that property doesn't have a value (either because it hasn't been set or because its value is an expression that can't be evaluated), Describe returns a question mark (?) for that property. The property's actual value is null.

Always check for errors

You should include error-checking code that checks for these return values. Other errors can occur later if you depend on settings that failed to take effect.

For more information

For more information on syntax and usage, see Describe and Modify in Methods for the DataWindow Control.