輸出資料表以及來自功能的錯誤訊息 (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);
);另外,如果發生錯誤,如何停止進一步執行使用該函數的程式碼並向使用者顯示包含錯誤訊息的對話框?
自動化和腳本編寫回覆:從函數(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 ) );回覆:從函數(JMP13)輸出資料表和錯誤訊息在這種情況下,請使用 Return() 而不是 Throw()
回覆:從函數(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();
);
回覆:從函數(JMP13)輸出資料表和錯誤訊息當然,這些應該就夠了! 這將有點依賴應用程式(以及程式設計師/腳本編寫者)您想要如何處理錯誤。
下面的範例有兩個相當簡單的選項,您可以如何處理這個問題。 如果 JMP13 的行為與 JMP16 類似,則兩者都應該沒問題(當然這也取決於您的應用程式)。 區別在於,第一個將在函數內部拋出錯誤,第二個將在檔案路徑存在問題時返回字串,並且結果在函數外部處理。
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");
回覆:從函數(JMP13)輸出資料表和錯誤訊息我還沒有關於使用該函數的主要程式碼的對話框。
當由於使用者傳遞錯誤的檔案名稱而出現錯誤時,我希望主程式碼向使用者顯示一個包含錯誤的對話框,並停止主程式碼的任何進一步執行。 當檔案名稱沒有錯誤時,將從網路位置提取檔案並將其傳遞到主程式碼中需要可用的資料表中。 希望我已經回答了你的問題@jthi 。
回覆:從函數(JMP13)輸出資料表和錯誤訊息您在函數之外是否有一個對話框要在其中填充錯誤訊息? 對於數據表,您想使用數據表返回訊息輸出嗎?
回覆:從函數(JMP13)輸出資料表和錯誤訊息謝謝@jthi ,是的,JMP 13確實有File Exists ()。 我會嘗試。
我之前沒有正確表達我的查詢。 當文件不存在時,我只希望該函數在對話框中輸出錯誤並停止進一步執行。
當文件存在時,我只想要數據表和訊息輸出。 如何實現這項目標?
回覆:從函數(JMP13)輸出資料表和錯誤訊息如果 JMP13 提供 File Exists() 函數,您可以將其與 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...");
編輯:
您希望傳回哪個資料表並出現錯誤? 如果無法開啟該文件,則除非您建立空資料表,否則不會傳回資料表。