Skip to main content
Inspiring
March 24, 2023
Answered

Using a Script to access InDesign’s DataMerge fields

  • March 24, 2023
  • 2 replies
  • 2099 views

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!

This topic has been closed for replies.
Correct answer Manan Joshi

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

2 replies

Community Expert
March 25, 2023

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

-Manan
Inspiring
March 24, 2023

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

Manan JoshiCommunity ExpertCorrect answer
Community Expert
March 25, 2023

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

-Manan
Inspiring
March 27, 2023

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.