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

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

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

translate Converter: #TSV String to #Markdown String

TSV文字列をMarkdown文字列に変換します。TSVの1行目はヘッダ行(TH: Table Header)とみなされ、2行目以降がデータ行(TD: Table Data)とみなされます。右寄せやセンタリングといった列の表示設定も可能です。

Auto Step icon
Configs for this Auto Step
StrConfA
A: TSV文字列をセットしてください *#{EL}
StrConfB
B: 列の右寄せ左寄せ設定をセットしてください(例: “R,L,R,C,C”) *#{EL}
SelectConfC
C: 変換されたMarkdown文字列を格納するデータを選択してください (更新)
SelectConfD1
D1: TSV行数を格納する数値型データを選択してください (更新)
SelectConfD2
D2: 末尾改行コードを無視した行数が格納される数値型データを選択してください (更新)
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
- The 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
- 入力TSVテキストが空文字列の場合、エラーとなります。
- 入力TSVテキストが1行の場合、エラーとなります。
- 入力TSVテキストに空行がある場合、スキップされます。
*/

Download

warning 自由改変可能な JavaScript (ECMAScript) コードです。いかなる保証もありません。
(アドオン自動工程のインストールは Professional editionでのみ可能です)

Notes

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

Capture

Appendix

  • 入力TSVテキストが空文字列の場合、エラーとなります。
  • 入力TSVテキストが1行の場合、エラーとなります。
  • 入力TSVテキストに空行がある場合、スキップされます。

See Also

上部へスクロール

Questetra Supportをもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む