Tsv String; Re-order Columns

Extracts a new TSV string re-arranged by a column ID list from TSV string. ID repetition in the list is also possible. It can be used to narrow down necessary information or conform to the format. Removes whitespace from both ends of a string.

2019-07-21 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/tsv-string-reorder-columns/

Configs
  • A: Select STRING DATA for TSV Text *
  • B: Set Column ID List to Extract (e.g. “0,1,3,0”) *
  • C: Select STRING DATA for New TSV Text Re-ordered (update) *
Script
// (c) 2019, Questetra, Inc. (the MIT License)

/*
input tsv
A1	B1	C1	D1	E1
A2	B2	C2	D2	E2
column id list
0,1,3,0
output tsv
A1	B1	D1	A1
A2	B2	D2	A2
*/

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


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

// Design-time Config Format Check
engine.log( " AutomatedTask Config: Column ID List " + columns );
if ( columns.match(/^[0-9][0-9,]*$/) === null ){
  throw new Error( "\n AutomatedTask UnexpectedConfigError:" +
  " Config {Column ID List} allows only numbers and commas. \n" );
}
const arrColumns  = columns.split(",");


//// == 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");
  for( let j = 0; j < arrColumns.length; j++ ){
    if ( arrColumns[j] === "" ){
      throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {Column ID List} must be CSV format \n" );
    }
    outputText += arrCellValues[ (arrColumns[j] - 0) ];
    if( j !== arrColumns.length - 1 ){
      outputText += "\t";
    }
  }
  if( i !== arrInputText.length - 1 ){
    outputText += "\n";
  }
}


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


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

Download

Capture

See also

4 thoughts on “TSV String; Re-order Columns”

  1. Pingback: Change Order of Tsv – Questetra Support

  2. Pingback: TSV String; Multiply N by Number Column – Questetra Support

  3. Pingback: TSV String; Add N to Number Column – Questetra Support

  4. Pingback: TSV String; Add Prefix to Column – Questetra Support

Leave a Reply

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