複数行文字列, 範囲行の取得
複数行文字列, 範囲行の取得 (Multiline String, Get Range Lines)
指定範囲の行を抽出します。抽出範囲は開始インデックスと終了インデックスで指定されます。インデックスはゼロ始まりで、先頭行のインデックスはゼロとなります。終了インデックスで指定された行は抽出範囲に含まれない点に注意が必要です。
Configs
  • A1: オリジナルのテキストをセットしてください *#{EL}
  • B1: 開始インデックスをセットしてください (例 “0” “2” “-5” ) *#{EL}
  • B2: 終了インデックスをセットしてください (例 “2” “-5” “” )#{EL}
  • C1: 抽出テキストが格納される文字列型データ項目を選択してください (更新) *
Script (click to open)
// GraalJS Script (engine type: 2)

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

//// == Config Retrieving / 工程コンフィグの参照 ==
const strInput         = configs.get( "StrConfA1" );          /// REQUIRED //////////////
  if( strInput       === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {A1: Text} is empty \n" );
  }
const strBeginIndex    = configs.get( "StrConfB1" );          /// REQUIRED //////////////
  if( strBeginIndex  === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {B1: BeginIndex} is empty \n" );
  }
  const numBeginIndex  = parseInt( strBeginIndex, 10 );
  if( isNaN( numBeginIndex ) ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {B1: BeginIndex} must be an integer \n" );
  }
const strEndIndex      = configs.get( "StrConfB2" );          // NotRequired ////////////
  const numEndIndex    = parseInt( strEndIndex, 10 );
  if( strEndIndex    !== "" ){
    if( isNaN( numBeginIndex ) ){
      throw new Error( "\n AutomatedTask ConfigError:" +
                       " Config {B2: EndIndex} must be an integer or empty \n" );
    }
  }
const strPocketOutput  = configs.getObject( "SelectConfC1" ); /// REQUIRED //////////////


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


//// == Calculating / 演算 ==
const arrInput = strInput.split("\n");
let arrOutput;

/// OutputLog and Extract
if( strEndIndex    === "" ){
  engine.log( " AutomatedTask RuntimeLog:" +
              " Range: from " + numBeginIndex + " to (end)" );
  arrOutput  = arrInput.slice( numBeginIndex );
}else{
  engine.log( " AutomatedTask RuntimeLog:" +
              " Range: from " + numBeginIndex + " to " + numEndIndex );
  arrOutput  = arrInput.slice( numBeginIndex, numEndIndex );
}

let strOutput = arrOutput.join("\n");

//// == Data Updating / ワークフローデータへの代入 ==
engine.setData( strPocketOutput, strOutput );


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

