Forum Discussion

BenColey's avatar
BenColey
Community Member
15 days ago

Converting a list to comma separated value using JavaScript

I need to turn a list (user defined) into a string of comma separated values that can then be copied to the clipboard, within Storyline 360.

Essentially, I want people to be able to paste a list of ID numbers and have that converted to a string of those numbers, separated with a comma.

My JavaScript knowledge is very basic and I am going round in circles trying to get anything to display.

I have the TextEntry variable setup where users can paste their numbers into, a button they press to perform the conversion and another variable OutputString setup to display the conversion. 

Any help will be greatly appreciated.

  • Probably a bit late on the response for you BenColey but if anybody else finds this post who needs something similar, I've included an example Storyline file.

    In the example there are two Storyline variables unprocessedText and processedText.

    The input text field, sets the Storyline variable unprocessedText and the processed text, after being worked on via JavaScript is saved to the processedText variable, displayed in the text box labelled Output.

    There is a JavaScript function called processText which is initialised when the timeline starts on the slide.

    window.processText = function()
    {
    	const player = GetPlayer();
    	// set the unprocessedText Storyline variable
    	const unprocessedText = player.GetVar('unprocessedText');
    	// process the data
    	let processedText = unprocessedText
    		.split('\n')                // Split by line breaks
    		.map(line => line.trim())   // Trim whitespace from each line
    		.filter(line => line)       // Filter out any empty lines
    		.join(', ');                // Join with a comma and space
    	// set the processedText Storyline variable
    	player.SetVar('processedText',processedText);
    }

    The function is then called via the Process button "user clicks" trigger.

    window.processText();