Copy link to clipboard
Copied
I have a voucher template where I am using XML data to format the greetings while data merge is handling the voucher codes. I am using XML for the greetings because their formatting varies from batch to batch, but are the same within each batch.
Now, someone asked me if it would be possible to have the codes appear within the XML-based greetings as well. So basically what I would like to do is to have a data merge tag within the XML-document so I can run a data merge on it afterwards to combine the static text and the variable data. Do anyone know if that is possible?
I have done some testing by adding
<<code>>
into the XML and that did not work out. But perhaps there is a more sophisticaded or advanced way to achieve this? I am confortable with scripting if that is needed.
Hi Per,
how do you identify the texts where a data merge field should be added?
If you can identify this you could write a script that will load a datamerge sourcetext file and adds the necessary fields.
To get what it is required to do this, analyze a valid data merge template document.
DOM documentation can be found here:
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#about.html
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Hi Per,
how do you identify the texts where a data merge field should be added?
If you can identify this you could write a script that will load a datamerge sourcetext file and adds the necessary fields.
To get what it is required to do this, analyze a valid data merge template document.
DOM documentation can be found here:
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#about.html
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Ah, so you mean I could add the datamerge tag to the xml content after it's been loaded? Sounds interesting. I will look into that. Thank you!
Copy link to clipboard
Copied
Yes, I was able to do it that way. Thank you for the valuable input! Here is a dummy script that relinks the data files and adds a datamerge tag where the word "giftcode" appears. I will improve it for my application but as a proof of concept it works like a charm.
#target indesign
var doc = app.activeDocument;
var currentPath = doc.filePath;
var xmlFile = new File(currentPath+"/"+"design-data.xml");
var csvFile = new File(currentPath+"/"+"code-data.csv");
var links = doc.links;
var pages = doc.pages;
var searchTag = "giftcode";
for(var i=0; i<links.length; i++) {
var link = links[i];
// cannot use the built-in .linkType property because ID incorrectly lists csv as xml
var linkName = link.name;
var linkType = linkName.split('.').pop();
if (linkType == "xml") {
link.relink(xmlFile);
}
if (linkType == "csv") {
link.relink(csvFile);
}
}
for(var i=0; i<pages.length; i++) {
var textFrames = pages[i].textFrames;
for(var j=0; j<textFrames.length; j++) {
var textContents = textFrames[j].texts[0].contents;
var searchPosition = textContents.search(searchTag);
if(searchPosition != -1) {
var story = textFrames[j].parentStory;
var dataMergeFields = doc.dataMergeProperties.dataMergeFields;
for(var k=0; k<dataMergeFields.length; k++) {
if(dataMergeFields[k].fieldName == searchTag) {
doc.dataMergeTextPlaceholders.add(story,searchPosition,dataMergeFields[k]);
}
}
}
}
}
var pdfFile = new File(currentPath+"/"+"export.pdf");
doc.dataMergeProperties.exportFile(pdfFile);
Copy link to clipboard
Copied
Yep. Exactly that was the idea!
Thank you for sharing the code.
Regards,
Uwe Laubender
( ACP )