Skip to main content
nicholasw52485545
Inspiring
March 31, 2021
Answered

split() - don't show 'undefined' text for blank data cell

  • March 31, 2021
  • 2 replies
  • 833 views

I'm an animator working with JS in AE expressions and I have to make dynamic charts with CSV data controlling points and values. That's all going fine, but what I'm stuck on is that some name value are two words, some are single, and I need to break up the double words onto two lines. When I do it the way I found, the single words display as the word on the first line, and the word 'undefined' on the second line. Obviously because there isn't a second value.

 

Is there a better way to script the line break so there isn't an undefined value? It needs to be flexible for one or two words in either cell.

 

Here's the code I have so far :

(I've simplified the for loop cos it linked to other layer names)

i=**forLoop;

nameText = footage("Spreadsheet.csv").dataValue([0,i]);

nameText.split(" ")[0] + ['\n'] + nameText.split(" ")[1]

This topic has been closed for replies.
Correct answer Dan Ebberts

Something like this should work:

 

nameText = footage("Spreadsheet.csv").dataValue([0,i]);
nameSplit = nameText.split(" ");
nameSplit.length > 1 ? nameSplit[0] + '\r' + nameSplit[1] : nameText

2 replies

Mathias Moehl
Community Expert
Community Expert
April 2, 2021

Great that Dan already provided a solution!

Extra Tip: For dynamic graphs based on spreadsheet data, the chapter Tempaltes & Automation of my free eBook Motion Graphics in After Effects that Speaks to Your Brain also contains a lot of helpful infos.

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
March 31, 2021

Something like this should work:

 

nameText = footage("Spreadsheet.csv").dataValue([0,i]);
nameSplit = nameText.split(" ");
nameSplit.length > 1 ? nameSplit[0] + '\r' + nameSplit[1] : nameText
nicholasw52485545
Inspiring
March 31, 2021

That worked perfectly! thank you so much. 

I think I understand the last line, except the ': nameText'

Does that say that the splits are part of the same item?

Total beginner.

Dan Ebberts
Community Expert
Community Expert
March 31, 2021

No, that's the else part of the conditional operator (?). If the split text isn't longer than one element, it just uses the original text.