Count number of Lines in cell

I have a text entry, and I’d like to count the number of lines in that text entry, for example:

Test1
Test2
Test3
Test4
Test5

Would output as 5,

I tried doing word count,

But the issue is if there is multiple words on a line it adds those words to the word count.

Any idea on how you would just count number of lines?

Thanks for your help!

You could use a Split Text column, splitting on a line break, then use a Rollup to get a count.
Alternatively, a small JavaScript snippet would do the job.

2 Likes

I’d probably go for the JavaScript option:

return p1.split('\n').length;

2 Likes

Thank you so much that Javascript function works perfectly!!

1 Like

Hi Darren,

Thank you for your suggestion—it worked perfectly to show the count correctly! However, I’ve noticed an issue: while it displays in real time on a computer, it doesn’t update in real time on mobile.

I’ve already ensured the phone app is updated to the latest version, but for some reason, the JavaScript function isn’t reflecting changes in real time on mobile.

Do you have any suggestions for achieving this more easily using Glide’s computed columns?

I really appreciate your help—thank you!

I’ve never had javascript fail to update as expected on desktop or mobile. I’d be curious to know more about your setup. Is the column you feed into the javascript a basic text column? Usually when a parameter value changes, that triggers the javascript to rerun.

Are you inside of an Add or Edit form by any chance?

Have you tried Darren’s other solution to see if it works any better?

2 Likes

I recall Duncan having syncing problems before, not once but twice.

I wonder if it’s the same problem this time. He suggested here that he uses “too many rows and columns”.

2 Likes

Yes, I believe the issue could indeed be related to the number of rows and columns used in my app. The app syncs perfectly when I make changes, but I’m wondering if the JavaScript function isn’t displaying the line count due to the volume of rows/columns or perhaps another underlying issue.

Here’s how I’ve set it up: the JavaScript function automatically updates the line count and displays that number as the title text beneath the text entry. This works flawlessly on desktop but doesn’t show up on mobile. Interestingly, when I use a simpler setup—like a word count function to display the word count as the title text—it works on both desktop and mobile without any issues.

It seems like the complexity of handling the line count might be the factor here, but I’d appreciate any insights or suggestions you might have.

The number from the JavaScript column is placed as the text title to display the count. For now, I’ve reverted to simply using the word count column since the number is close enough, though not exact (in cases where there are multiple words per line).

Here’s the issue: while the JavaScript function calculates and shows the correct line count in the Glide table, when I set it to display as the title, it only appears correctly on desktop, not on mobile. However, all other app changes are syncing perfectly, so the sync itself doesn’t seem to be the problem.

Interestingly, there’s no issue when I use the word count column instead of the JavaScript column to display the number as the title—this works fine on both desktop and mobile. It seems the problem arises specifically with the JavaScript column in combination with the mobile display.

I’d appreciate any advice on resolving this!

Is this one specific mobile device, or do you have the same problem on other mobile devices as well? I wonder if the javascript is crashing. I’ve seen that happen on rare occasions if bad or null data is being passed into the javascript and it can’t handle it properly. I’ve had cases where I’ve needed to refresh the browser to get it to run again. In those cases, I usually wrap my code in a try/catch so it can gracefully handle uncaught errors.

Try this once.

try {
  return p1.split('\n').length
} catch {
  return 0;
}

If that doesn’t work, you still have Darren’s alternative method which should work just as well if not better. All the javascript solution does is perform the count with one column while the other method uses two columns. Not that much of a difference.

3 Likes

Thank you for your insights! To answer your first question, this issue is currently happening on one specific mobile device, as I haven’t tested it on others yet. It’s possible that null or unexpected data is causing the JavaScript to behave this way, as you mentioned.

As for Darren’s method, I’m trying to use the Split Text column to split on a line break and then applying a Rollup to count the lines. I’ve tried splitting on /n and \n, but I’m not sure if I’m missing something here. Could you clarify the exact delimiter for the Split Text column or if there’s an additional step in the Rollup configuration? I want to ensure I’m implementing it correctly.


Thanks for your help!

Just press the Enter key as a carriage return. You don’t need any special character combinations for the split character. Enter is all you need.

3 Likes

Wow that was easy! Thank you for your help Jeff (and Darren)! :grin:

1 Like

I played around some more and it seems like the javascript column does not play well with carriage returns. Not sure why, or if I’m missing something obvious. Normally it should work.

The fix would involve replacing all carriage returns with some other kind of delimiter using a template column before passing that into a javascript column to split on that delimiter. Kind of pointless at that point, since the alternative method works better.

2 Likes

I see, well, I’m glad we’ve got the other solution in the meantime! Thanks so much for helping troubleshoot this—it’s much appreciated! :blush:

1 Like