Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Evaluate by Indesign javascript if the data in an table cell is numeric or alphanumeric

New Here ,
May 31, 2011 May 31, 2011

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

TOPICS
Scripting
2.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 31, 2011 May 31, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 31, 2011 May 31, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 02, 2011 Jun 02, 2011

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)).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 02, 2011 Jun 02, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 02, 2011 Jun 02, 2011

Yeah, you're right. sigh.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jun 02, 2011 Jun 02, 2011
LATEST

How about Number()? Number("4You") returns NaN. I don't know if there are other gotchas with it though.

Jeff

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines