Jump to content

Formatting strings in rules: Difference between revisions

From Resco's Wiki
No edit summary
Marek Rodak (talk | contribs)
No edit summary
 
(10 intermediate revisions by one other user not shown)
Line 1: Line 1:
One of the available [[Rules_editor#Operations|operations]] in [[Rules editor]] when building a function step is '''Format'''. The purpose of this operation is to convert a string or a series of strings to a custom format.
One of the available [[Rules_editor#Operations|operations]] in [[Rules editor]] when building a function step is '''Format'''. The purpose of this operation is to convert a string or a series of strings to a custom format.


This operation functions similarly to String.Format method in .NET Framework:
This operation functions similarly to the .NET composite formatting feature:
: ''The (...) feature takes a list of objects and a composite format string as input. A composite format string consists of fixed text intermixed with indexed placeholders, called format items, that correspond to the objects in the list. The formatting operation yields a result string that consists of the original fixed text intermixed with the string representation of the objects in the list.'' <sup>([https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting Microsoft documentation])</sup>
: ''The (...) feature takes a list of objects and a composite format string as input. A composite format string consists of fixed text intermixed with indexed placeholders, called format items, that correspond to the objects in the list. The formatting operation yields a result string that consists of the original fixed text intermixed with the string representation of the objects in the list.'' <sup>([https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting Microsoft documentation])</sup>
Additional related operations include:
* '''Format Text''' - Returns a formatted string that can contain references to entity fields using the [[dot notation]] - {entity.field:format} ([[Formatting_strings_in_rules#Format_Text_example|Example]])
* '''Format Single''' - Returns a formatted string with lookup target information. This operation uses two arguments. The first is a text with placeholders: {1} for entity ID, {2} for entity name. The second argument is a lookup field. [https://www.youtube.com/watch?v=hn3lsFl3o9I&t=1518s More info] {{Badge|Webinar}}


== Arguments ==
== Arguments ==


The Format operation in Rules editor has two arguments (parameters). The first argument describes how the second argument should be formatted.
The Format operation in Rules editor has two arguments (parameters). The first argument describes how the second argument should be formatted.
* Arg1: Text - composite format string
* Arg1: Text = composite format string
* Arg2: StringList - format items
* Arg2: StringList = format items


In Microsoft terminology, the first argument is '''composite format string''' <sup>([https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting#composite-format-string Microsoft documentation])</sup>. Essentially, it says how the result should look, with placeholders for strings from the second argument. The placeholders are numbers in curly brackets that correspond to the index of a format item in argument 2. The first format item has the index <code>{0}</code>. The same format item can be used multiple times in a single composite format string.
The first argument is '''composite format string''' <sup>([https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting#composite-format-string Microsoft documentation])</sup>. Essentially, it says how the result should look, with placeholders for strings from the second argument.
* The placeholders are numbers in curly brackets that correspond to the index of a format item in argument 2.
* The first format item has the index <code>{0}</code>.
* The same format item can be used multiple times in a single composite format string.
* Each format item can be further customized. For example, you can return only a date portion of a datetime variable <code>{0:d}</code>, you can apply currency formatting to a number <code>{0:C2}</code>, and much more.


The second argument is a [[Rules_editor#StringList_type|StringList]] type of variable, a series or array of strings (or '''format items''' in Microsoft terminology).
The second argument is a [[Rules_editor#StringList_type|StringList]] type of variable, a series or array of strings (or '''format items''').


== Examples ==
== Format examples ==


=== Form button that fills an email address ===
=== Form button that fills an email address ===
Line 20: Line 28:
Add a button to your form. Add a '''Button Click''' rule that builds your email address from first name, last name, and a static suffix.
Add a button to your form. Add a '''Button Click''' rule that builds your email address from first name, last name, and a static suffix.


[[File:Format string list example.png|600px]]
[[File:Format string list example.png|alt=Formatting strings in rules: Format string list example|600px]]


This is how it looks in the app:
This is how it looks in the app:


[[File:Set email example.png|300px]]
[[File:Set email example.png|alt=Formatting strings in rules: Set email example|300px]]


=== More composite format string examples ===
=== More composite format string examples ===
Line 36: Line 44:
|  <code>The price of {0} is {1:C2}.</code>  || product name, price || The price of Car Door Hanging Plate is $89.
|  <code>The price of {0} is {1:C2}.</code>  || product name, price || The price of Car Door Hanging Plate is $89.
|}
|}
== Format Text example ==
In this example, add a button that concatenates multiple entity fields (with formatting) into a single sentence.
;Rule
:[[File:Format text example.png|alt=Formatting strings in rules: Format text example|600px]]
:Here's the important part: "Format Text" in action.
:<code>The property {Entity.name}, first offered in {Entity.createdon:MMMM yyyy}, located in {Entity.address_city} ({Entity.address_country}) is available from {Entity.seller} for {Entity.price:C}.</code>
;Result in the app
:[[File:Real estate details in a sentence.png|alt=Formatting strings in rules: Real estate details in a sentence]]


== See also ==
== See also ==
* [[Report_Designer#Formatting|Formatting strings in Report Designer]]
* https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting
* https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting
* https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
* https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings
* https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings
* https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings


[[Category:Woodford]]
[[Category:Business logic]]

Latest revision as of 09:09, 16 November 2023

One of the available operations in Rules editor when building a function step is Format. The purpose of this operation is to convert a string or a series of strings to a custom format.

This operation functions similarly to the .NET composite formatting feature:

The (...) feature takes a list of objects and a composite format string as input. A composite format string consists of fixed text intermixed with indexed placeholders, called format items, that correspond to the objects in the list. The formatting operation yields a result string that consists of the original fixed text intermixed with the string representation of the objects in the list. (Microsoft documentation)

Additional related operations include:

  • Format Text - Returns a formatted string that can contain references to entity fields using the dot notation - {entity.field:format} (Example)
  • Format Single - Returns a formatted string with lookup target information. This operation uses two arguments. The first is a text with placeholders: {1} for entity ID, {2} for entity name. The second argument is a lookup field. More info Webinar

Arguments

The Format operation in Rules editor has two arguments (parameters). The first argument describes how the second argument should be formatted.

  • Arg1: Text = composite format string
  • Arg2: StringList = format items

The first argument is composite format string (Microsoft documentation). Essentially, it says how the result should look, with placeholders for strings from the second argument.

  • The placeholders are numbers in curly brackets that correspond to the index of a format item in argument 2.
  • The first format item has the index {0}.
  • The same format item can be used multiple times in a single composite format string.
  • Each format item can be further customized. For example, you can return only a date portion of a datetime variable {0:d}, you can apply currency formatting to a number {0:C2}, and much more.

The second argument is a StringList type of variable, a series or array of strings (or format items).

Format examples

Form button that fills an email address

Add a button to your form. Add a Button Click rule that builds your email address from first name, last name, and a static suffix.

Formatting strings in rules: Format string list example

This is how it looks in the app:

Formatting strings in rules: Set email example

More composite format string examples

Argument 1 Argument 2 Result
Let's meet on {0:d} at {0:t}. DateTime field Let's meet on 4/10/2015 at 9:00 AM.
The price of {0} is {1:C2}. product name, price The price of Car Door Hanging Plate is $89.

Format Text example

In this example, add a button that concatenates multiple entity fields (with formatting) into a single sentence.

Rule
Formatting strings in rules: Format text example
Here's the important part: "Format Text" in action.
The property {Entity.name}, first offered in {Entity.createdon:MMMM yyyy}, located in {Entity.address_city} ({Entity.address_country}) is available from {Entity.seller} for {Entity.price:C}.
Result in the app
Formatting strings in rules: Real estate details in a sentence

See also