JavaScript use mergeRecords() for all records
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
