Converter: TSV-String to Table

Converter: TSV-String to Table
Converter: TSV-String to Table

Converts TSV String to Table-type data. The string value of all cells in the TSV string is copied over Table-type data B. If there is an inconsistency in the Table column design, an error may occur.

2020-02-27 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/converter-tsv-string-to-table/

Configs
  • A: Set TSV String * #{EL}
  • B: Select TABLE DATA (update)
  • B2: Select TABLE DATA (append)
Script
// Nashorn Script (engine type: 1)
// 
// Notes:
// Copies text information of all cell values.
// Copies in the display order of the column. (Does not depend on column ID)
// If the number of columns decreases, the right columns will not be copied.
// If the number of columns increases, blank strings will be completed.
// For Numeric-column, the period "." is recognized as a decimal point.
// For Numeric-column, the comma "," is recognized as a thousand separator.
// For Select-column, the value must be choice ID. (Otherwise, not copied)
// Auto-calculated columns are reevaluated. (Not always the same value)
// To add data without erasing already stored cell data, use B2.
//
// Notes (ja):
// 全てのセル値の文字情報をコピーします。
// カラムの表示順にコピーします。(カラムIDに依らない)
// カラム列数が少なくなる場合は、右列部がカットされます。
// カラム列数が増える場合は、空白文字列で補完されます。
// 数値カラムへの値コピーは、ピリオド "." を小数点として認識して代入します。
// 数値カラムへの値コピーは、桁区切り文字 "," が存在しても構いません。
// 選択カラムへの値コピーは、選択肢IDでなければなりません。(コピーされません)
// 自動計算カラムは、再評価されます。(必ずしも同じ値になりません)
// 既に格納されているセルデータを消さずに追記したい場合は、B2をご利用ください。


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

//// == Config Retrieving / 工程コンフィグの参照 ==
const strTsv   = configs.get( "conf_StrTsv" )   + ""; // required
const dataIdB  = configs.get( "conf_DataIdB" )  + ""; // not required
const dataIdB2 = configs.get( "conf_DataIdB2" ) + ""; // not required
// 'java.lang.String' to javascript primitive 'string'

if( strTsv === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " TSV String {A} is empty \n" );
}
let numColsB = 0;
if( dataIdB !== "" ){
  numColsB  = engine.findDataDefinitionByNumber( dataIdB )
                    .getSubDataDefinitions().size() - 0;
  engine.log( " AutomatedTask TableConfig:" +
              " #of {B}-Columns Definition: " + numColsB );
}
let numColsB2 = 0;
if( dataIdB2 !== "" ){
  numColsB2  = engine.findDataDefinitionByNumber( dataIdB2 )
                     .getSubDataDefinitions().size() - 0;
  engine.log( " AutomatedTask TableConfig:" +
              " #of {B2}-Columns Definition: " + numColsB2 );
}


//// == Data Retrieving / ワークフローデータの参照 ==
// (nothing)


//// == Calculating / 演算 ==
let twoDimArrayA = []; // for TSV-A
const arrTsv = strTsv.split("\n");
engine.log( " AutomatedTask MultilineString:" + 
            " TSV {A}, number of lines " + arrTsv.length );
for( let i = 0; i < arrTsv.length; i++ ){
  twoDimArrayA[i] = arrTsv[i].split("\t");
}

let outputTable1 = new com.questetra.bpms.core.model.formdata.ListArray();
if( dataIdB !== "" ){
  for( let i = 0; i < arrTsv.length; i++ ){
    let tmpRow = new com.questetra.bpms.core.model.formdata.ListArray.ListRow();
    if( twoDimArrayA[i].length < numColsB ){
      for( let j = 0; j < twoDimArrayA[i].length; j++ ){
        if( engine.findDataDefinitionByNumber( dataIdB )
                  .getSubDataDefinitions()
                  .get(j).matchDataType("DECIMAL") ){
          twoDimArrayA[i][j] = twoDimArrayA[i][j].replace( /,/g, "" );
        }
        tmpRow.addCol( twoDimArrayA[i][j] );
      }
      for( let j = 0; j < numColsB - twoDimArrayA[i].length; j++ ){
        tmpRow.addCol( "" ); // Fill with empty characters
      }
    }
    if( twoDimArrayA[i].length >= numColsB ){
      for( let j = 0; j < numColsB; j++ ){
        if( engine.findDataDefinitionByNumber( dataIdB )
                  .getSubDataDefinitions()
                  .get(j).matchDataType("DECIMAL") ){
          twoDimArrayA[i][j] = twoDimArrayA[i][j].replace( /,/g, "" );
        }
        tmpRow.addCol( twoDimArrayA[i][j] );
      }
    }
    outputTable1.addRow( tmpRow );
    engine.log( " AutomatedTask:" +
                " Add B[" + i + "]" );
  }
}

