I am hoping to make a script to add numbers to paragraphs.
I was wondering if anyone has ran in to this before and would get me headed in the right direction.
If I need such an entity to be enumerated (sorting, boilerplate, etc.), then I consider making these into a table (one paragraph per record).
Sent from miPhone
Are you talking about adding numbers to printed report paragraphs?
If you define a new paragraph as the presence of carriage return then this is relatively straightforward. Here's a potential script snippet:
Set Variable [ $delim; Value: ". " ] //configure how we want to delimit our paragraph numbersSet Variable [ $x; Value: 1 ] //starting countSet Variable [ $y; Value: ValueCount ( $myText ) ] //total number of paragraphsSet Variable [ $myText; Value: $x & $delim & $myText ] //add number to first paragraphLoop
Set Variable [ $x ; Value: $x + 1 ] //increment counter for next paragraph Exit Loop If [ $x > $y ] //exit if counter is greater than number of paragraphs # this next step replaces the "x - 1"th carriage return with a carriage return plus our paragraph number plus our delimiter Set Variable [ $myText; Value: Replace ( $myText; Position ( $myText; "¶"; 1; $x - 1 ); 1; "¶" & $x & $delim ) ]End Loop
There is an error in your code: line 3 valuecounts line 4, i.e. a variable is used before it's defined. Doesn't work in FM ... (there is no syntax error, but the reference is empty)
The other problem of course is that $myText is never initialized with any actual contents, i.e. a text field's contents ...
Apart from that, I think this is easier if you don't treat the first paragraph as something special, and if you take care to only number what should be numbered.
Set Variable [ $myTextRaw ; YourTable::someField ]
Set Variable [ $myText ; Substitute ( $myTextRaw ; "¶¶" ; "§§§¶ ) ]
Set Variable [ $paraCount ; ValueCount ( $myText ) ]
Set Variable [ $delim ; ". " ]
Exit Loop if [ Let ( $i = $i + 1 ; $i > $paraCount ) ]
Set Variable [ $thisPara ; $i & $delim & GetValue ( $myText ; $i ) ]
Set Variable [ $result ; List ( $result ; $thisPara ) ]
Set Variable [ $result ; Substitute ( $result ; "§§§" ; ¶ ) ]
Ser Field [ YourTable::resultField ; $result ]
If you want each ¶ to start a new paragraph and are not opposed to using custom functions, then this is easily handled by CustomList().
CustomList(1; ValueCount(MyText); "[n] & " & Quote(". ") & " & GetValue( MyText ; [n] )")
and MyText = "My first line¶The second line.¶A third line is here."
1. My first line
2. The second line.
3. A third line is here.
Funny, I got a "The specified table could not be found" error when I tried executing your version.
Also, in line two: Set Variable [ $myText ; Substitute ( $myTextRaw ; "¶¶" ; "§§§¶ ) ] I get an error "This text content does not end with a quotation mark." (of course the first one isn't a real error but the second one is a real syntax error)
If you have include tab characters after the number, it's possible to use paragraph formatting on the Inspector's appearance tab to get numbers as hanging indents with the text properly aligned and wrapped within the field or merge text object.
user1069 wrote: of course the first one isn't a real error but the second one is a real syntax error)
of course the first one isn't a real error but the second one is a real syntax error)
Yes, that stuff happens when you type code from scratch on an iPad, rather than copying it (somehow) from a working script.
The first error comes from the fact that there is no field in your file with that fully qualified name, and to correvt the syntax error, just add the missing quote after the pilcrow.
I actually did set up a quick and dirty sample file, where that method worked. Thing you have to watch out for and take care of are more than two CRs in succession.
Here is another way to do it directly in a calculation (without Custom Functions).
theText = MyText; // Your text field takes the place of MyText
theTextRemEndPilcrow = Case(Right(theText;1) = "¶"; Left(MyText; Length(theText) - 1); theText);
theCalc = "Let([$_num = 1]; \"\" ) & " & Substitute(
"\¶\" & Let([$_num=$_num+1]; $_num & \". \" ) & \""
"1. " & Evaluate(theCalc)
Here is another way using a recursive Custom Function.
Note that this lets you specify the format and starting value for the leading number. It is auto-incremented using SerialIncrement().
It is written to have a List as input, but that's what a block of text with ¶ is. Each paragraph would be an entry in the list.
On FileMaker Pro this should work with text containing up to 10,000 paragraphs.
/* NumberTheList(text; firstEnumeration)
Example: NumberTheList("Dog¶Cat¶Pig"; "1. ")
Example: NumberTheList("Dog¶Cat¶Pig"; "001. ")
Specify the value for firstEnumeration to designate the format and spacing of the leading number.
This is incremented using SerialIncrement ( firstEnumeration ; 1 )
The default choice for firstEnumeration is "1. " when firstEnumeration is empty.
listCountText = Case(
IsEmpty(firstEnumeration); "1. ";
) // End Case
ValueCount(text) = 1; listCountText & GetValue(text; 1);
listCountText & GetValue(text; 1) & "¶" &
NumberTheList(RightValues(text; ValueCount(text) - 1); SerialIncrement(firstEnumeration; 1))
) // End Case
) // End Let
Yes that is the end goal.
I like this.
I was thinking about making every page a new record, but the added flexibility of breaking down every paragraph would give me more options down the line.
For adding boilerplate to a list of line items (Quotes, for example), I found it easier to have new "Item" records to link to the paragraphs. Then the report prints so much nicer than with long (multi-paragraph) along with the labor, service or product line items.
Clients loved the flexibility. A sortOrder field helps.
Retrieving data ...