Singleline String, Set One Line from Multiline Randomly
Extracts one line randomly from the multi-line string and sets that line in singleline string data. For example, it can be used to test sales copy or to set the response text for lottery results. It is also possible to exclude specific rows from the selection candidates.
Configs
A: Set Multiline String *#{EL}
B: To exclude specific lines, Set Match Words Patterns (eg “//”)#{EL}
C: Select STRING DATA that stores Extracted Line (update) *
Script (click to open)
// GraalJS Script (engine type: 2)
//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){
//// == Config Retrieving / 工程コンフィグの参照 ==
const strCandidateLines = configs.get( "StrConfA" ); /// REQUIRED ///////////
if( strCandidateLines === "" ){
throw new Error( "\n AutomatedTask ConfigError:" +
" Config {A: String} is empty \n" );
}
const strExclusionPatterns = configs.get( "StrConfB" ); // not required ///////
const strPocketExtracted = configs.getObject( "SelectConfC" ); /// REQUIRED ///////////
//// == Data Retrieving / ワークフローデータの参照 ==
// (Nothing. Retrieved via Expression Language in Config Retrieving)
//// == Calculating / 演算 ==
let arrExclusionPatterns = strExclusionPatterns.split("\n"); // empty returns [ '' ]
let arrCandidateLines = strCandidateLines.split("\n");
let arrCandidateLinesExcluded = [];
for( let i = 0; i < arrCandidateLines.length; i++ ){
if( arrCandidateLines[i] === "" ){
arrCandidateLinesExcluded.push( arrCandidateLines[i] );
continue;
}
let boolMatch = false;
for( let j = 0; j < arrExclusionPatterns.length; j++ ){
if( arrExclusionPatterns[j] === "" ){ break; }
if( arrCandidateLines[i].includes( arrExclusionPatterns[j] ) ){
boolMatch = true;
break;
}
}
if( ! boolMatch ){
arrCandidateLinesExcluded.push( arrCandidateLines[i] );
}
}
if( arrCandidateLinesExcluded.length === 0 ){
throw new Error( "\n AutomatedTask UnexpectedStringError:" +
" Population is Zero \n" );
}
let numRand = Math.floor( Math.random() * arrCandidateLinesExcluded.length );
engine.log( " AutomatedTask numSeq: " + numRand +
" (" + arrCandidateLinesExcluded.length + ")" );
let strExtracted = arrCandidateLinesExcluded[ numRand ];
//// == Data Updating / ワークフローデータへの代入 ==
engine.setData( strPocketExtracted, strExtracted );
} //////// END "main()" /////////////////////////////////////////////////////////////////
/*
Notes:
- When the process reaches the automated task, one line in the multiline string is extracted.
- If the multiline string contains a blank line, a blank line (empty string) may be extracted.
- If you want to delete blank lines in advance, place an Automated Task upstream.
- Multiline String, Delete Empty Lines
- https://support.questetra.com/ja/addons/multiline-string-delete-empty-lines-2021/
- Please set the exclusion setting (detection pattern) for each line: multiple patterns can be set.
- If no the exclusion setting (detection pattern), one line is extracted from all candidate lines.
- If a blank line is set in the exclusion setting (detection pattern), it will be ignored.
APPENDIX:
- The random number generation depends on `Math.random()` (JavaScript).
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
Notes-ja:
- 案件がこの自動処理工程に到達した際、複数行文字列(候補行リスト)内の一行を自動的に抽出します。
- 空行を含む複数行文字列の場合、空行(空文字列)が抽出される可能性があります。
- あらかじめ空行を削除しておきたい場合、上流に自動削除工程を配置します。
- 複数行文字列, 空行の削除
- https://support.questetra.com/ja/addons/multiline-string-delete-empty-lines-2021/
- 除外設定(検出文字パターン)は、各行に設定してください(複数のパターンを設定できます)
- 除外設定(検出文字パターン)が未設定の場合、全ての候補行から一行抽出されます。
- 除外設定(検出文字パターン)に空行が設定された場合、無視されます。
APPENDIX-ja:
- 乱数の生成分布は `Math.random()` (JavaScript) に依存します。
- https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Math/random
*/
Pingback: Singleline String, Set Nth Line of Multiline – Questetra Support