Skip to main content
Inspiring
February 24, 2020
Question

Getting number of records in data attached to document

  • February 24, 2020
  • 7 replies
  • 1671 views

Hi

Could anyone tell me how to get the number of records in the data attached to a document?

I've been trying to write a script which needs this to work. I've been looking through the data model at DataMerge options and preferences but I can't seem to find anything that stores the data count.

Thanks!

7 replies

Community Expert
February 26, 2020

Just answered in the other thread, but I'll repeat it here.

 

Question was:
How can I know the data source file of a document template that is prepared for data merge?

It seems there is no property in the scripting DOM for this. I'd expect this perhaps with documentPreferences or dataMergePreferences. But no, it's missing.

 

The solution:

Export your document to IDML and look into Preferences.xml of the IDML container.

In the third line you should find the DataSourceFile attribute:

<DataMerge DataSourceFile="C:\folderName\fileName.txt" />

 

Regards,
Uwe Laubender

( ACP )

Community Expert
February 26, 2020

Hi Ashley,

it seems you cannot reply to this thread for unknown reason:

https://community.adobe.com/t5/indesign/cannot-reply-to-post/td-p/10947947

 

So I am trying a reply to see if something's wrong on the thread in the forum.

 

Regards,
Uwe Laubender

( ACP )

Community Expert
February 25, 2020

Hi Colin,

the other workaround:

Load the data source to a new document and get the range from there.

 

Regards,
Uwe Laubender

( ACP )

Community Expert
February 25, 2020

Hi Ashley,

then I suggest you set recordSelection first to RecordSelection.ALL_RECORDS.

Did not test this, but I hope that the range will adapt.

 

Regards,
Uwe Laubender

( ACP )

Inspiring
February 25, 2020

Hi

 

Unfortunately it 'remembers' the previous range, so I'd need some way to change the range to specify the full range of records (and I guess if I could do that I would already have the total number of records!)

 

I'll tinker with it some more.

Colin Flashman
Community Expert
Community Expert
February 25, 2020

Yes. It's a bug. I usually create a variable for the total amount of records by doing a line-count of data within the text file being referenced.

csvFile.open('r'); 
var maxRange = csvFile.read().split(/\n/).length - 1;  //total length of actual records (not including the header)
csvFile.close(); 

 

If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!
Community Expert
February 25, 2020

Hi Brian,

property recordNumber is something else. From my German InDesign on Windows 10:

 

 

Regards,
Uwe Laubender

( ACP )

Community Expert
February 25, 2020

Hi Ashley,

see into other properties of dataMergePreferences as well:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#DataMergePreference.html

 

Set recordSelection to RecordSelection.RANGE

And exploit property recordRange that returns a string.

 

var doc = app.documents[0];
doc.dataMergeProperties.dataMergePreferences.recordSelection = RecordSelection.RANGE;
var rangeString = doc.dataMergeProperties.dataMergePreferences.recordRange;
var lastNumberString = rangeString.split("-")[1];
var lastNumberAsInteger = parseInt( lastNumberString , 10 );

alert( lastNumberAsInteger );

 

Regards,
Uwe Laubender

( ACP )

Inspiring
February 25, 2020

Hi

 

That looks promising, but currently returns 500 instead of the actual 1500 records - that's because the last time I exported I only selected records 1-500.

I guess it should be possible to tweak this to get all the records all the time - I'll have a look!

brian_p_dts
Community Expert
Community Expert
February 25, 2020

doc.dataMergeProperties.dataMergePreferences.recordNumber;

Inspiring
February 25, 2020

Hmm - that just reads 1, so it's just the record I'm on at the moment?

I tried going to the last record before running the script and it still came out as 1.

Do I need to do something else?

I'm using var doc = app.activeDocument;

Cheers