I have some xml or excel I want to loop through this document in InDesign script and insert values on defined places
this is excel or xml
I don't have so much scripting experiences so I can only this piece of code
var doc = app.activeDocument;
var myFile = File("~/Desktop/test.xml");
var textExcel = doc.textFrames.add();
textExcel.geometricBounds = [50, 80, 10, 150];
textExcel.place(myFile);
but now how I can get a single value? for example in indesign template first paragraph should look like table --> lorem--> 150
Here is the possible solution:
var doc = app.activeDocument;
// get a text from the XLSX file
var inputFile = File("d:/table.xlsx");
var temp_frame = doc.textFrames.add();
temp_frame.place(inputFile);
var text = temp_frame.parentStory.contents;
temp_frame.remove();
// make a table from the text
var rows = text.split('\r');
var table = [];
for (var i = 1; i < rows.length; i++) table.push(rows[i].split('\t'));
// loop through the table and make the cards
for (var i = 0; i < table.length; i++) {
var title = table[i][0];
var description = table[i][1];
var price = table[i][2];
var card = make_card(title, description, price);
// move the card to some places
card.move([10,10]);
card.move(undefined, [i*75, 0]);
}
// the function to create and return a card
function make_card(title, description, price) {
var doc = app.activeDocument;
var title_frame = doc.textFrames.add();
title_frame.geometricBounds = [20, 80, 30, 150];
title_frame.contents = title;
var description_frame = doc.textFrames.add();
description_frame.geometricBounds = [30, 80, 80, 150];
description_frame.contents = description;
var price_frame = doc.textFrames.add();
price_frame.geometricBounds = [80, 80, 100, 150];
price_frame.contents = price;
// apply styles to the texts in the card
apply_style('title', title_frame);
apply_style('description', description_frame);
apply_style('price', price_frame);
var group = doc.groups.add([title_frame, description_frame, price_frame]);
return group;
}
function apply_style(style_name, frame) {
var doc = app.activeDocument;
try {
var style = doc.paragraphStyles.itemByName(style_name);
frame.paragraphs.everyItem().appliedParagraphStyle = style;
} catch(e) {}
}
This is the XLSX table:
Here is the result layout (3 cards):
It creates cards from XLSX file (applies the styles to the texts inside cards, why not?) and put them on the page of the current document.