Copy link to clipboard
Copied
Hi all,
I've recently gotten into scripting for InDesign thanks to the fantastic book Scripting InDesign CS3/4 with JavaScript written by Peter Kahrel. This helped me to successfully write a couple of scripts, however, I need my scripts to work within Data Merge, and I can find precious little information on how to script Data Merge and unfortunately it wasn't covered in the book.
What I want to do is simply perform a script on all the records without affecting the template file. The data is of different lengths for each record. For instance, record 1 may contain a table of 80 rows while record 2 may contain a table of 100 rows. I figured out how to write a script to delete the empty rows, however, I want to delete them for that record only. I figured that a way to do this would be to merge the data of each record into a new InDesign file, and run the script to delete the empty rows in there. However, when using the mergeRecords() function, I only seem to get the first record. Is it possible to make this loop through the number of records or something? I asked ChatGPT that insists I should use createMergedDocument() but that doesn't seem to be a function (I am using JavaScript with ExtendScript in VS code btw).
So far, I have this:
// Update the data source
app.activeDocument.dataMergeProperties.updateDataSource();
// Set data merge properties
app.activeDocument.dataMergeOptions.fittingOption = Fitting.PROPORTIONAL;
app.activeDocument.dataMergeOptions.centerImage = false;
app.activeDocument.dataMergeOptions.linkImages = true;
app.activeDocument.dataMergeOptions.removeBlankLines = true;
// Find the number of records. The record range returns a string in the form of x-y. We want to know y and turn it into an integer.
var myRange = app.activeDocument.dataMergeProperties.dataMergePreferences.recordRange;
var lastNumberString = myRange.split("-")[1];
var totalRecords = parseInt(lastNumberString,10);
// Merge records
app.activeDocument.dataMergeProperties.mergeRecords();I would like to use my variable totalRecords to create a for-loop for the mergeRecords() function, but I am not sure how to go about this, since it doesn't seem like mergeRecords() takes any variables other than a text string. If there is another method that is great too. Alternatively, if you can recommend some good reading material (for beginners) about data merge scripting I would be very appreciative too! I've been using this for reference: https://www.indesignjs.de/extendscriptAPI/indesign-latest/#DataMerge.html but I am still having trouble understanding how to use this.
Cheers,
Heidi
Hi @Jurre37971467hccf, I searched in my previous scripts for the "mergeRecords" method of Document and I found this and this. They might help, but let us know if not.
- Mark
Copy link to clipboard
Copied
Hi @Jurre37971467hccf, I searched in my previous scripts for the "mergeRecords" method of Document and I found this and this. They might help, but let us know if not.
- Mark
Copy link to clipboard
Copied
Thanks a lot, Mark! This looks very promising. I will need some time to fully understand it and integrate it with my scripts, then I will let you know if it is solved.
Copy link to clipboard
Copied
It worked! Thank you so much. 🙂
Just one little question about it: why is it that Record.Selection.ALL_RECORDS does not seem to work in combination with .mergeRecords()?
I adapted the script as follows:
// Select the template document
var doc = app.documents.itemByName('TestDoc.indd');
// Select a data source file
doc.dataMergeProperties.selectDataSource("/Users/HeidiSchigt/OneDrive - Scope Biosciences B.V/Documenten - Scope Biosciences B.V_/Employees/Wet Lab/Manual templates/All plant manuals/0DataValSheet.csv");
// Update the data source
// app.activeDocument.dataMergeProperties.updateDataSource();
// Set data merge properties
doc.dataMergeOptions.fittingOption = Fitting.PROPORTIONAL;
doc.dataMergeOptions.centerImage = false;
doc.dataMergeOptions.linkImages = true;
doc.dataMergeOptions.removeBlankLines = true;
// Find the number of records. The record range returns a string in the form of x-y. We want to know y and turn it into an integer.
var myRange = doc.dataMergeProperties.dataMergePreferences.recordRange;
var lastNumberString = myRange.split("-")[1];
var totalRecords = parseInt(lastNumberString,10);
// Merge records
for(var i = 1; i <= totalRecords; i++){
doc.dataMergeOptions.createNewDocument = true;
doc.dataMergeProperties.dataMergePreferences.recordSelection = RecordSelection.ONE_RECORD;
doc.dataMergeProperties.dataMergePreferences.recordNumber = i
doc.dataMergeProperties.mergeRecords();
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now