Kettle_js指令碼_示例
Kettle 版本7.0
一,String function
1 .date2str
//js操作時間
var dValue = new Date();
//把時間變成字串
var dValue1 = date2str(dValue);
//把時間變成當前格式字串
var dValue2 = date2str(dValue,"dd.MM.yyyy");
//把時間變成當前格式字串
var dValue3 = date2str(dValue,"dd.MM.yyyy HH:mm:ss");
//把時間變成當前格式字串 + 語言
var dValue4 = date2str(dValue,"E.MMM.yyyy","DE");
//把時間變成當前格式字串 + 語言
var dValue5 = date2str(dValue,"dd.MM.yyyy HH:mm:ss","EN");
//把時間變成當前格式字串 + 語言 + 時區
var dValue6 = date2str(dValue,"dd.MM.yyyy HH:mm:ss","EN", "EST");
var str = dValue1 + "\n" + dValue2 + "\n" + dValue3 + "\n" + dValue4 + "\n" + dValue5 + "\n" + dValue6;
Alert(str);
2.escapeStr1
unEscapeHtml
//js加密解密1
var escapeStr = "<html></html>";
//編碼字串
var escapeStr1 = escapeHtml(str);
//解碼字串
var escapeStr2 = unEscapeHtml(escapeStr1);
Alert(escapeStr1 + "\n" + escapeStr2);
3.escapeXml
unEscapeXml
//js加密解密2
var escapeXmlStr = "<html></html>" ;
//編碼字串
var escapeXmlStr1 = escapeXml(escapeXmlStr);
//解碼字串
var escapeXmlStr2 = unEscapeXml(escapeXmlStr1);
Alert(escapeXmlStr1 + "\n" + escapeXmlStr2);
4.escapeSQL
//js轉換SQL,把單引號變成雙引號
var escapeSQLStr = "a'b'c"
var escapeSQLStr1 = escapeSQL(escapeSQLStr);
Alert(escapeSQLStr1);
5.fillString
//把這個 char複製10遍
Alert(fillString("a",10));
6.getOcuranceString
//判斷這個字串在另外一個字串有多少個
Alert(getOcuranceString("abcakldhiasdku","h"));
7.indexOf
//搜尋字串
var str1= "Hello Pentaho!";
//搜尋某個指定的字串值在字串中首次出現的位置
var str2= indexOf(str1, "Pentaho");
//搜尋某個指定的字串值在字串第7個之後中首次出現的位置
var str3= indexOf(str1, "o", 7);
Alert(str2 + "\n" +str3);
8.initCap
//返回經過處理的字串 -My Home
var str1 = "my home";
Alert(initCap(str1));
9.lower
//把字串的大寫變成小寫
var str1= "Hello World!";
var str2= lower(str1);
Alert("Input:" + str1);
Alert("Converted to LowerCase:" + str2);
10.upper
//把字串變成大寫
var str1= "Hello World!";
var str2= upper(str1);
Alert("Input:" + str1);
Alert("Converted to UpperCase:" + str2);
11.lpad
//判斷字串是否=>13,如果不夠13,則在前面補足x至13個
var str1= "Hello World!";
Alert("Input:" + str1);
Alert("Lpad:" + lpad(str1, "x",13));
12.rpad
//判斷字串是否=>13,如果不夠13,則在後面補足x至13個
var str1= "Hello World!";
Alert("Input:" + str1);
Alert("Rpad:" + rpad(str1, "x",13));
13.ltrim
Rtrim
trim
//去除字串空格
var str1= " Hello World! ";
Alert("Input:" + str1+" "+str1.length);
//去除左邊字串
Alert("Ltrim:" + ltrim(str1)+" "+ltrim(str1).length);
//去除右邊字串
Alert("Rtrim:" + rtrim(str1)+" "+rtrim(str1).length);
//去除全部字串空格
Alert("Trim:" + trim(str1)+" "+trim(str1).length);
14.num2str
//把數字變成字串
var d1 = 123.40;
var d2 = -123.40;
//把數字變成字串
Alert(num2str(d2));
//把字串變成指定格式的字串
Alert(num2str(d1, "0.00"));
//把字串變成指定格式的字串+區域
Alert(num2str(d1, "0.00", "EN"));
//把字串變成指定格式的字串+區域
Alert(num2str(d2, "0.00;(0.00)", "EN"));
15.protectXMLCDATA
//) 在XML中,指定某段內容不必被XML解析器解析時,使用<![CDATA[...]]>。也就是說中括號中的內容解析器不會去分析。所以其中可以包含>, <, &, ', "這5個特殊字元。經常把一段程式程式碼嵌入到<![DATA[...]]>中。 因為程式碼中可能包含大量的 >, <, &, "這樣的特殊字元。
var str1 = "my home";
Alert(protectXMLCDATA(str1));
16.removeCRLF
//去除字串裡面的回車 \n\r字元
var removeCRLFStr = "abc"+"\n"+"\r"+"abc";
Alert(removeCRLF(removeCRLFStr));
17.replace
//轉換字串
var str1 = "Hello World, this is a nice function";
//把字串的World轉換成 Folk
var str2 = replace(str1,"World", "Folk");
Alert(str2);
//把字串的World轉換成 Folk nice 轉換成 beautifull
var str2 = replace(str1,"World", "Folk", "nice","beautifull");
Alert(str2);
18.str2RegExp
//跟據指定字元切割字串
var strToMatch = "[email protected]";
var strReg = "^(\\w+)@([a-zA-Z_]+?)\\.([a-zA-Z]{2,3})$";
var xArr = str2RegExp(strToMatch, strReg);
if ( xArr != null ) {
for(i=0;i<xArr.length;i++) {
Alert(xArr[i]);
}
}
else {
Alert("no match");
}
19.substr
//擷取字串
var str1= "0123456789";
//擷取前面6個字串,返回6789
var str2= substr(str1, 6);
//擷取8個字串,從第2個開始,返回23456789
var str3 = substr(str1,2,8)
Alert(str2);
Alert(str3);
二,numeric function
1.abs
//返回絕對值
var d1 = -1234.01;
var d2 = 1234.01;
Alert(abs(d1));
Alert(abs(d2));
2.ceil
//返回相反數
var d1 = -1234.01;
var d2 = 1234.01;
Alert(ceil(d1));
Alert(ceil(d2));
3.floor
//返回該數值的最大整數
var d1 = -1234.01;
var d2 = 1234.01;
Alert(floor(d1));
Alert(floor(d2));
4.str2num
//把字串轉成數字
var str1 = "1.234,56";
var str2 = "12";
//把字串按照指定的格式轉換成數字
Alert((str2num(str1,"#,##0.00")));
Alert((str2num(str2)));
5.trunc
//四捨五入取整,只能傳double
Alert(trunc(new Date()));
三,Date Function
1.dateAdd
//再Date時間上增加
// y - Years
// m - Months
// d - Days
// w - Weeks
// wd - Workingdays
// hh - Hours
// mi - Minutes
// ss - Seconds
var d1 = new Date();
Alert(dateAdd(d1, "y", 1));
Alert(dateAdd(d1, "m", 4));
Alert(dateAdd(d1, "d", 5));
Alert(dateAdd(d1, "wd", 5));
2.dateDiff
//返回倆個時間型別之間的差異
var d1 = new Date();
var d2 = dateAdd(d1,"y",1);
Alert(dateDiff(d1,d2,"y"));
Alert(dateDiff(d1,d2,"m"));
Alert(dateDiff(d1,d2,"d"));
Alert(dateDiff(d1,d2,"wd"));
3.getDayNumber
//返回一個數值,對應一個格式
var d1 = new Date();
Alert(d1);
//這一年的第幾天
Alert(getDayNumber(d1, "y"));
//這個月的第幾天
Alert(getDayNumber(d1, "m"));
//這個星期的第幾天
Alert(getDayNumber(d1, "w"));
//這個月的第幾個星期
Alert(getDayNumber(d1, "wm"));
4.getFiscalDate
//返回財政日期的日期值,基於給定的偏移量。格式 dd.MM.(暫時不知道怎麼用)
var d1 = new Date();
var str1 = "01.00.";
var str2 = "00.01.";
Alert(getFiscalDate(d1, str1));
Alert(getFiscalDate(d1, str2));
5.getNextWorkingDay
//返回下一個工作日,星期4-星期5,星期5-星期1
var d = new Date();
var d1 = getNextWorkingDay(d);
Alert(getNextWorkingDay(d1));
6.month
//返回當前月份0-11
var d1 = new Date();
Alert(month(d1));
7.quarter
//返回當前季度 1-4
var d1 = new Date();
Alert(quarter(d1));
8.week
//返回當前是這一年的第幾個星期year(var)
var d1 = new Date();
Alert(week(d1));
9.year
//返回當前是第幾年
var d1 = new Date();
Alert(year(d1));
10.str2date
//按照字串格式轉換成Data資料
Alert(str2date("01.12.2006","dd.MM.yyyy"));
Alert(str2date("01.12.2006 23:23:01","dd.MM.yyyy HH:mm:ss"));
Alert(str2date("Tue.May.2006","E.MMM.yyyy","EN"));
Alert(str2date("22.02.2008 23:23:01","dd.MM.yyyy HH:mm:ss","DE"));
Alert(str2date("22.02.2008 23:23:01","dd.MM.yyyy HH:mm:ss","DE", "EST"));
四,Logic Function
1.isCodepage
//檢查,一個字串值對給定的內碼表(不知道怎麼用)
var xStr = "Réal";
Alert(isCodepage(xStr, "UTF-8"));
2.isDate
//檢查當前變數是不是時間變數
var d1 = "Hello World";
var d2 = new Date();
Alert(isDate(d1));
Alert(isDate(d2));
3.isEmpty
//檢查字串是不是空的
var d = "Hello World";
Alert(isEmpty(d));
4.isMailValid
//檢查這個郵件地址是不是對的
var str = "[email protected]";
Alert(isMailValid(str));
5.isNum
//檢查這個有變數是不是數字
var str1 = "Hello World";
var str2 = 123456;
Alert(isNum(str1));
Alert(isNum(str2));
6.isRegExp
//正則表示式匹配,先匹配第一個,如果沒有直接返回-1,如果有接下去匹配,返回匹配條數
var email1 ="[email protected]";
var email2= "[email protected]";
var email3= "[email protected]";
var str = "啊";
var reg1="^\\[email protected][a-zA-Z_]+?\\.[a-zA-Z]{2,3}$";
var reg2="^[\\w-\.][email protected]([\\w-]+\\.)+[\\w-]{2,4}$";
var reg3 ="[^\x00-\xff]";
Alert(isRegExp(email1, reg1,reg2) + " Matches");
Alert(isRegExp(email2, reg1,reg2) + " Matches");
Alert(isRegExp(email3, reg1,reg2) + " Matches");
Alert(isRegExp(str, reg3) + " Matches");
7.isWorkingDay
//判斷當前時間是不是工作日
var d1 = new Date();
Alert(isWorkingDay(d1));
五,Special Function
1.Alert
/*
//訊息彈出框
Alert("Hello World!");
2.LoadScriptFile
//讀取當前路徑下的JavaScript檔案
var xPfad = "C:/Users/LiHan/Desktop/adc.js";
LoadScriptFile(xPfad);
3.LoadScriptFromTab
//從表裡讀取Script(不知道怎麼用)
var xTab = "Alert("+"Hello World!"+")";
LoadScriptFromTab(xTab);
4.LuhnCheck
//判斷當前卡號是不是正確信用卡號
var str1 = "4444333322221111";
Alert(str1 + ": " + LuhnCheck(str1));
var str2 = "4444333322221110";
Alert(str2 + ": " + LuhnCheck(str2));
5.appendToFile
//把資料新增到檔案。如果檔案不存在,它將被建立。
var strFile = "C:/Users/LiHan/Desktop/abc.txt";
var strContent = "Hello World!";
appendToFile(strFile, strContent);
6.decode
//匹配字串,返回指指定字串或預設值
var str1 = "Hallo";
//匹配hallo 如果有返回hello,沒有返回Hallo
Alert(decode(str1, "Hall", "Hello"));
//匹配1,如果有返回Mr。匹配2,如果有返回Mrs,沒有返回指定值N/A
Alert(decode(str1, "1", "Mr", "2", "Mrs", "N/A"));
//匹配1,如果有返回Mr。匹配2,如果有返回Mrs,沒有返回預設值,原字串
Alert(decode(str1, "1", "Mr", "2", "Mrs"));
var str1 = "Mrs";
//匹配1,如果有返回Mr,匹配2,如果有返回Mrs,沒有返回預設值,原字串
Alert(decode(str1, "1", "Mr", "2", "Mrs"));
7.execProcess
//執行一個processus(不知道什麼東西),返回執行結果
var processus="檔案路徑";
execProcess(processus);
8.fireToDB
//使用時(找不到MySQL連線不知道怎麼使用)
var strConn = "MySQL";
var strSQL = "SELECT count(*) from ttt";
var xArr = fireToDB(strConn, strSQL);
9.getDigitsOnly
removeDigits
//返回字串包含的數字的值
var str1 = "abc123cde";
Alert(getDigitsOnly(str1));
//去除字串中的數字
var str1 = "abc123cde";
Alert(removeDigits(str1));
10.getEnvironmentVar
setEnvironmentVar
//獲得kettle系統變數的值
Alert(getEnvironmentVar("user.dir"));
Alert(getEnvironmentVar("user.name"));
//設定系統全域性變數
var strVarName="setEnvTest";
var strVarValue="123456";
Alert(getEnvironmentVar(strVarName));
setEnvironmentVar(strVarName,strVarValue);
Alert(getEnvironmentVar(strVarName));
strVarValue="654321";
setEnvironmentVar(strVarName,strVarValue);
Alert(getEnvironmentVar(strVarName));
11.getProcessCount
//返回當前處理行數
// 1: String - The Pentaho/Kettle Type:
// u - Lines Update
// i - Lines Insert
// w - Lines Write
// r - Lines Read
// o - Lines Output
Alert(getProcessCount("i"));
Alert(getProcessCount("o"));
12.getTransformationName
//返回當前js步驟名稱,如果在轉換中執行加上轉換的名字
var xTranName = getTransformationName();
Alert(xTranName);
13.setVariable
getVariable
//設定kettle變數
var strVarName="getVariableTest";
var strVarValue="123456";
// 1: String - The name of the variable to set.
// 2: Variable - The value which should be assigned
// 3: Type - "s"ystem, "r"oot, "p"arent, "g"randparent. Upto which
// level the variable is set.
setVariable(strVarName,strVarValue, "r");
//獲取kettle變數,如果沒有顯示為空
Alert(getVariable("strVarName", ""));
14.Print
Println
//在結果中打印出這個字串
var str = "Hello World!";
print(str);
//加一個換行
Println(str);
15.resolveIP
//轉換為一個數字IP地址(x.x.x.x)作為字串返回(不知道怎麼用)
Alert(resolveIP("127.0.0.1","IP"));
16.sendMail
//把電子郵件傳送到指定的地址
// sendMail(var, var, var, var, var);
// 1: String - SMTP-Server
// 2: String - The Address from which the mail shoud be send
// 3: String - The Address to which the mail should be send.
// For more Recipient's, please provide a list seperated by ','
// 4: String - Subject of the Mail
// 5: String - The Message Text of the mail.
var xSMTP="Your SMTP Server";
var xSender = "Sender eMail";
var xRecipients = "Recipient1,Recipient2,...";
var xSubject = "Hello World!";
var xMessage = "This is the message";
sendMail(xSMTP,xSender,xRecipients,xSubject,xMessage);
17.writeToLog
//寫一個字串到kettle日誌裡
// writeToLog(var);
// 1: String - The Message which should be written to
// the Kettle Debug Log
//
// writeToLog(var,var);
// 1: String - The Type of the Log
// d - Debug
// l - Detailed
// e - Error
// m - Minimal
// r - RowLevel
//
// 2: String - The Message which should be written to
// the Kettle Log
writeToLog("Hello World!");
writeToLog("r", "Hello World!");
六,File Function
1.copyFile
//複製一個檔案
// copyFile(var1,var2,var3);
// 1: String - The file to copy with Path.
// 2: String - The destination filename with Path.
// 3: Boolean- Overwrite destination file if exist.
var sourcefile="C:/Users/LiHan/Desktop/abc.txt";
var destinationfile="D:/abc.txt";
copyFile(sourcefile,destinationfile,true);
2.createFolder
//在指定路徑建立一個資料夾
var strFolder = "C:/Users/LiHan/Desktop/abcd";
createFolder(strFolder);
3.deletFile
//刪除指定路徑的檔案
var strFile = "C:/Users/LiHan/Desktop/abc.txt";
deletFile(strFile);
4.fileExists
//判斷檔案是否存在
var strFile = "C:/Users/LiHan/Desktop/檔案路徑.xlsx";
Alert(fileExists(strFile));
5.getFileExtension
//返回檔案型別
var sourcefile="C:/Users/LiHan/Desktop/檔案路徑.xlsx";
Alert(getFileExtension(sourcefile));
6.getFileSize
//返回檔案大小
var sourcefile="C:/Users/LiHan/Desktop/檔案路徑.xlsx";
Alert(getFileSize(sourcefile));
7.getLastModifiedTime
//返回檔案建立時間
var sourcefile="C:/Users/LiHan/Desktop/檔案路徑.xlsx";
var dateformat="yyyy-MM-dd HH:MM:ss";
Alert(getLastModifiedTime(sourcefile,dateformat));
8.getParentFoldername
//返回檔案的父路徑
var sourcefile="C:/Users/LiHan/Desktop/檔案路徑.xlsx";
Alert(getParentFoldername(sourcefile));
9.getShortFilename
//獲取檔名
var sourcefile="C:/Users/LiHan/Desktop/檔案路徑.xlsx";
Alert(getShortFilename(sourcefile));
10.isFile
//判斷是不是檔案
var sourcefile="C:/Users/LiHan/Desktop/檔案路徑.xlsx";
Alert(isFile(sourcefile));
11.isFolder
//判斷是不是資料夾
var sourcefile="C:/Users/LiHan/Desktop";
Alert(isFolder(sourcefile));
12.loadFileContent
//載入檔案內容
Alert(loadFileContent("C:/Users/LiHan/Desktop/abc.txt"));
Alert(loadFileContent("C:/Users/LiHan/Desktop/abc.txt"));
//載入檔案內容+字符集
Alert(loadFileContent("C:/Users/LiHan/Desktop/abc.txt","GBK"));
13.moveFile
//移動檔案
// moveFile(var1,var2,var3);
// 1: String - The file to move with Path.
// 2: String - The destination filename with Path.
// 3: Boolean- Overwrite destination file if exists.
var sourcefile="C:/Users/LiHan/Desktop/abc.txt";
var destinationfile="D:/abc.txt";
Alert(moveFile(sourcefile,destinationfile,false));
14.touch
//建立一個檔案,並修改檔案的修改時間
Alert(touch("C:/Users/LiHan/Desktop/abcd.txt"));