TSV String, Verify Rectangle

TSV String, Verify Rectangle
TSV String, Verify Rectangle
Verifies that the cells are in a matrix format. If the number of cell values are different for each row, tabs will be added. It is also possible to get the number of rows and columns after formatting. No trailing newline will be added to the verified TSV.
Configs
  • A1: Set TSV *#{EL}
  • B1: Select STRING DATA that stores Output TSV (update)
  • B2: Select NUMERIC DATA that stores Number of Rows (update)
  • B3: Select NUMERIC DATA that stores Number of Columns (update)
Script (click to open)
// GraalJS Script (engine type: 2)

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

//// == Config Retrieving / 工程コンフィグの参照 ==
const strTsv          = configs.get( "StrConfA1" );           /// REQUIRED /////////////
  if( strTsv        === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A1: Tsv} is empty \n" );
  }
  const arr2dTsv      = parseAsRectangular( strTsv );
const strPocketOutput       = configs.getObject( "SelectConfB1" );  // NotRequired /////
const numPocketNumOfRows    = configs.getObject( "SelectConfB2" );  // NotRequired /////
const numPocketNumOfColumns = configs.getObject( "SelectConfB3" );  // NotRequired /////


//// == Data Retrieving / ワークフローデータの参照 ==
// (Nothing. Retrieved via Expression Language in Config Retrieving)


//// == Calculating / 演算 ==

// output TSV
let strOutput = "";
for( let i = 0; i < arr2dTsv.length; i++ ){
  strOutput += arr2dTsv[i].join("\t") + "\n";
}
strOutput = strOutput.slice( 0, -1 ); // delete last "\n"


//// == Data Updating / ワークフローデータへの代入 ==
if( strPocketOutput !== null ){
  engine.setData( strPocketOutput, strOutput );
}
if( numPocketNumOfRows !== null ){
  engine.setData( numPocketNumOfRows, new java.math.BigDecimal( arr2dTsv.length ) );
}
if( numPocketNumOfColumns !== null ){
  engine.setData( numPocketNumOfColumns, new java.math.BigDecimal( arr2dTsv[0].length ) );
}


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


// Parses TSV string as two-dimensional rectangular data matrix and creates a 2D array.
function parseAsRectangular( strTsv ){
  const arrTsv = strTsv.split("\n");

  /// Get numMinWidth and numMaxWidth (blank lines are excluded)
  let numMinWidth   = Infinity; // cf. String-Type Max: 1 million
  let numMaxWidth   = 0;
  let numBlanklines = 0;
  for( let i = 0; i < arrTsv.length; i++ ){
    if( arrTsv[i] === "" ){ // Skip blank lines
      numBlanklines += 1;
      continue;
    }
    let arrCells = arrTsv[i].split("\t");
    if( numMinWidth > arrCells.length ){ numMinWidth = arrCells.length; }
    if( numMaxWidth < arrCells.length ){ numMaxWidth = arrCells.length; }
  }
  engine.log( " AutomatedTask TsvDataCheck:" + 
              " MinWidth:" + numMinWidth +
              " MaxWidth:" + numMaxWidth +
              " Lines:" + arrTsv.length +
              " (BlankLines:" + numBlanklines + ")" );

  /// Get numMinWidth and numMaxWidth (blank lines are excluded)
  let arr2dTsv      = [];
  for( let i = 0; i < arrTsv.length; i++ ){
    if( arrTsv[i] === "" ){ // Skip blank lines
      continue;
    }
    let arrTmp = [];
    let arrCells = arrTsv[i].split("\t");
    for( let j = 0; j < numMaxWidth; j++ ){
      if( j < arrCells.length ){
        arrTmp[j] = arrCells[j];
      }else{
        arrTmp[j] = "";
      }
    }
    arr2dTsv.push( arrTmp );
  }

  return arr2dTsv;
}

/*
Notes:
- When the process reaches this automated task, TSV is automatically read.
    - TSV: Monthly sales, monthly access log, etc.
- If there is a blank line in the input TSV text, it will be skipped.
    - The output TSV is rectangular data. There are no blank lines.
    - No line feed code is added to the last line of the output TSV.

APPENDIX:
- TSV data that is not rectangular is automatically formatted with an empty string.
    - Tabs are added according to the row with the largest number of columns.
    - However, lines with only line breaks (blank lines) are skipped.


Notes-ja:
- 案件が自動処理工程に到達した際、文字列型データに保存されているTSVが自動的に読み込まれます。
    - TSV: 月次売上、月次アクセスログ、など
- 入力TSVテキストに空行がある場合、スキップされます。
    - 出力TSVは矩形データとなります。空行は存在しません。
    - 出力TSVの最終行に改行コードは付与されません。

APPENDIX-ja:
- 矩形でないTSVデータは、空文字によって自動整形されます。
    - もっとも列数(タブ数)が多い行にあわせて、タブが追加されます。
    - ただし、改行のみの行(空行)は、スキップされます。
*/

Download

The Add-on import feature is available with Professional edition.
Freely modifiable JavaScript (ECMAScript) code. No warranty of any kind.

Notes

  • When the process reaches this automated task, TSVs stored in String-type data will be read automatically.
    • TSV: monthly sales, monthly access log, etc.
  • If there is a blank line in the input TSV text, it will be skipped.
    • The output TSV will be rectangular data. There are no blank lines.
    • No line feed code is added to the last line of the output TSV.

Capture

Verifies that the cells are in a matrix format. If the number of cell values are different for each row, tabs will be added. Also possible to get the number of rows and columns after formatting. No trailing newline in the verified TSV.
Verifies that the cells are in a matrix format. If the number of cell values are different for each row, tabs will be added. Also possible to get the number of rows and columns after formatting. No trailing newline in the verified TSV.

Appendix

  • TSV data that is not rectangular is automatically formatted with an empty string.
    • Tabs are added according to the row with the largest number of columns.
    • However, lines with only line breaks (blank lines) are skipped.

See also

TSV String, Convert

Leave a Reply

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

Discover more from Questetra Support

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

Continue reading

Scroll to Top