Converter (JapanZipCsv to JapanZipXml)
Generates a postal code master. The zip data “KEN_ALL.CSV” (120k lines) of Japan Post is converted to “Japan-Zip.xml”. You can refer to postal code master in input form settings such as “Select (Search Select box)”.
https://support.questetra.com/addons/converter-japanzipcsv-to-japanzipxml/
2019-01-28 (C) Questetra, Inc. (MIT License)
Configs
  • A: Select FILES DATA for KEN_ALL.CSV (Multiple not supported) *
  • B: Select FILES DATA for Japan-Zip.xml (update) *
  • Bx: Set the file name if you want to save as a different name #{EL}
Script
//////// START "main()" ////////
main();
function main(){ 


//// == Config Retrieving / 工程コンフィグの参照 ==
const dataIdA = configs.get( "conf_DataIdA" ) + "";
const dataIdB = configs.get( "conf_DataIdB" ) + "";
const saveAs  = configs.get( "conf_SaveAs" ) + "";
// 'java.lang.String' (String Obj) to javascript primitive 'string'

let fileName = "";
if( saveAs === "" ){
  fileName = "Japan-Zip.xml";
}else{
  fileName = saveAs;
}


//// == Data Retrieving / ワークフローデータの参照 ==
var myFiles = engine.findDataByNumber( dataIdA ); 
var upFiles = engine.findDataByNumber( dataIdB ); 
// java.util.ArrayList
if (myFiles === null || myFiles.size() != 1) {
  throw new Error( '\n One-file attachment is only valid. \n' );
}
if (upFiles === null ) {
  upFiles = new java.util.ArrayList();
}


//// == Calculating / 演算 ==
/// Read Lines
let myLineCounter = 0;
let arrCodeAddr   = [];
fileRepository.readFile(myFiles.get(0), "Shift_JIS", function(line) {
  const lineVals = line.split(",");
  const zipCode  = lineVals[2].replace(/"/g, '');
  const zipAddr  = (lineVals[6] + lineVals[7]).replace(/"/g, '');
  let   zipAddr2 = lineVals[8].replace(/"/g, '');
  if( lineVals[12] == "1" ){ zipAddr2 += "、"; } // 二以上の町域
  const codeAddr = [zipCode, zipAddr, zipAddr2];
  arrCodeAddr.push( codeAddr );
  myLineCounter++;
});
engine.log( "#Line of Text-File: " + myLineCounter );

/// Sort by ZipCode
// "二以上の町域" を結合しやすくするため
// "0680546","北海道","夕張市","南部青葉町",0,0,0,1,0,0
// "0680545","北海道","夕張市","南部東町",0,0,0,0,0,0
// "0680541","北海道","夕張市","南部遠幌町",0,0,0,0,0,0
// "0680544","北海道","夕張市","南部大宮町",0,0,0,0,0,0
// "0680546","北海道","夕張市","南部菊水町",0,0,0,1,0,0
// ...
arrCodeAddr.sort(function(a, b){
	if (a[0] > b[0]) return 1;
	if (a[0] < b[0]) return -1;
	return 0;
});

/// Generate XML
// '<item value="1500041" display="150-0041 東京都渋谷区神南" />'
// https://www.post.japanpost.jp/zipcode/dl/readme.html
let xmlFileText = '';
let stackedAddr = '';
for( var i = 0; i < myLineCounter - 1; i++ ){
  if( arrCodeAddr[i][0] !== arrCodeAddr[i+1][0] ){
    xmlFileText += '<item value="';
    xmlFileText += arrCodeAddr[i][0];
    xmlFileText += '" display="';
    xmlFileText += arrCodeAddr[i][0].substring(0,3) + '-' + arrCodeAddr[i][0].substring(3,7);
    xmlFileText += ' ';
    if( stackedAddr !== '' ){
      var joinedAddr = stackedAddr + arrCodeAddr[i][2];
      joinedAddr = joinedAddr.replace(/、$/, '');
      xmlFileText += joinedAddr;
    }else{
      xmlFileText += arrCodeAddr[i][1] + arrCodeAddr[i][2];
    }
    xmlFileText += '" />\n';
    stackedAddr = '';
  }else{
    if( stackedAddr !== '' ){
      stackedAddr += arrCodeAddr[i][2];
    }else{
      stackedAddr += arrCodeAddr[i][1] + arrCodeAddr[i][2];
    }
  }
}
/// Generate XML (last line)
xmlFileText += '<item value="';
xmlFileText += arrCodeAddr[myLineCounter - 1][0];
xmlFileText += '" display="';
xmlFileText += arrCodeAddr[myLineCounter - 1][0].substring(0,3) + '-' + arrCodeAddr[myLineCounter - 1][0].substring(3,7);
xmlFileText += ' ';
if( stackedAddr !== '' ){
  let joinedAddr = stackedAddr + arrCodeAddr[myLineCounter - 1][2];
  joinedAddr = joinedAddr.replace(/、$/, '');
  xmlFileText += joinedAddr;
}else{
  xmlFileText += arrCodeAddr[myLineCounter - 1][1] + arrCodeAddr[myLineCounter - 1][2];
}
xmlFileText += '" />\n';

/// Replace Noise Words
xmlFileText = xmlFileText.replace(/(その他)/g,'')
  .replace(/以下に掲載がない場合/g,'')
  .replace(/の次に番地がくる場合/g,'');

/// Add XML Declaration etc
xmlFileText = '<?xml version="1.0" encoding="UTF-8"?><items>\n'
  + xmlFileText + '</items>';

/// Add to Files Array
upFiles.add( new com.questetra.bpms.core.event.scripttask.NewQfile(
  fileName, "application/xml", xmlFileText ));


//// == Data Updating / ワークフローデータへの代入 ==
engine.setDataByNumber( dataIdB, upFiles );

} //////// END "main()" ////////

Download

Capture

See also

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: