EZ Exporter Calculated Fields - Text Operations

Text operations let you manipulate text fields in your report.

AVAILABLE OPERATORS

  • +, Combines text (e.g. {{ text_field }} + "hello", {{ text_field1 }} + {{ text_field2 }}).
  • [], Slice part of the text (gives the character from a given index, e.g. if the value of a text_field is "hello", {{ text_field }}[1] will give "e", note that the index count starts at 0).
  • [:], Range slice (gives the characters from a given range, e.g. if the value of text_field is "hello", {{ text_field }}[1:4] will give "ell", note that the index count starts at 0 and the ending index is exclusive. You can also count backwards, for example {{ text_field }}[-3:] will give the last 3 characters "llo").
  • {{ text_field }}.replace("text_to_replace", "new_text"), Replace part of the text with new text. If you need to do multiple replacements, we recommend using the replace_multiple_characters() function as it executes much faster.
  • {{ text_field }}.upper(), Converts the entire text to uppercase.
  • {{ text_field }}.lower(), Converts the entire text to lowercase.
  • {{ text_field }}.title(), Capitalize the first letter of each word (e.g. don quixote -> Don Quixote).
  • {{ text_field }}.split(","), Splits the text by the comma separator (you can specify a different separator).
  • {{ text_field }}.strip(), Removes leading and trailing characters (by default, it will remove leading and trailing space characters, you can specify the characters inside the function like strip("abc")).
  • {{ text_field }}.zfill(number_of_characters), Zero fill/padding. Adds zeros at the beginning of the string, until it reaches the specified length. (e.g. if the value of a text_field is 10012, {{ text_field }}.zfill(7) will output 0010012).
  • str({{ number_field }}).replace(".", ","), Converts a number field into text/string so you can perform text operations on it.

EXAMPLES

Remove parentheses and dashes in the shipping phone number.

{{ shipping_address.phone }}.replace("(", "").replace(")", "").replace("-", "")

Note: If you need to use .replace() for more than 3 characters, we recommend using the replace_multiple_characters() function instead as it executes much faster.

I want to convert the product title to all capital/uppercase letters.

{{ line_items.title }}.upper()

I want to convert the product title to all lowercase letters.

{{ line_items.title }}.lower()

I want to caplitalize the first letter of each word in the product title.

{{ line_items.title }}.title()

I want a field of just the first five letters of the SKU (note that the index starts at 0) and add the text "snipped" to it.

{{ line_items.sku }}[0:5] + "snipped"

Remove all leading and trailing spaces from the data.

{{ line_items.title }}.strip()

To remove leading and trailing dashes instead:

{{ line_items.title }}.strip("-")

Split the text by the "-" character and return the first element, removing leading and trailing spaces at the same time.

If the product title is: "Red Shorts - Special Edition - Extra Large"

The formula:

{{ title }}.split("-")[0].strip()

Will return: "Red Shorts"

To return the 3rd element instead after splitting the text:

{{ title }}.split("-")[2].strip()

Will return: "Extra Large"

Only export the first 10 characters from the data.

If the value of the title field is: "Super Accurate Block Clock"

The formula:

{{ title }}[0:10]

Will return: "Super Accu"

Note that spaces count as characters.

Only export the last 10 characters from the data.

If the value of the title field is: "Super Accurate Block Clock"

The formula:

{{ title }}[-10:]

Will return: "lock Clock"

Note that spaces count as characters.

Pad the order_number field with zeros so it's 10 characters long.

If the value of the order_number field is: "1001"

The formula:

str({{ order_number }}).zfill(10)

Will output: "0000001001"

Note here that since the order_number field is a number data type, we had first to coerce it to a string data type before we can apply the zfill() method.

NOTES

Please note that these can only be applied to "text-type" fields. If you'd like to use text operations on "number-type" fields such as IDs and prices, you'll need to convert them to text first by simply enclosing the variables with quotes like this:

"{{ customer.id }}"[1:5]

Or use the str() function like this:

str({{ customer.id }})[1:5]

Related Articles:


App: EZ Exporter

Tags: advanced features