Converter: #TSV String to #Markdown String

Converter: #TSV String to #Markdown String

Converter: #TSV String to #Markdown String

translate コンバータ: #TSV文字列 to #Markdown文字列

Converts a TSV string to a Markdown string. The first line of the TSV is recognized as a header line, and the second and subsequent lines are recognized as data lines. Also supports column settings such as right alignment and center alignment.

Auto Step icon
Configs for this Auto Step
StrConfA
A: Set TSV String *#{EL}
StrConfB
B: Set Right/Left alignment for each column (eg: “R,L,R,C,C”) *#{EL}
SelectConfC
C: Select DATA to store Converted Markdown String (update)
SelectConfD1
D1: Select NUMERIC for Number of TSV Lines (update)
SelectConfD2
D2: Select NUMERIC for Number of TSV Lines Trimmed (update)
Script (click to open)
// Script Example of Business Process Automation
// for 'engine type: 3' ("GraalJS standard mode")
// cf. 'engine type: 2' ("GraalJS Nashorn compatible mode") (renamed from "GraalJS" at 20230526)


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

//// == Config Retrieving / 工程コンフィグの参照 ==
const strTsv        = configs.get       ( "StrConfA" );     // REQUIRED
  if( strTsv === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A: TSV} is empty \n" );
  }
  const numTsvLines = strTsv.split("\n").length;
  if(   numTsvLines < 2 ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A: TSV} requires at least 2 lines (header and data) \n" );
  }
  const numTsvNoLf  = strTsv.replace(/[\n]*$/, "").split("\n").length;
                      // delete Line Feed codes at the end

  // get TSV as Array-Array (2d Array)
  const arr2dTsv    = parseAsRectangular( strTsv );

const strAlignment  = configs.get       ( "StrConfB" )  === "" ?     // not required
                      "L" : // default
                      configs.get       ( "StrConfB" ) ; 
  let arrAlignment  = strAlignment.split(",");

const strPocketC    = configs.getObject ( "SelectConfC" );  // REQUIRED

const numPocketD1   = configs.getObject ( "SelectConfD1" );  // not required
const numPocketD2   = configs.getObject ( "SelectConfD2" );  // not required



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



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

let strMarkdown = "";

/// Get first line (Table Header)
strMarkdown += "| ";
for ( let i = 0; i < arr2dTsv[0].length; i++ ){
  strMarkdown += arr2dTsv[0][i];
  strMarkdown += " | ";
}
strMarkdown += "\n";

/// Get alignment line
strMarkdown += "| ";
for ( let i = 0; i < arr2dTsv[0].length; i++ ){
  let strTmp = arrAlignment?.[i] ?? "L"; // see Optional Chaining and Nullish Coalescing
  if( strTmp === "L" ){
    strMarkdown += " :--- | ";
  }else if( strTmp === "C" ){
    strMarkdown += " :---:| ";
  }else if( strTmp === "R" ){
    strMarkdown += " ---: | ";
  }
}
strMarkdown += "\n";

/// Get second line onwards
for ( let i = 1; i < arr2dTsv.length; i++ ){
  strMarkdown += "| ";
  for ( let j = 0; j < arr2dTsv[i].length; j++ ){
    strMarkdown += arr2dTsv[i][j];
    strMarkdown += " | ";
  }
  strMarkdown += "\n";
}


//// == Data Updating / ワークフローデータへの代入 ==
/// ref) Retrieving / Updating from ScriptTasks
/// https://questetra.zendesk.com/hc/en-us/articles/360024574771-R2301
/// https://questetra.zendesk.com/hc/ja/articles/360024574771-R2301


if ( strPocketC !== null ){ 
  engine.setData( strPocketC, strMarkdown );
}

if ( numPocketD1 !== null ){ 
  engine.setData( numPocketD1, new java.math.BigDecimal( numTsvLines ) );
}
if ( numPocketD2 !== null ){ 
  engine.setData( numPocketD2, new java.math.BigDecimal( numTsvNoLf ) );
}

} //////// 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 a process reaches this [Automated Step], the conversion is automatically executed.
    - TSV text will be converted to Markdown text.
- The number of lines of the input TSV text can also be extracted at the same time.

NOTES-ja
- この[自動工程]に案件が到達すると、「変換処理」が自動実行されます。
    - TSVテキストが、Markdownテキストに変換されます。
- 入力TSVテキストの行数も同時に抽出できます。


▼input TSV example
-	Customer	Quote Amount	Proposer	Reviewer
#1	Qatar Airlines	$12,345,678	Akira TORIYAMA	Shohei OTANI
#2	Singapore Airlines	$1,234,567	Ichiro SUZUKI	Shohei OTANI
#4	ANA All Nippon Airways	$123,456	Taro ASO	Kakuei TANAKA
#12	Japan Airlines	$123	Taro ASO	Shinzo ABE
*/

/*
APPENDIX
- If the input TSV text is an empty string, an error occurs.
- If the input TSV text is a single line, an error occurs.
- If the input TSV text contains blank lines, they will be skipped.
*/

Download

warning Freely modifiable JavaScript (ECMAScript) code. No warranty of any kind.
(Installing Addon Auto-Steps are available only on the Professional edition.)

Notes

  • When a process reaches this [Automated Step], the conversion is automatically executed.
    • TSV text will be converted to Markdown text.
  • The number of lines of the input TSV text can also be extracted at the same time.

Capture

Appendix

  • If the input TSV text is an empty string, an error occurs.
  • If the input TSV text is a single line, an error occurs.
  • If the input TSV text contains blank lines, they will be skipped.

See Also

Scroll to Top

Discover more from Questetra Support

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

Continue reading