Resources[Tips]
【連載:Tips_for_FM7】
─第2回:HTML自動生成などに便利な「FieldSubstitute関数」

茂田カツノリ
text by Katsunori Shigeta
■FileMaker 7のカスタム関数はすごい
FileMaker 7/8のメリットは多数あるが、僕のようにHTML生成などのツールとして活用してる場合、あえて特筆したいものがある。それが「FieldSubstitute関数」だ。これは標準で装備されているのではなく、FileMaker 7/8から新設された「カスタム関数」機能を使って作られたもので、作者/改変者としてMike Hackettさん/Bruce Robertsonさんの名が記されている。
・ソース(コメント等も含め改変しなければ配布可能)
// FieldSubstitute(text) // By Mike Hackett // 8-23-2004 // modifed 9-18-04 Bruce Robertson // This takes a text field with user entered merge fields // in the form <<Field Name>> // and replaces the merge field names with the field values. // It also evaluates inline functions
Let( [ l = Length(text); p1 = Position(text;"<<";1;1); p2 = Position(text;">>";p1;1); fieldText = Middle(text;p1+2;p2-(p1+2))];
If(p1 * p2; //If both sets of brackets exist Left(text;p1-1) &
If( PatternCount(FieldText;"S4HU_EventScript(" ) or IsValid(Evaluate(fieldText) );
Evaluate(fieldText); //Evaluate inline function
"<Field Missing: \"" & fieldText & "\">") & //field does not exist
FieldSubstitute(Right(text;l-(p2+1)));
text//no matching brackets
)//if )//let
・サンプルファイルはこちら
■使い方
FileMaker Developer 7をお持ちなら、[ファイル]→[定義]→[カスタム関数]に、上記の式をコピー&ペーストすればよい。このとき、関数名は「FieldSubstitute」、関数の引数は「text」と指定する。
もしFileMaker Pro 7しかお持ちでなくても、安心してほしい。上記サンプルファイルにはFieldSubstitute関数が入っているので、このファイルを改変する形であれば利用が可能だ。
使い方は、とても簡単だ。サンプルファイルには以下のフィールドが定義されている。
・template テキストフィールド(グローバル)
・結果 計算フィールド[式は「FieldSubstitute(template)」]
・会社名 テキストフィールド
・氏名 テキストフィールド
templateに任意の文やHTMLコードを入力しておくのだが、その中で「<<会社名>>」や「<<氏名>>」と記述しておくと、その部分がフィールドに入力されている値に差し変わる、という動作になる。引数が1つだけというごくごくシンプルな動作ながら、これはなかなかに実用性が高い。
(つづく)
|