Wednesday, 14 September 2016

Quick Notes Tip: Build A Better @Abstract Formula

In certain situations it can be required or necessary to display, for example, the first 120 characters of a (Rich Text) Field in a View Column. For this purpose, assuming the data is text,  it is possible to use, among others, @Abstract (Formula Language).
The basic syntax of the @Abstract Formula is:

@Abstract( [ keywords ] ; size ; beginText ; bodyFields )

size 
Number. The maximum size of the abstracted text. Can be no larger than 64,994 bytes. The number of bytes available for the abstracted text is size - 1; one byte is reserved for internal use.
beginText
Text. A comment to insert at the beginning of the returned text, no larger than 10 characters. The size of beginText counts toward the total size of the abstracted text, but its contents are unaffected by @Abstract commands. Specify an empty string ("") if you do not want a comment.
bodyFields
Text or text list. Any number of fields containing the text to abstract. May be text, rich text, or keyword fields. The text within each field is concatenated with spaces in the order specified. If Notes/Domino cannot locate a field by name, it uses the string literal instead. Enclose each field name in quotes and separate multiple names with colons: "Sales":"Figures".

Common problem with the standard use of @Abstract is that the truncation/clipping, based on the size, can be in the middle of a word.
For this there is a simple solution, to which I was pointed by a colleague, by expanding the @Abstract formula so that the truncation / clipping is based on the last word before the last space.
As an additional option an ellipsis can be added after the last word.
The new @Abstract Formula will look like this:

_ellipsis := " ...";
_length := 120;
_range := 10;

_abstract := @Abstract([TextOnly]; _length; ""; "FIELDNAME");

REM {to make sure we don't truncate/clip in the middle of a word,};
REM {get the text up to the last word before the last space};
REM {and append ellipsis ' ...'};

@If( @Length(_abstract) >= (_length - _range);
@LeftBack(_abstract; " ") + _ellipsis;
_abstract
)

The result is always good an the truncation/clipping is never in de middle of a word.

It is not possible to use @AbstractSimple because this function only use the parameter (bodyFields) and returns the first 100 characters or first 2 paragraphs of text, whichever is smaller. All newline and tab characters are converted into spaces, and all empty paragraphs (containing only a newline character) are ignored. If the parameter is a single field, a text value is returned. If the parameter is a list of field names, then a text list is returned with each list element containing the abstracted text of the corresponding field in the parameter list.
A simple Notes Tip that I always use when I need to display some text from a Rich Text Field in a View Column.

No comments:

Post a Comment