#target indesign
var doc = app.activeDocument;
var pathFile = doc.extractLabel("pathFile");
var file = File(pathFile).openDlg("Select a table with data in .csv format", "*.csv");
if (file) {
doc.insertLabel ("pathFile", file);
file.open("r");
var row = file.read().split('\n');
file.close;
};
if (row) {
app.generalPreferences.pageNumbering = PageNumberingOptions.SECTION;
app.doScript(main, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, 'InsertDataTable');
};
function main(){
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = "<<\\d+:\\d+>>";
var findResults = doc.findGrep();
if (findResults.length > 0) {
var idTextArr = [];
for ( var i = 0; i < findResults.length; i++ ) {
idTextArr.push(findResults[i].contents);
};
idTextArr = arrayCompress(idTextArr);
for ( var i = 0; i < idTextArr.length; i++ ) {
var id = idTextArr[i].replace(/<</g, "").replace(/>>/g, "");
var r = --id.split(":")[1];
var c = --id.split(":")[0];
app.findGrepPreferences.findWhat = idTextArr[i];
app.changeGrepPreferences.changeTo = row[r].split('\t')[c];
doc.changeGrep();
};
} else { alert ("The replaced sections of text in the format\n<<ColumnNumber:RowNumber>> not found")};
};
function arrayCompress(array){
var str = array.sort().join('\r')+'\r';
str = str.replace(/([^\r]+\r)(\1)+/g,'$1');
str = str.replace(/\r$/,'');
return str.split('\r');
};