我正在尝试使用 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
有没有办法使用附加功能或其他方式在添加内容后将标题分配到正确的位置来实现这一点?也许内容中的特殊字符来定义样式?