Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
doc.dataMergeProperties.dataMergePreferences.recordNumber;
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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 )
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
Hi Brian,
property recordNumber is something else. From my German InDesign on Windows 10:
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
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 )
Copy link to clipboard
Copied
Hm…
If this does not help you may load the data source again.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
Hi Colin
EDIT - Just tried it and I'm getting a 'csvFile is undefined' message. I can set up the var, but how do I tell it to look at the current data source?
Thanks
Copy link to clipboard
Copied
You need to define and open the CSV file first.
var csvFile = File("path/to/file/thedata.csv");
For example, if the file is on your desktop and called data.csv, then you would do:
var csvFile = "~/Desktop/data.csv";
csvFile.open('r');
...
Copy link to clipboard
Copied
Hello, old post but i'm in the same situation, i need to get the total of reccords. But i don't want to upload a news db file, i would like to get the one is in link. it's a txt can you help me ?
Copy link to clipboard
Copied
Hi @Morgane33604108ep9w ,
see into this workaround:
Load the data source to a new document and get the range from there.
Regards,
Uwe Laubender
( Adobe Community Expert )
Copy link to clipboard
Copied
Thank i find an other solution 🙂 i find the right file by a research of the txt or csv file in the links
Copy link to clipboard
Copied
Hi Colin,
the other workaround:
Load the data source to a new document and get the range from there.
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
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 )
Copy link to clipboard
Copied
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 )