Formulas
Welcome to the comprehensive guide on using formulas in your documents! This feature empowers you to create dynamic, intelligent forms that automatically calculate values, transform data, and respond to user input in real-time.
Introduction to Formulas
Formulas allow you to create dynamic calculations and automated behaviors in your documents. Instead of manually updating values when data changes, formulas automatically recalculate results based on the current state of your document.
Key Benefits:
- Automate calculations and data transformations
- Ensure consistency across your document
- Create intelligent forms that respond to user input
- Reduce manual work and potential for errors
Getting Started with Basic Formulas
Your First Formula
Let's start with a simple example. Imagine you have a form with two fields: price
and quantity
. You want to automatically calculate the total cost in a third field.
To create this formula:
- Add a new field for the total
- Set its formula to:
price * quantity
That's it! Now whenever the price or quantity changes, the total will automatically update.
Simple Examples
Here are a few basic formulas to get you started:
// Basic arithmetic
price * quantity // Multiplication
subtotal + tax // Addition
total_cost / number_of_items // Division
// Text manipulation
"Hello, " + customer_name // Concatenation
// Simple conditions
if(score > 70, "Pass", "Fail") // Conditional logic
Referencing Fields
The real power of formulas comes from their ability to reference other fields in your document.
Basic Field References
To use a field's value in your formula, simply use its name:
price * quantity
In this example, price
and quantity
are references to other fields in your document.
Field Naming Rules
When referencing fields, remember these rules:
- Field names are case-sensitive
- They must not be wrapped in quotes
- They cannot be reserved words (like
true
,if
,this
) - They must consist of letters, numbers, and underscores
- They cannot start with a number (e.g.,
item1
is valid,1item
is not)
Self-Referencing with this
this
You can reference the current field's value using the this
keyword:
// Limit a number field to a maximum value of 100
if(this > 100, 100, this)
This formula checks if the current value exceeds 100, and if so, caps it at 100.
Working with Different Data Types
Formulas can work with various types of data:
Numbers
Numbers can be integers or decimals:
42 // Integer
3.14159 // Decimal
Number fields support all mathematical operations.
Text (Strings)
Text values must be enclosed in quotes:
"Hello, World!" // Double quotes
'Single quotes work too'
Booleans
Boolean values represent true or false:
true
false
Arrays
Arrays are ordered lists of values:
[1, 2, 3]
["apple", "banana", "cherry"]
[true, false, true]
Objects
Objects are collections of key-value pairs:
{ name: "John", age: 30 }
Using Operators
Operators allow you to perform calculations and comparisons.
Arithmetic Operators
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 6 / 3 | 2 |
% | Modulo (remainder) | 5 % 3 | 2 |
Comparison Operators
Operator | Description | Example | Result |
---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 3 | true |
> | Greater than | 5 > 3 | true |
< | Less than | 3 < 5 | true |
>= | Greater than or equal | 5 >= 5 | true |
<= | Less than or equal | 3 <= 5 | true |
Conditional Logic
Conditional logic allows your formulas to make decisions based on certain conditions.
The if
Function
if
FunctionThe most basic conditional function is if
:
if(condition, value_if_true, value_if_false)
Examples:
// Set shipping cost based on order total
if(order_total > 100, 0, 10)
// Set status based on score
if(score >= 70, "Pass", "Fail")
// Nested conditions
if(score >= 90, "A",
if(score >= 80, "B",
if(score >= 70, "C",
if(score >= 60, "D", "F")
)
)
)
Logical Functions
For more complex conditions:
// AND: All conditions must be true
if(and(age >= 18, has_license == true), "Can drive", "Cannot drive")
// OR: At least one condition must be true
if(or(is_member, total_purchases > 1000), "Apply discount", "Regular price")
// NOT: Invert a condition
if(not(is_weekend), "Open", "Closed")
Working with Text
Text manipulation is a common task in formulas.
Text Functions
Function | Description | Example | Result |
---|---|---|---|
upper | Convert to uppercase | upper("hello") | "HELLO" |
lower | Convert to lowercase | lower("HELLO") | "hello" |
length | Length of string | length("Hello") | 5 |
substring | Extract substring | substring("Hello", 0, 2) | "He" |
trim | Remove whitespace | trim(" Hello ") | "Hello" |
replace | Replace substring | replace("Hello", "H", "J") | "Jello" |
concat | Join two strings together | concat("Order #", order_number, " has been shipped") | "Order #1234 has been shipped" |
Example: Formatting Names
// Format a name as "Last, First"
concat(upper(last_name), ", ", first_name)
Mathematical Operations
Perform calculations with numbers in your formulas.
Basic Math
price * quantity // Multiplication
subtotal + tax // Addition
total - discount // Subtraction
amount / number_of_people // Division
Math Functions
Function | Description | Example | Result |
---|---|---|---|
abs | Absolute value | abs(-5) | 5 |
ceil | Round up to nearest int | ceil(4.2) | 5 |
floor | Round down to nearest int | floor(4.8) | 4 |
round | Round to nearest int | round(4.5) | 5 |
max | Maximum value | max(3, 7) | 7 |
min | Minimum value | min(3, 7) | 3 |
sqrt | Square root | sqrt(16) | 4 |
pow | Power function | pow(2, 3) | 8 |
Example: Calculating Discounts
// Apply a 10% discount if order is over $100
if(order_total > 100, order_total * 0.9, order_total)
// Round a calculation to 2 decimal places
round(price * quantity * (1 - discount_rate), 2)
Date and Time Manipulation
Work with dates in your formulas.
Date Functions
Function | Description | Example | Result |
---|---|---|---|
now | Current date and time | now() | 2025-10-05T14:48:00Z |
dateAdd | Add days to a date | dateAdd(now(), 5) | 2025-10-10T14:48:00Z |
dateSubtract | Subtract days from a date | dateSubtract(now(), 5) | 2025-09-30T14:48:00Z |
formatDate | Format date to string | formatDate(now(), "YYYY-MM-DD") | "2025-10-05" |
Example: Calculating Due Dates
// Set due date to 30 days from today
dateAdd(now(), 30)
// Format a date as MM/DD/YYYY
formatDate(invoice_date, "MM/DD/YYYY")
Working with Arrays and Collections
Arrays allow you to work with collections of values.
Accessing Array Elements
Use dot notation with the index to access array elements (zero-based):
// Get the first item in an array
items.0
// Get the third item in an array
items.2
Array Functions
Function | Description | Example | Result |
---|---|---|---|
length | Length of array | length([1, 2, 3]) | 3 |
map | Transform array elements | map([1, 2, 3], x -> x * 2) | [2, 4, 6] |
filter | Filter array elements | filter([1, 2, 3], x -> x > 1) | [2, 3] |
reduce | Reduce array to single value | reduce([1, 2, 3], (acc, x) -> acc + x, 0) | 6 |
contains | Check if array contains value | contains([1, 2, 3], 2) | true |
countIf | Count elements matching condition | countIf([1, 2, 3, 2], x -> x == 2) | 2 |
some | Check if any element matches condition | some([1, 2, 3], x -> x > 2) | true |
every | Check if all elements match condition | every([2, 2, 2], x -> x == 2) | true |
sum | Sum of array elements | sum([1, 2, 3]) | 6 |
average | Average of array elements | average([1, 2, 3]) | 2 |
count | Count of array elements | count([1, 2, 3]) | 3 |
Example: Working with Collections
// Calculate total from an array of prices
sum(prices)
// Find the highest price
max(prices)
// Count items with quantity > 0
countIf(items, item -> item.quantity > 0)
// Double all values in an array
map(numbers, n -> n * 2)
Advanced Techniques
Working with Objects
Access object properties using dot notation:
// Get a property from an object
customer.name
// Use object properties in conditions
if(customer.level == "Gold", 0.1, 0.05)
Nested Arrays and Objects
You can access deeply nested data:
// Access nested array elements
orders.0.items.1.price
// Access nested object properties
customer.address.city
Complex Transformations
Combine multiple functions for powerful transformations:
// Calculate average of filtered values
average(filter(scores, score -> score > 50))
// Transform and sum values
sum(map(items, item -> item.price * item.quantity))
Troubleshooting and Best Practices
Handling Empty Values
When a formula references a field that is empty (null), it may cause errors. Use the empty()
function to check for empty values:
// Check if a field is empty
if(empty(email), "Please enter email", "Valid")
// Provide default values for calculations
if(empty(price) || empty(quantity), 0, price * quantity)
Formula Debugging Tips
- Start simple: Build your formula step by step
- Test with known values: Verify each part works as expected
- Use parentheses: Clarify the order of operations
- Break down complex formulas: Use intermediate fields for complex calculations
Best Practices
- Keep formulas readable: Break complex formulas into smaller parts
- Use descriptive field names: Makes formulas easier to understand
- Consider edge cases: Handle empty values and unexpected inputs
- Document complex formulas: Add comments to explain your logic
- Test thoroughly: Verify your formulas work with various inputs
Complete Function Reference
Logical Functions
Function | Description | Example | Result |
---|---|---|---|
if | Conditional expression | if(score > 50, "Pass", "Fail") | "Pass" |
and | Logical AND | and(true, false) | false |
or | Logical OR | or(true, false) | true |
not | Logical NOT | not(true) | false |
empty | Check if value is empty | empty(field1) | true/false |
Math Functions
Function | Description | Example | Result |
---|---|---|---|
abs | Absolute value | abs(-5) | 5 |
ceil | Round up to nearest int | ceil(4.2) | 5 |
floor | Round down to nearest int | floor(4.8) | 4 |
round | Round to nearest int | round(4.5) | 5 |
max | Maximum value | max(3, 7) | 7 |
min | Minimum value | min(3, 7) | 3 |
sqrt | Square root | sqrt(16) | 4 |
pow | Power function | pow(2, 3) | 8 |
log | Natural logarithm | log(2.718) | 1 |
exp | Exponential function | exp(1) | 2.718 |
sum | Sum of array elements | sum([1, 2, 3]) | 6 |
average | Average of array elements | average([1, 2, 3]) | 2 |
count | Count of array elements | count([1, 2, 3]) | 3 |
Text Functions
Function | Description | Example | Result |
---|---|---|---|
concat | Concatenate strings | concat("Hello, ", "World!") | "Hello, World!" |
upper | Convert to uppercase | upper("hello") | "HELLO" |
lower | Convert to lowercase | lower("HELLO") | "hello" |
length | Length of string | length("Hello") | 5 |
substring | Extract substring | substring("Hello", 0, 2) | "He" |
trim | Remove whitespace | trim(" Hello ") | "Hello" |
replace | Replace substring | replace("Hello", "H", "J") | "Jello" |
toNumber | Convert to number | toNumber("42") | 42 |
toString | Convert to string | toString(42) | "42" |
Date Functions
Function | Description | Example | Result |
---|---|---|---|
now | Current date and time | now() | 2025-10-05T14:48:00Z |
dateAdd | Add days to a date | dateAdd(now(), 5) | 2025-10-10T14:48:00Z |
dateSubtract | Subtract days from a date | dateSubtract(now(), 5) | 2025-09-30T14:48:00Z |
formatDate | Format date to string | formatDate(now(), "YYYY-MM-DD") | "2025-10-05" |
Array Functions
Function | Description | Example | Result |
---|---|---|---|
length | Length of array | length([1, 2, 3]) | 3 |
map | Transform array elements | map([1, 2, 3], x -> x * 2) | [2, 4, 6] |
filter | Filter array elements | filter([1, 2, 3], x -> x > 1) | [2, 3] |
reduce | Reduce array to single value | reduce([1, 2, 3], (acc, x) -> acc + x, 0) | 6 |
contains | Check if array contains value | contains([1, 2, 3], 2) | true |
countIf | Count elements matching condition | countIf([1, 2, 3, 2], x -> x == 2) | 2 |
some | Check if any element matches condition | some([1, 2, 3], x -> x > 2) | true |
every | Check if all elements match condition | every([2, 2, 2], x -> x == 2) | true |
Updated 12 days ago