如何使用 JavaScript 中的脚本在 InDesign 中创建自动目录
How to create automatic catalog in InDesign with scripting in JavaScript
我有以下代码,非常适合将 excel 导入 InDesign 并创建自动网格目录。
var doc = app.activeDocument;
// get a text from the XLSX file
var inputFile = File("~/Desktop/test.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'));
// width and height of cards, gaps, properties of the grid
var card_width = 70;
var card_height = 80;
var gap = 5;
var cols = 2;
var rows = 3;
var cards_per_page = cols * rows;
// calculate and add pages
var cards = table.length;
var pages_to_add = Math.ceil(cards / cards_per_page) - 1;
while(pages_to_add--) doc.pages.add();
var page_num = 0; // start page
var start_point = [10,10]; // start point for a first card on a page
main_loop:
for (var i = 0; i < cards; i++) {
for (var row = 0; row < rows; row++) {
for (var col = 0; col < cols; col++) {
// break the loop if there is no more rows in the table
if (table.length == 0) break main_loop;
var table_row = table.shift(); // take a first row from the table
var title = table_row[0];
var description = table_row[1];
var price = table_row[2];
// make a card
var card = make_card(title, description, price);
// send the card to the some page and move at some place
card.move(doc.pages[page_num]);
card.move(start_point);
card.move(undefined, [(card_width + gap)*col, (card_height + gap)*row]);
}
}
if (i > (page_num-1) * cards_per_page) page_num++; // increase the page number
}
// 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;
var group = doc.groups.add([title_frame, description_frame, price_frame]);
return group;
}
但是如果我想要 3 页呢?每个页面都会有不同的模板
第 1、4、7 页...将有此模板
第 2、5、8 页 ...
第 3、6、9 页 ... 将使用网格模板
我不知道如何在 javascript 中解决它。
这是模板 IDML:https://app.box.com/s/muqvroawbwv8zydwdizfc3xsmyzmenmy
代码如下:
var doc = app.activeDocument;
// get a text from the XLSX file
var inputFile = File("~/Desktop/test.xlsx");
var temp_frame = doc.textFrames.add();
temp_frame.place(inputFile);
var text = temp_frame.parentStory.contents;
temp_frame.remove();
// split the text by rows
var rows = text.split('\r');
rows.shift(); // remove the first row (a header of the table)
// add pages if there is more than 10 rows
var i = rows.length - 10;
while (i > 0) {
doc.pages[0].duplicate(LocationOptions.AT_END);
i -= 1; if (i < 0) break;
doc.pages[1].duplicate(LocationOptions.AT_END);
i -= 5; if (i < 0) break;
doc.pages[2].duplicate(LocationOptions.AT_END);
i -= 4;
}
// fill all the text frames with texts from the rows
var frames = doc.textFrames;
var i = 0;
while (rows.length > 0) {
var row = rows.shift().split('\t');
frames[i++].contents = row[0]; // title
frames[i++].contents = row[1]; // text
frames[i++].contents = row[2]; // price
}
// remove empty frames
while (frames.length > i) frames[i].remove();
脚本将 Excel table 中的文本放入模板的文本框架中。如果超过 10 行,脚本将添加页面。
脚本取决于模板中帧的顺序。它填充从第一页上的第一帧[0]到最后一帧的帧。如果您更改顺序,文本将出现在错误的位置。
这是前 10 个 rows/cards 的填充方式:
我有以下代码,非常适合将 excel 导入 InDesign 并创建自动网格目录。
var doc = app.activeDocument;
// get a text from the XLSX file
var inputFile = File("~/Desktop/test.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'));
// width and height of cards, gaps, properties of the grid
var card_width = 70;
var card_height = 80;
var gap = 5;
var cols = 2;
var rows = 3;
var cards_per_page = cols * rows;
// calculate and add pages
var cards = table.length;
var pages_to_add = Math.ceil(cards / cards_per_page) - 1;
while(pages_to_add--) doc.pages.add();
var page_num = 0; // start page
var start_point = [10,10]; // start point for a first card on a page
main_loop:
for (var i = 0; i < cards; i++) {
for (var row = 0; row < rows; row++) {
for (var col = 0; col < cols; col++) {
// break the loop if there is no more rows in the table
if (table.length == 0) break main_loop;
var table_row = table.shift(); // take a first row from the table
var title = table_row[0];
var description = table_row[1];
var price = table_row[2];
// make a card
var card = make_card(title, description, price);
// send the card to the some page and move at some place
card.move(doc.pages[page_num]);
card.move(start_point);
card.move(undefined, [(card_width + gap)*col, (card_height + gap)*row]);
}
}
if (i > (page_num-1) * cards_per_page) page_num++; // increase the page number
}
// 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;
var group = doc.groups.add([title_frame, description_frame, price_frame]);
return group;
}
但是如果我想要 3 页呢?每个页面都会有不同的模板
第 1、4、7 页...将有此模板
这是模板 IDML:https://app.box.com/s/muqvroawbwv8zydwdizfc3xsmyzmenmy
代码如下:
var doc = app.activeDocument;
// get a text from the XLSX file
var inputFile = File("~/Desktop/test.xlsx");
var temp_frame = doc.textFrames.add();
temp_frame.place(inputFile);
var text = temp_frame.parentStory.contents;
temp_frame.remove();
// split the text by rows
var rows = text.split('\r');
rows.shift(); // remove the first row (a header of the table)
// add pages if there is more than 10 rows
var i = rows.length - 10;
while (i > 0) {
doc.pages[0].duplicate(LocationOptions.AT_END);
i -= 1; if (i < 0) break;
doc.pages[1].duplicate(LocationOptions.AT_END);
i -= 5; if (i < 0) break;
doc.pages[2].duplicate(LocationOptions.AT_END);
i -= 4;
}
// fill all the text frames with texts from the rows
var frames = doc.textFrames;
var i = 0;
while (rows.length > 0) {
var row = rows.shift().split('\t');
frames[i++].contents = row[0]; // title
frames[i++].contents = row[1]; // text
frames[i++].contents = row[2]; // price
}
// remove empty frames
while (frames.length > i) frames[i].remove();
脚本将 Excel table 中的文本放入模板的文本框架中。如果超过 10 行,脚本将添加页面。
脚本取决于模板中帧的顺序。它填充从第一页上的第一帧[0]到最后一帧的帧。如果您更改顺序,文本将出现在错误的位置。
这是前 10 个 rows/cards 的填充方式: