Copy link to clipboard
Copied
I’m trying to work on making a script to automate a portion of our work flow and trying to access the content being loaded in via the Data Merge Function. My hope is to be able to access the names of the fields that are loaded in, and then access the items within that field, however i’m having no luck with the script recognizing names of fields. what ive tried is:
app.activeDocument.dataMergeProperties.dataMergeFields.count() – this returns the correct number of fields loaded in
app.activeDocument.dataMergeProperties.dataMergeFields.item[0] – this returns as undefined, and is not an object
app.activeDocument.dataMergeProperties.dataMergeFields.toSource() – this returns ‘resolve(“/document[@id=23]/@data-merge-properties/data-merge-field”)’
app.activeDocument.dataMergeProperties.dataMergeFields[0] – this returns [object DataMergeField]
I’m referencing indesignjs.de but so far has not cleared things up, any help would be appreciated!
1 Correct answer
To get a list of placed text fields and their values as arrays you can try something like the following
var flds = [], vals = []
var tph = app.documents[0].dataMergeTextPlaceholders
var lgth = tph.everyItem().length
var ip = tph.everyItem().storyOffset
for(var i = 0; i < tph.length; i++){
flds.push(tph[i].field.fieldName)
vals.push(tph[i].parentStory.insertionPoints.itemByRange(ip[i].index, ip[i].index + lgth[i]).contents)
}
Here the vals array contains the values and flds array contains
...Copy link to clipboard
Copied
i got to the field name with:
app.activeDocument.dataMergeProperties.dataMergeFields[0].fieldName
so now just to figure out if i can access the data within the fields and if i can create an array of the field anmes and data
Copy link to clipboard
Copied
To get a list of placed text fields and their values as arrays you can try something like the following
var flds = [], vals = []
var tph = app.documents[0].dataMergeTextPlaceholders
var lgth = tph.everyItem().length
var ip = tph.everyItem().storyOffset
for(var i = 0; i < tph.length; i++){
flds.push(tph[i].field.fieldName)
vals.push(tph[i].parentStory.insertionPoints.itemByRange(ip[i].index, ip[i].index + lgth[i]).contents)
}
Here the vals array contains the values and flds array contains the fields
-Manan
Copy link to clipboard
Copied
Thanks for the response Manan! i tested this code and did an alert to check what some of the variables returned, but flds, vals, lgth, and ip all returned blank alerts. does this bit of code require that the placeholders be added in the document? doing a quick test of that did return the name of field that i placed into the copy but not the values. If this is the case does it mean i wont be able to access the data that is loaded into the data merge after choosing a source without actually placing text holders down into the document?
I know it seems odd that i would want to do it like that, but ive developed a method for loading in glossaries for books using Liocs InlineMerge Script and some javascript'd hyperlinks to generate popups with the glossary term and definition, but i requires manually entering in the glossary terms and javascript for each hyperlink. My hope was to access the data values in the datamerge without having to place down textplaceholders but so far it has eluded me.
Copy link to clipboard
Copied
The line of code that I gave before i.e.
app.documents[0].dataMergeProperties.dataMergeFields.everyItem().fieldName
should give you the fieldnames without having to place any textplaceholders in the document. Just importing the CSV in the document should suffice. Give it a try
-Manan
Copy link to clipboard
Copied
Yes, this does give me the field names, but i'm hoping to access the actual data contained within those fields. For instance if my data doc (a tab-deliminited text file) has the column named Term, and in that column are items: Abalone, Bacon, Cheddar, etc..., i can get that field name Term with your code, after loading the source in data merge, but as of yet am unable to access the items Abalone, Bacon, Cheddar, etc... I might be mistaken in how InDesign's DataMerge function works as i'm assuming it loads in all the data, but it may just use the field names and then access the linked data doc.
Copy link to clipboard
Copied
No you can't get the data of the columns without placing it on the document. I don't see any method or property to do so in the DOM documentation. For that probably you would have to parse the CSV yourself. There are libraries to parse CSV that would work with Extendscript as well, you can search Github for the same.
-Manan
Copy link to clipboard
Copied
Thats what i thought as well, thanks for confirming that. The DOM documentation is a bit difficult to understand sometimes since i dont have much experience with InDesign scripting yet. Parsing the data document was going to be my next option but since it was loaded into the datamerge i hoped to be able to skip doing it again. Thanks again!
Copy link to clipboard
Copied
To get the name of all the fields in one shot you could do something like the following
app.documents[0].dataMergeProperties.dataMergeFields.everyItem().fieldName
-Manan

