【发布时间】:2014-07-22 01:18:31
【问题描述】:
我正在尝试使用 inDesign JSX 脚本将以下数据插入到文档中:
data = [{heading:"Heading 1", content: ["Some content"]},
{heading:"Heading 2", content: ["Some other content with", "Multiple paragraphs"]}]
数据必须放在单个 TextFrame 中,但标题和内容的样式不同。
我能看到添加文本的唯一方法是一次性通过textFrame.contents 变量:
allContent = "";
headingParagraphs = []; // keep track of which paragraphs are headings
paragraph = 0;
for (var i = 0; i < data.length; i++) {
allContent += data.heading + "\r"; // Use a newline to split the paragraph
headingParagraphs.push(paragraph);
paragraph++;
for (var j = 0; j < data.content.length; j++) {
allContent += data.content[j] + "\r"; // Use a newline to split the paragraph
paragraph++;
}
}
textFrame.contents = allContent; // all data is in, but all text is styled the same
然后,一旦数据进入,我会迭代段落并为标题添加一些样式:
for (var i = 0; i < textFrame.paragraphs.count(); i++) {
if (headingParagraphs.indexOf(i) != -1) { // this is a heading paragraph
textFrame.paragraphs[i].pointSize = 20;
}
}
这适用于适合一页的小型数据集,但是一旦内容大于框架,paragraphs 只会返回可见段落。如果我继续使用新的 textFrame,段落将被拆分,headingParagraphs[] 数组不再排列。
理想情况下,我想在追加下一个内容之前追加到内容并设置样式 - 但 API 文档不太清楚你可以如何做到这一点(如果有的话)
// Pseudo code:
for all sections:
append the heading to the frame, split to next page if needed
style all the *new* paragraphs as headings
for all section contents
append the content to the frame, split to next page if needed
style any *new* paragraphs as normal content
有没有办法在添加内容后使用附加功能或其他方式将标题分配到正确的位置?也许内容中的特殊字符来定义样式?
【问题讨论】:
标签: adobe-indesign jsx indesign-server