TSV 文字列; 数値列に掛け算
TSV 文字列; 数値列に掛け算 (TSV String; Multiply N by Number Column)

数値カラムの各数にNを掛けて、新しいTSV文字列を生成します。計算結果は小数点以下が四捨五入され、行末に要素追加されます。消費税計算などでご利用いただけます。TSV文字列の末尾先頭にある空白文字や改行文字は事前に削除します。

2019-08-01 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/ja/addons/tsv-string-multiply-n-by-number-column/

Configs
  • A: TSV テキストが格納されている文字列型データ項目を選択してください *
  • B: 乗算が適用される数値カラムのIDをセットしてください (例 “0”, “3”) *
  • C: 掛ける値をセットしてください (例 “1.1”, “1.0185185185”) *
  • D: 新しいTSVテキストが格納される文字列型データ項目を選択してください(更新) *
Script
// (c) 2019, Questetra, Inc. (the MIT License)

/*
=input tsv=
A1	10000	C1
A2	10800	C2
=N=
1.0185185185
=output tsv=
A1	10000	C1	10185
A2	10800	C2	11000
*/

//////// START "main()" ////////
main();
function main(){ 


//// == Config Retrieving / 工程コンフィグの参照 ==
const dataIdA = configs.get( "conf_DataIdA" ) + ""; // config required
const columnB = configs.get( "conf_ColumnB" ) + ""; // config required
const valueC  = configs.get( "conf_ValueC" )  + ""; // config required
const dataIdD = configs.get( "conf_DataIdD" ) + ""; // config required
// 'java.lang.String' to javascript primitive 'string' by ""

engine.log( " AutomatedTask Config: ID of Numeric Column " + columnB );
engine.log( " AutomatedTask Config: Value to multiply " + valueC );

// Design-time Config Format Check
const intColumnB  = parseInt( columnB );
if ( isNaN( intColumnB )){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {ID of Numeric Column} is not an integer \n" );
}
const numValueC  = parseFloat( valueC );
if ( isNaN( numValueC )){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {Value to multiply} is not a number \n" );
}


//// == Data Retrieving / ワークフローデータの参照 ==
// Run-time Data Format Check
if( engine.findDataByNumber( dataIdA ) === null ){
  throw new Error( "\n AutomatedTask UnexpectedStringError:" +
                   " String {A} as TSV is null \n" );
}
const inputText = (engine.findDataByNumber( dataIdA ) + "").trim();
if( inputText === "" ){
  throw new Error( "\n AutomatedTask UnexpectedStringError:" +
                   " String {A} as TSV is empty \n" );
}


//// == Calculating / 演算 ==
let   outputText = "";
const arrInputText = inputText.split("\n");
engine.log( " AutomatedTask MultilineString:" + 
            " String {A} as TSV, number of lines " + arrInputText.length );

for( let i = 0; i < arrInputText.length; i++ ){
  let arrCellValues = arrInputText[i].split("\t");
  let strValueB = arrCellValues[ intColumnB ];
  let numValueB = parseFloat( strValueB );
  // Run-time Data Format Check
  if ( isNaN( numValueB )){
    throw new Error( "\n AutomatedTask UnexpectedStringError:" +
    " String {A} as TSV, Specified Column is not numeric at " + i + "\n" );
  }
  let newValue  = Math.round( numValueB * numValueC );
  outputText += arrInputText[i] + "\t" + newValue;
  if( i !== arrInputText.length - 1 ){
    outputText += "\n";
  }
}


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


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

Download

Capture

See also

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

%d人のブロガーが「いいね」をつけました。