Hi @Hugo290714246dr1, I was wondering how this works, and @Brito Haroldo is right on the money.
Here is a demonstration script, showing a function that inserts a DataMergeTextPlaceholder at an InsertionPoint of your choosing. I hope you can use the function, or adapt it to your needs.
I've put two example insertionPoints to try—for example 1, just put the text insertion cursor into a story somewhere, and for example 2, run it with a document containing a table.
You will have to set the field name you want. Because you want it dynamically, you will probably be generating the fieldNameOrIndex parameter elsewhere, for example:
myFieldName = "mycolumn" + i;
Or if it is easier in your case, you can use the numerical index instead of the field name—passing i to the function will insert data merge field i. Either of these approaches could sit in a loop.
- Mark
/**
* Demonstrates a function for adding a DataMergeTextPlaceholder.
* @author m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/api-to-insert-a-reference-to-merge-data-field-into-a-cell/m-p/14273192
*/
function main() {
var doc = app.activeDocument;
// NOTE: the field name must match a data merge field in the document.
var myFieldName = 'mycolumn';
// example 1: the active insertion point:
var ip = doc.selection[0];
// example 2: an insertion point in the 3rd cell of the first table of the document:
// var ip = doc.stories.everyItem().tables.everyItem().getElements()[0].cells[2].insertionPoints[0];
var placeholder = insertDataMergeTextPlaceholder(doc, ip, myFieldName);
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Add Data Merge Text Placeholder');
/**
* Inserts a DataMergeTextPlaceholder
* at the specified InsertionPoint.
* @author m1b
* @version 2023-12-03
* @param {Document} doc - an Indesign Document.
* @param {InsertionPoint} insertionPoint - the target InsertionPoint.
* @param {String|Number} fieldNameOrIndex - the name, or index, of a valid DataMergeField.
* @returns {DataMergeTextPlaceholder}
*/
function insertDataMergeTextPlaceholder(doc, insertionPoint, fieldNameOrIndex) {
if (
doc == undefined
|| doc.constructor.name !== 'Document'
)
throw Error('insertDataMergeTextField: bad `doc` supplied.');
if (
insertionPoint == undefined
|| insertionPoint.constructor.name !== 'InsertionPoint'
|| !insertionPoint.isValid
)
throw Error('insertDataMergeTextField: bad `insertionPoint` supplied.');
// datamerge objects
var dm = doc.dataMergeProperties,
dmFields = dm.dataMergeFields,
index = Number(fieldNameOrIndex);
if (fieldNameOrIndex.constructor.name == 'String') {
// get the index to the named field
for (index = 0, len = dmFields.length; index < len; index++)
if (dmFields[index].fieldName == fieldNameOrIndex)
break;
}
var field = dmFields[index];
if (!field.isValid)
throw Error('Did not find a valid data merge field "' + fieldNameOrIndex + '".');
// add the placeholder at the insertionPoint
var placeholder = doc.dataMergeTextPlaceholders.add(insertionPoint.parentStory, insertionPoint, field);
// refresh
insertionPoint.parentStory.recompose();
if (placeholder.isValid)
return placeholder;
};