/*
Notes:
- When the process reaches this automated task, the Text in the string data is automatically processed.
    - TSV data, Access log, Email body, etc.
    - {Number of lines} = {Number of line breaks} + 1
- The range specified by the start line (Begin Index) and end line (End Index) is extracted.
    - Index starts at zero. The Index on the first line is "0". (10-line text: 0,1,2,3,4,5,6,7,8,9)
        - If a negative value for Index, interpreted as the value subtracted from the number of lines.
        - If "-1" is specified in 10 lines of text, it is the same as if "9" is specified.
    - End Index is optional. If omitted, the entire part will be extracted.
        - Even if End Index is specified to exceed the Index of the last row, the end will be extracted.
    - A case where an empty string is returned.
        - {Begin Index} >= {End Index}
        - {Begin Index} > (Index of the last line)
    - Example of range specification
        - Extract the first 5 lines (0,1,2,3,4,5,,, to 0,1,2,3,4):
            - {Begin Index}: `0`
            - {End Index}: `5`
        - Extract only the 3rd line (0,1,2,3,4,5,,, to 2):
            - {Begin Index}: `2`
            - {End Index}: `3`
        - Delete one first line (header row) (0,1,2,3,,,N to 1,2,3,,,N):
            - {Begin Index}: `1`
            - {End Index}: (empty)
        - Delete one last line (footer row) (0,1,2,,,N-1,N to 0,1,2,,,N-1):
            - {Begin Index}: `0`
            - {End Index}: `-1`

APPENDIX:
- The range specification is the same as the JavaScript `slice ()` method.
    - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
    - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
- 10-line text:0,1,2,3,4,5,6,7,8,9
    - {Begin Index}: `-10`
        - Output Line: 0,1,2,3,4,5,6,7,8,9
    - {Begin Index}: `5`, {End Index}: `20`
        - Output Line: 5,6,7,8,9
- When setting an index by referring to numeric, set an EL expression using arithmetic operations.
    - Extract only (N+1) line; (Index N)
            - {Begin Index}: `#{#q_numN}`
            - {End Index}: `#{#q_numN + 1}`


Notes-ja:
- 案件が自動処理工程に到達した際、文字列型データに保存されているTextが自動的に加工されます。
    - TSVデータ、アクセスログ、メール本文など
    - {行の数} = {改行の数} + 1
- 開始行(Begin Index)と終了行(End Index)で指定された範囲が抽出されます。
    - Index はゼロ始まりです。1行目の Index は "0" です。(10行テキストの場合:0,1,2,3,4,5,6,7,8,9)
        - Index に負の値が指定された場合、テキストの全行数から減算された値と解釈されます。
        - 10行のテキストで "-1" が指定された場合は "9" が指定された場合と同じ処理になります。
    - End Index は省略可能です。省略した場合は末尾までが抽出されます。
        - End Index に最終行の Index を超える値が指定された場合も、末尾までが抽出されます。
    - 空文字列が返されるケース。
        - {Begin Index} >= {End Index}
        - {Begin Index} > (最終行のIndex)
    - 範囲指定の例
        - 先頭の5行を抽出 (0,1,2,3,4,5… ⇒ 0,1,2,3,4):
            - {Begin Index}: `0`
            - {End Index}: `5`
        - 3行目だけを抽出 (0,1,2,3,4,5… ⇒ 2):
            - {Begin Index}: `2`
            - {End Index}: `3`
        - 先頭行(ヘッダ行)を1行削除 (0,1,2,3,…,N ⇒ 1,2,3,…,N):
            - {Begin Index}: `1`
            - {End Index}: `(empty)`
        - 最終行(フッタ行)を1行削除 (0,1,2,…,N-1,N ⇒ 0,1,2,…,N-1):
            - {Begin Index}: `0`
            - {End Index}: `-1`

APPENDIX-ja:
- 範囲の指定方法は、JavaScript `slice()` メソッドと同様です。
    - https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
    - https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/String/slice
- 10行テキストの場合:0,1,2,3,4,5,6,7,8,9
    - {Begin Index}: `-10`
        - Output Line: 0,1,2,3,4,5,6,7,8,9
    - {Begin Index}: `5`, {End Index}: `20`
        - Output Line: 5,6,7,8,9
- 数値型データを参照してインデックスを設定する場合、四則演算を使ったEL式設定が便利です。
    - N+1行目 (Index N) だけを抽出
            - {Begin Index}: `#{#q_numN}`
            - {End Index}: `#{#q_numN + 1}`
*/

Download

2021-08-29 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/ja/addons/multiline-string-get-range-lines-2021/
Addonファイルのインポートは Professional でのみご利用いただけます。
自由改変可能な JavaScript (ECMAScript) コードです。いかなる保証もありません。

Notes

  • 案件が自動処理工程に到達した際、文字列型データに保存されているTextが自動的に加工されます。
    • TSVデータ、アクセスログ、メール本文など
    • {行の数} = {改行の数} + 1
  • 開始行(Begin Index)と終了行(End Index)で指定された範囲が抽出されます。
    • Index はゼロ始まりです。1行目の Index は “0” です。(10行テキストの場合:0,1,2,3,4,5,6,7,8,9)
      • Index に負の値が指定された場合、テキストの全行数から減算された値と解釈されます。
      • 10行のテキストで “-1” が指定された場合は “9” が指定された場合と同じ処理になります。
    • End Index は省略可能です。省略した場合は末尾までが抽出されます。
      • End Index に最終行の Index を超える値が指定された場合も、末尾までが抽出されます。
    • 空文字列が返されるケース。
      • {Begin Index} >= {End Index}
      • {Begin Index} > (最終行のIndex)
    • 範囲指定の例
      • 先頭の5行を抽出 (0,1,2,3,4,5… ⇒ 0,1,2,3,4):
        • {Begin Index}: 0
        • {End Index}: 5
      • 3行目だけを抽出 (0,1,2,3,4,5… ⇒ 2):
        • {Begin Index}: 2
        • {End Index}: 3
      • 先頭行(ヘッダ行)を1行削除 (0,1,2,3,…,N ⇒ 1,2,3,…,N):
        • {Begin Index}: 1
        • {End Index}: (empty)
      • 最終行(フッタ行)を1行削除 (0,1,2,…,N-1,N ⇒ 0,1,2,…,N-1):
        • {Begin Index}: 0
        • {End Index}: -1

Capture

指定範囲の行を抽出します。抽出範囲は開始インデックスと終了インデックスで指定されます。インデックスはゼロ始まりで、先頭行のインデックスはゼロとなります。終了インデックスで指定された行は抽出範囲に含まれない点に注意が必要です。
指定範囲の行を抽出します。抽出範囲は開始インデックスと終了インデックスで指定されます。インデックスはゼロ始まりで、先頭行のインデックスはゼロとなります。終了インデックスで指定された行は抽出範囲に含まれない点に注意が必要です。

Appendix

See also

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

%d人のブロガーが「いいね」をつけました。