TSV String; Add Prefix to Column

Generates a new TSV with concatenating the prefix Str to all cells in the column. The concatenated strings are added at the end of the line. Available at categorization etc. Removes whitespace from both ends of a string.

2019-08-02 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/tsv-string-add-prefix-to-column/

Configs
  • A: Select STRING DATA for TSV Text *
  • B: Set Column ID to which prefix is concatenated (e.g. “0”) *
  • C: Set Prefix String (e.g. “https://”, “$ “, “DEL: “) #{EL}
  • D: Set Number of Characters to delete beginning (e.g. “0”) *
  • E: Select STRING DATA for New TSV Text (update) *
Script
// (c) 2019, Questetra, Inc. (the MIT License)

/*
=input tsv=
A1	B1	C1	10000
A2	B1	C1	10800
=Column ID=
3
=Prefix=
$
=BeginIndex=
2
=output tsv=
A1	B1	C1	10000	$000
A2	B1	C1	10800	$800
*/

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


//// == Config Retrieving / 工程コンフィグの参照 ==
const dataIdA = configs.get( "conf_DataIdA" ) + ""; // config required
const columnB = configs.get( "conf_ColumnB" ) + ""; // config required (int)
const prefixC = configs.get( "conf_PrefixC" ) + ""; // config el-enabled
const beginIndex = configs.get( "conf_BeginIndex" ) + ""; // config required (int)
const dataIdE = configs.get( "conf_DataIdE" ) + ""; // config required
// 'java.lang.String' to javascript primitive 'string' by ""

engine.log( " AutomatedTask Config: ID of Numeric Column: " + columnB );
engine.log( " AutomatedTask Config: Prefix: " + prefixC );
engine.log( " AutomatedTask Config: Begin Index: " + beginIndex );

// 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 intBeginIndex = parseInt( beginIndex );
if( isNaN( intBeginIndex )){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {Begin Index} is not an integer \n" );
}
if( prefixC === "" ){
  engine.log( " AutomatedTask ConfigWarning:" +
              " Config {Prefix} is empty" );
}
if( prefixC.match(/\t/) !== null ){
  throw new Error( "\n AutomatedTask ConfigError or UnexpectedStringError:" +
                   " Config {Prefix}, Tab characters are not allowed. \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 newValue  = prefixC + strValueB.slice( intBeginIndex );
  outputText += arrInputText[i] + "\t" + newValue;
  if( i !== arrInputText.length - 1 ){
    outputText += "\n";
  }
}


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


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

Download

Capture

Notes

  1. If D is set to a negative value (-N), the prefix is concatenated to the last N characters.

See also

1 thought on “TSV String; Add Prefix to Column”

  1. Pingback: TSV String; Add Suffix to Column – Questetra Support

Leave a Reply

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

%d bloggers like this: