TSV 文字列: 数値列の総和
TSV 文字列; 数値列の総和 (TSV String; Sum of Number Column)
数値カラムの値の合計を算出します。指定カラムに数値以外のデータが混在している場合、当該レコードはゼロとみなされ和算されません。
Configs
  • A: TSV テキストが格納されている文字列型データ項目を選択してください *
  • B: 総和が計算される数値カラムのIDをセットしてください (例 “0”) *
  • C: 列の合計が格納される数値型データ項目を選択してください(更新) *
Script (click to open)
// GraalJS Script (engine type: 2)
// (c) 2021, Questetra, Inc. (the MIT License)

/*
=input tsv=
A1	10000	C1
A2	$9900	C2
A3	10800	C3
=column id=
1
=output value=
20800
*/

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


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

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

// 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" );
}


//// == 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   outputNum = 0;
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 )){
    engine.log( " AutomatedTask StringWarning:" +
                " CellStr is not numeric at " + i +  ": " + strValueB );
    numValueB = 0;
  }
  outputNum += numValueB;
}


//// == Data Updating / ワークフローデータへの代入 ==
engine.setDataByNumber( dataIdC, new java.math.BigDecimal( outputNum ) );


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

Download

2021-07-16 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/ja/addons/tsv-string-sum-of-number-column-2021/
Addonファイルのインポートは Professional でのみご利用いただけます

Notes

  • 数値判定は JavaScript parseFloat() に依存するため “接頭辞” があると数値認識されません
  • 数値データに桁区切り文字が含まれる場合、計算結果が異なるので注意が必要です

Capture

See also

コメントを残す

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

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