let outputTable2 = new com.questetra.bpms.core.model.formdata.ListArray();
let numOriginLines = 0;
if( dataIdB2 !== "" ){
  if( engine.findDataByNumber( dataIdB2 ) !== null ){
    outputTable2 = engine.findDataByNumber( dataIdB2 ); 
    numOriginLines = engine.findDataByNumber( dataIdB2 ).size() - 0;
  }
  engine.log( " AutomatedTask:" +
              " #of {B2}-Rows: " + numOriginLines );

  for( let i = 0; i < arrTsv.length; i++ ){
    let tmpRow = new com.questetra.bpms.core.model.formdata.ListArray.ListRow();
    if( twoDimArrayA[i].length < numColsB2 ){
      for( let j = 0; j < twoDimArrayA[i].length; j++ ){
        if( engine.findDataDefinitionByNumber( dataIdB2 )
                  .getSubDataDefinitions()
                  .get(j).matchDataType("DECIMAL") ){
          twoDimArrayA[i][j] = twoDimArrayA[i][j].replace( /,/g, "" );
        }
        tmpRow.addCol( twoDimArrayA[i][j] );
      }
      for( let j = 0; j < numColsB2 - twoDimArrayA[i].length; j++ ){
        tmpRow.addCol( "" ); // Fill with empty characters
      }
    }
    if( twoDimArrayA[i].length >= numColsB2 ){
      for( let j = 0; j < numColsB2; j++ ){
        if( engine.findDataDefinitionByNumber( dataIdB2 )
                  .getSubDataDefinitions()
                  .get(j).matchDataType("DECIMAL") ){
          twoDimArrayA[i][j] = twoDimArrayA[i][j].replace( /,/g, "" );
        }
        tmpRow.addCol( twoDimArrayA[i][j] );
      }
    }
    outputTable2.addRow( tmpRow );
    engine.log( " AutomatedTask:" +
                " Add B2[" + (numOriginLines + i) + "]" );
  }
}


//// == Data Updating / ワークフローデータへの代入 ==
if( dataIdB !== "" ){
  engine.setDataByNumber( dataIdB, outputTable1 );
}
if( dataIdB2 !== "" ){
  engine.setDataByNumber( dataIdB2, outputTable2 );
}

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

Download

Capture

Converts TSV String to Table-type data. The string value of all cells in the TSV string is copied over Table-type data B. If there is an inconsistency in the Table column design, an error may occur.

Notes

  • Copies text information of all cell values.
  • Copies in the display order of the column. (Does not depend on column ID)
  • If the number of columns decreases, the right columns will not be copied.
  • If the number of columns increases, blank strings will be completed.
  • For Numeric-column, the period “.” is recognized as a decimal point.
  • For Numeric-column, the comma “,” is recognized as a thousand separator.
  • For Select-column, the value must be choice ID. (Otherwise, not copied)
  • Auto-calculated columns are reevaluated. (Not always the same value)
  • To add data without erasing already stored cell data, use B2.

See also

3 thoughts on “Converter: TSV-String to Table”

  1. Pingback: Converter (Tsv to Table) – Questetra Support

  2. Pingback: Converter: Table to TSV-String – Questetra Support

  3. Pingback: Converter: Table to TSV-String – Questetra Support

Leave a Reply

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

Scroll to Top

Discover more from Questetra Support

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

Continue reading