TSV String; Add N to Number Column

TSV String; Add N to Number Column
Generates a new TSV string by adding N to each number in the number column. Calculation results are added at the end of the line. This can be used for creating discounted price lists, etc. Removes whitespace from both ends of a string.
Configs
  • A: Select STRING DATA for TSV Text *
  • B: Set ID of Numeric Column to be added (e.g. “0”, “3”) *
  • C: Set Value to add (e.g. “1.5”, “-500”) *
  • D: Select STRING DATA for New TSV Text (update) *
Script (click to open)
// GraalJS Script (engine type: 2)
// (c) 2021, Questetra, Inc. (the MIT License)

/*
=input tsv=
A1	B1	C1	10000
A2	B1	C1	10800
=N=
-500
=output tsv=
A1	B1	C1	10000	9500
A2	B1	C1	10800	10300
*/

//////// 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 add " + 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 add} 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  = numValueB + numValueC;
  outputText += arrInputText[i] + "\t" + newValue;
  if( i !== arrInputText.length - 1 ){
    outputText += "\n";
  }
}


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


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

Download

2021-07-15 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/tsv-string-add-n-to-number-column-2021/
The Add-on import feature is available with Professional edition.



Capture

See also

TSV String; Re-order Columns

1 thought on “TSV String; Add N to Number Column”

  1. Pingback: TSV String; Sum of Number Column – Questetra Support

Comments are closed.

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top