7(重) JavaScript -dialog.ktr(需要調用Java代碼,有界面)
// This JavaScript asks for a start and ending date with text dialog boxes. // It is a proof of concept if dialogs could be used out of JavaScript within transformations. // This could be a base for discussion if a general input step dialog should be created. // It runs within Spoon, Pan and launched by the "Test script" button within the JavaScript editor. // @author Jens Bleuel // @since 2006-07-11 // to get the "Test script" button at the editor working correctly we need the existing Display / Shell var display; var displayHasToBeDisposed=false; var shell=null; try { display=Packages.org.eclipse.swt.widgets.Display.getCurrent(); shell=display.getActiveShell(); } catch(e) { // if it runs in batch mode (Pan or preview mode) no Display is available, so we have to create one display=new Packages.org.eclipse.swt.widgets.Display(); displayHasToBeDisposed=true; shell=new Packages.org.eclipse.swt.widgets.Shell(display); } // if we run in Pan we need to load the properties: if(!Packages.org.pentaho.di.ui.core.PropsUI.isInitialized()) { Packages.org.pentaho.di.ui.core.PropsUI.init(display,2); //2=TYPE_PROPERTIES_PAN } var dateDefaultFrom=DateFromProposal.getString().substr(0,10);//only the date and not the time var dialogDateFrom=newPackages.org.pentaho.di.ui.core.dialog.EnterTextDialog(shell,"Date from", "Please enter the beginning date", dateDefaultFrom); var dateFromAsString=dialogDateFrom.open(); if(dateFromAsString!=null&& dateFromAsString.length()>0) { var dateDefaultTo=DateToProposal.getString().substr(0,10);//only the date and not the time; var dialogDateTo=newPackages.org.pentaho.di.ui.core.dialog.EnterTextDialog(shell,"Date to", "Please enter the ending date", dateDefaultTo); var dateToAsString=dialogDateTo.open(); if(dateToAsString!=null&& dateToAsString.length()>0) { // here you could check or change formats a.s.o } else { // stop transformation when user cancels throw new Packages.java.lang.RuntimeException("Input canceled by the user."); } } else { // stop transformation when user cancels throw new Packages.java.lang.RuntimeException("Input canceled by the user."); } if(displayHasToBeDisposed) { display.dispose(); } |
文本檔案輸出
[email protected]@[email protected] 2006/01/01 00:00:[email protected]/12/31 00:00:[email protected]/01/[email protected]/12/31 |
8 JavaScript - extract date fromfilename.ktr(從檔案名中提取日期,字元串處理)
// C:\temp\LIM_kettle\ST18.PROTOKL.TENOPROD_2006*07_21_00_09 // rightstr(16),截取右邊16個字元 var dat = DIR.Clone().rightstr(16).str2dat("yyyy*MM_dd_HH_mm"); |
文本檔案輸出:
DIR@@@dat C:\temp\LIM_kettle\ST18.PROTOKL.TENOPROD_2006*07_21_00_09@@@2006/07/21 00:09:00.000 |
9 JavaScript - Get the filenamefrom a path.ktr(從路徑中提取檔案名)
var strArray = File.getString().split("/");//數組 var last = strArray[strArray.length-1];//數組中的最後一個即檔案名 |
文本檔案輸出
File@@@@last C:/Test/file.txt@@@@file.txt |
10.JavaScript - Hex to Integer conversion.ktr(16進制->10進制)
java; //不寫木發現影響,僅限本例 //16進制->10 var nummerke = Packages.java.lang.Integer.valueOf(Hex.getString(), 16).intValue(); //Alert(nummerke); |
文本檔案輸出:
Hex;nummerke DEAD; 0057005 |
11 JavaScript - parse Debet-Credit prefix innumbers.ktr(數值中有字首)
var first = amountField.Clone().substr(0,1).getString(); var amount = amountField.Clone().substr(1).str2num("###.00",".").getNumber(); if (first.equals("D")) { amount = -amount; } |
文本檔案輸出:
amountField@@@first@@@amount C100.00 @@@C@@@00100.00 D100.00 @@@D@@@-00100.00 |