Like Excel, most FormCalc scripts are only one line long. The following code sets the rawValue property of TextField1 to the sum of (1,2,3,4), which is 10:
form1::ready:form - (FormCalc, client) ---------------- TextField1.rawValue = Sum(1,2,3,4)
Date and time functions
FormCalc has many functions that are ideal for the common tasks that interactive forms require. For instance, the following script sets a Date/Time Field to the current date:
form1::ready:form - (FormCalc, client) ---------------- // Today's Date in long-style date format. DateTimeField1.rawValue = num2date(date(), DateFmt(3))
If you are working on financial forms, FormCalc has many built-in financial functions that you can add to your form. For instance, it is easy to calculate the APR (annual percentage rate) of a loan with FormCalc. In the following example, the principal amount of the loan is $35,000, the monthly payment amount is $269.50, and the total amount of months is 360. This script returns an annual percentage rate of 0.08515404566 to TextField1:
form1::ready:form - (FormCalc, client) ---------------- TextField1.rawValue = Apr(35000, 269.50, 360)
Adding FormCalc to your forms
To see FormCalc in action, open a blank form and add a Text Field and a Button object. Follow these steps to add a FormCalc script to each of your objects.
- Select the Text Field object and select the form:ready event in your Script Editor (Figure 4.2).
Figure 4.2 Inserting a built-in FormCalc function into your script.
- Click on the script editing field to create a blinking "I" beam. While in this mode, select the Functions button and select the Concat(s1[,s2...]) function (Figure 4.2). Double-clicking this item inserts the Concat function into your script.
- Complete your script by adding a reference to your Text Field object's rawValue property:
form1::ready:form - (FormCalc, client) ---------------- TextField1.rawValue = Concat("Live", "Cycle")The Concat function in FormCalc returns the concatenation of two or more strings. In this example, the Concat function joins the string "Live" with the string "Cycle" to form the word "LiveCycle." This new value is assigned to the rawValue property of the text field. You can use string functions like Concat in your forms to join a user's first name with his last name to create a full name value.
- Select the Button object and add the following code to the click event:
This chapter will cover more about the host object and the other object models in XFA forms. If you are interested in exploring FormCalc further, Adobe has included excellent reference materials in the Help section including the "FormCalc User Reference," which you can find under the Scripting section of the Help System.