Forum Discussion

BenColey's avatar
BenColey
Community Member
2 months 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 ...
  • SamHill's avatar
    29 days ago

    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();