kettle转换中使用javascript例子整理(2)

时间:2022-12-29 11:34:41

7(重) JavaScript -dialog.ktr(需要调用Java代码,有界面)

kettle转换中使用javascript例子整理(2)

kettle转换中使用javascript例子整理(2)

kettle转换中使用javascript例子整理(2)

// 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();

}

文本文件输出

DateFromProposal@DateToProposal@dateFromAsString@dateToAsString

2006/01/01 00:00:00.000@2006/12/31 00:00:00.000@2014/01/02@2014/12/31

8 JavaScript - extract date fromfilename.ktr(从文件名中提取日期,字符串处理)

kettle转换中使用javascript例子整理(2)

kettle转换中使用javascript例子整理(2)kettle转换中使用javascript例子整理(2)

// 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(从路径中提取文件名)

kettle转换中使用javascript例子整理(2)

kettle转换中使用javascript例子整理(2)

kettle转换中使用javascript例子整理(2)

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进制)

 kettle转换中使用javascript例子整理(2)

kettle转换中使用javascript例子整理(2)kettle转换中使用javascript例子整理(2)

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(数值中有前缀)

kettle转换中使用javascript例子整理(2)

kettle转换中使用javascript例子整理(2)kettle转换中使用javascript例子整理(2)kettle转换中使用javascript例子整理(2)



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