Copy link to clipboard
Copied
Hello!
I have a little problem, to be honest i have several of them, but lets begin with the first one:
i have to find out if the data in the columns of an table are numeric or alphanumeric and then to align them according to the data-type. can anyone tell me how this can be evaluated in javascript in indesign.
thank you in advance
Copy link to clipboard
Copied
You can check parseInt(s, 10) !== NaN to see if a string s contains a number.
So if you have a table t, you could do something like:
if (parseInt(t.rows[0].columns[0].cells[0].contents,10) !== NaN) {
t.rows[0].columns[0].cells[0].texts[0].justification = Justification.RIGHT_ALIGN;
} else {
t.rows[0].columns[0].cells[0].texts[0].justification = Justification.CENTER_ALIGN;
}
to right-align numbers and center non-numbers. of course you'll need to loop. And probably you can just check one cell in each column and set the whole column, rather than checking all cells in the table? It all depends.
I guess it also depends on what you mean by alphanumeric. You could test with a regular expression. s.match(/^\w+$/) will return true if s contains only alphanumeric characters (including the underscore _).
Probably using s.match(/^\d+$/) would be faster than parseInt() and would be true if s contains only digit characters, 0-9. Maybe you need leading minus signs too though? Decimal points? add them to the regexp, or use parseInt.
Copy link to clipboard
Copied
thanks for the answer, but somehow the parseInt doesn't work for me.
the if you coded is allways true no metter what is the content of the cell.
![]()
Copy link to clipboard
Copied
Sorry, I was sloppy and I didn't actually test it in InDesign's javascript impementation, and I somehow forgot that you cannot test for NaN with the equality operator. But you might be better off with the regexp solution. Anyhow, instead of parseInt(s, 10) !== NaN instead you can use !isNaN(parseint(s,10)).
Copy link to clipboard
Copied
I'm not sure parseInt() will solve his problem at all.
parseInt("4You") will return 4 and just drop out the rest.
To find pure numbers you'd probably want to strip out all numbers (and decimals) and test for an empty string...
Harbs
Copy link to clipboard
Copied
Yeah, you're right. sigh.
Copy link to clipboard
Copied
How about Number()? Number("4You") returns NaN. I don't know if there are other gotchas with it though.
Jeff
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more