関数からのエラーメッセージと一緒にデータテーブルを出力します(JMP13)以下の私のJSL関数スクリプトは、ネットワークフォルダーからファイルをプルし、他の場所で使用するためにデータファイルに出力します。 「この制限ファイルは存在しません!」というエラーメッセージを出力するにはどうすればよいですか。 データファイルと一緒に「ファイル名を確認してください」。 私はJMP13を使用しています。
Names Default To Here( 1 );
getLimitsFile = Function({LimFileName}, {dtspk},
dtspk = Try(
Open(
"\\gbshyt-ppor-334e\DBX\XPD\System\Limits\"||LimFileName||".txt",
columns(
New Column( "ParameterName", Character, "Nominal" ),
New Column( "ColumnLSL", Character, "Nominal" ),
New Column( "ColumnUSL", Character, "Nominal" ),
New Column( "ColumnUnits", Character, "Nominal" ),
New Column( "ColumnSymb", Character, "Nominal" ),
New Column( "ColumnComm", Character, "Nominal" )
),
Import Settings(
End Of Line( CRLF, CR, LF ),
End Of Field( Tab, Comma, CSV( 1 ) ),
Strip Quotes( 1 ),
Use Apostrophe as Quotation Mark( 0 ),
Use Regional Settings( 0 ),
Scan Whole File( 1 ),
Treat empty columns as numeric( 0 ),
CompressNumericColumns( 0 ),
CompressCharacterColumns( 0 ),
CompressAllowListCheck( 0 ),
Labels( 1 ),
Column Names Start( 1 ),
Data Starts( 2 ),
Lines To Read( "All" ),
Year Rule( "20xx" )
)
//,invisible)
),"This limit file does not exist! Check file name");
return(dtspk);
);また、エラーが発生した場合、関数が使用されているコードのそれ以上の実行を停止し、エラーメッセージを含むダイアログボックスをユーザーに表示するにはどうすればよいですか?
スクリプティングRe:関数からのエラーメッセージと一緒にデータテーブルを出力します(JMP13)ありがとう@jthi 、はい、JMP 13にはファイルが存在します()。 やってみます。
以前、クエリを正しく表現していませんでした。 ファイルが存在しない場合は、ダイアログボックスにエラーを出力して、それ以上の実行を停止するだけです。
ファイルが存在する場合、データテーブルとメッセージ出力のみが必要です。 これを達成する方法は?
Re:関数からのエラーメッセージと一緒にデータテーブルを出力します(JMP13)JMP13がFileExists()関数を提供している場合は、ifステートメントで使用できます。 次に、何を返すか/プログラムを停止する方法を決定します。 たとえば、Throw()またはStop()を使用できます
Names Default To Here(1);
getLimitsFile = Function({LimFileName}, {dtspk},
If(!File Exists("\\gbshyt-ppor-334e\DBX\XPD\System\Limits\"||LimFileName||".txt"),
Throw("This limit file does not exist! Check file name");
//or use return and throw outside function
);
//Open...
return(dtspk);
);
getLimitsFile("");
Show("continue...");
編集:
エラーとともにどのデータテーブルを返しますか? ファイルを開くことができない場合は、空のデータテーブルを作成しない限り、返すデータテーブルはありません。
Re:関数からのエラーメッセージと一緒にデータテーブルを出力します(JMP13)関数を使用するメインコードのダイアログボックスはまだありません。
ユーザーが間違ったファイル名を渡したためにエラーが発生した場合、メインコードでエラーのあるダイアログボックスをユーザーに表示し、メインコードのそれ以上の実行を停止する必要があります。 ファイル名にエラーがない場合、ファイルはネットワークの場所からプルされ、メインコードで使用可能である必要があるデータテーブルに渡されます。 私があなたの質問に答えたことを願っています@jthi 。
Re: 関数からエラーメッセージとともにデータテーブルを出力する (JMP13)確かに、それらで十分なはずです! これは、エラーをどのように処理するかによって、アプリケーション (およびプログラマー/スクリプター) に少し依存します。
以下の例には、これを処理するための 2 つの非常に単純なオプションがあります。 JMP13 が JMP16 と同様に動作する場合、これらはどちらも問題ありません (もちろん、アプリケーションにも依存します)。 違いは、1 つ目は関数内でエラーをスローし、2 つ目はファイル パスに問題があり、結果が関数外で処理される場合に文字列を返すことです。
Names Default To Here(1);
open_file_with_throw = function({file_path}, {default local},
If(!File Exists(file_path), //! inverts the File Exists result
Throw(Eval Insert("^file_path^ file doesn't exist, check file path"));
);
dt = Open(file_path);
return(dt); //return datatable reference
);
open_file_with_throw("testpath");
Show("Shouldn't be executed, due to throw");
//with working file path you will get reference to opened table returned
dt = open_file_with_throw("$SAMPLE_DATA/Big Class.jmp");
open_file_with_return = function({file_path}, {default local},
If(!File Exists(file_path), //! inverts the File Exists result
return(Eval Insert("^file_path^ file doesn't exist, check file path"));
);
dt = Open(file_path);
return(dt); //return datatable reference
);
// this wont error, so you will have to handle error yourself
// one thing I don't like about this solution, is that you will have two different types of returns
// from same function
result = open_file_with_return("testpath");
If(Type(result) == "String",
//Throw(result);
//or you can build the error with modal window and stop after it is closed
//@craige_hales post has quite good template for this type of modals here: https://community.jmp.com/t5/Uncharted/Modal-Dialogs/ba-p/436177
//below is very simple one
New window("Error", << modal,
text box(result)
);
stop();
);
Show("Shouldn't be executed, due to stop");
dt = open_file_with_throw("$SAMPLE_DATA/Big Class.jmp");
Re: 関数からエラーメッセージとともにデータテーブルを出力する (JMP13)ありがとう@jthi . 捕まえる方法投げるダイアログボックスのメッセージ? 以下は動作しないようです。
Names Default To Here(1);
open_file_with_throw = function({file_path}, {default local},
If(!File Exists(file_path), //! inverts the File Exists result
Throw(Eval Insert("^file_path^ file doesn't exist, check file path"));
);
dt = Open(file_path);
return(dt); //return datatable reference
);
result = open_file_with_throw("testpath");
Show(result);
If(Type(result) == "String",
New window("Error", << modal,
text box(result)
);
stop();
);
Re: 関数からエラーメッセージとともにデータテーブルを出力する (JMP13)その場合は、Throw() の代わりに Return() を使用します
Re: 関数からエラーメッセージとともにデータテーブルを出力する (JMP13)必要に応じてキャッチでき、キャッチされない場合はJSLを停止できるため、私はスローを好みます。
open_file_with_throw = Function( {file_path},
{default local},
If( !File Exists( file_path ), //! inverts the File Exists result
Throw( Eval Insert( "^file_path^ file doesn't exist, check file path" ) )
);
dt = Open( file_path );
Return( dt ); //return datatable reference
);
moreJSL = Function( {dt},
Show( N Rows( dt ) );
Close( dt, nosave );
);
errorHandler = Function( {msg}, Show( msg ) );
Write( "\!ncase 1:" );
Try(
dt = open_file_with_throw( "$sample_data/big class.jmp" ),
dt = Empty();
errorHandler( exception_msg );
);
If( !Is Empty( dt ),
moreJSL( dt )
);
Write( "\!ncase 2:" );
Try(
dt = open_file_with_throw( "testpath" ),
dt = Empty();
errorHandler( exception_msg );
);
If( !Is Empty( dt ),
moreJSL( dt )
);
// or even this, but the error handler may be handling exceptions from moreJSL() as well as open_file_with_throw()
Write( "\!ncase 3:" );
Try( moreJSL( open_file_with_throw( "$sample_data/big class.jmp" ) ), errorHandler( exception_msg ) );
Write( "\!ncase 4:" );
Try( moreJSL( open_file_with_throw( "testpath" ) ), errorHandler( exception_msg ) );