Indesign:将单元格样式复制到新文档

Indesign: Copying cell style to new document

本文关键字:新文档 文档 复制 单元格 样式 Indesign      更新时间:2023-09-26

我想编写一个脚本(以Javascript/Extendscript为单位),将单元格样式从源文档复制到一个或多个目标文档。该功能应该很像InDesign中现有的"称重传感器样式"选项,除了它应该能够在批处理文件夹而不是一次一个文档上执行此操作。

问题:

1)

下面的代码工作

cStyle_source = app.documents[0].cellStyles[1]
cStyle_target = app.documents[0].cellStyles[2]
value = cStyle_source.basedOn
cStyle_target.basedOn = value

它会将源单元格样式中"基于"下选择的单元格样式正确复制到目标样式。

我现在尝试将 basedOn 属性复制到另一个打开文档中的目标样式:

cStyle_source = app.documents[0].cellStyles[1]
cStyle_target = app.documents[1].cellStyles[2]
value = cStyle_source.basedOn
cStyle_target.basedOn = value

新文档是第一个文档的副本,它包含所有相同的样式。但是,运行此代码会给我错误消息:

设置属性"基于"的值无效。预期的 CellStyle 或字符串,但收到了 CellStyle。

是我,还是这个信息没有意义?当将属性值应用于同一文档中的样式而不应用于另一个文档时,为什么会这样做?

2)

为什么下面的示例代码适用于段落样式和字符样式,而不适用于对象样式和单元格样式?

myProperties = app.activeDocument.paragraphStyles[1].properties
app.activeDocument.paragraphStyles[2].properties = myProperties

这非常方便地将所有属性从样式 1 复制到样式 2,甚至可以跨文档工作。简单易行。

为什么单元格样式(和对象样式)在这方面不同?

似乎我需要遍历每个属性才能复制完整的单元格样式,因此出现了我在上面 1) 中的问题。


更新:

我想将我的解决方案添加到复制整个单元格样式中,包括"其他 ID"对象,如 basedOnappliedParagraphStyle 和色板。

function AddCStyles() {
    var cStyles_source = app.documents[1].cellStyles;
    var cStyles_target = app.activeDocument.cellStyles;
    var loopLength = cStyles_target.length
    for (var i in selectedCStyles) {
        var myProperties = cStyles_source.item(selectedCStyles[i]).properties;
        var incomingName = selectedCStyles[i];
        for (var j=0; j < loopLength; j++) {
            if (incomingName === cStyles_target[j].name) { // Checks if cell style already exists. If yes, loop through properties and overwrite
                for (var k in myProperties) {
                    try {
                    if (myProperties[k] == "[object CellStyle]") {
                        value = cStyles_source.item(selectedCStyles[i]).properties[k].name;
                        cStyles_target[j][k] =  app.documents[0].cellStyles.item(value);
                        continue
                        }
                    if (myProperties[k] == "[object ParagraphStyle]") {
                        value = cStyles_source.item(selectedCStyles[i]).properties[k].name;
                        cStyles_target[j][k] =  app.documents[0].paragraphStyles.item(value);
                        continue
                        }
                    if (myProperties[k] == "[object StrokeStyle]" || myProperties[k] == "[object Color]") {
                        value = cStyles_source.item(selectedCStyles[i]).properties[k].name;
                        cStyles_target[j][k] = value;
                        continue
                        }
                    cStyles_target[j][k] = myProperties[k];
                    }
                    catch (e) {}
                    }
                }
            if (j === loopLength - 1 && cStyles_target.item(incomingName).isValid === false) {  // If cell style doesn't exist, create new and loop through properties
                var newCStyle = cStyles_target.add();               
                for (var k in myProperties) {
                    try {
                    if (myProperties[k] == "[object CellStyle]") {
                        value = cStyles_source.item(selectedCStyles[i]).properties[k].name;
                        newCStyle[k] =  app.documents[0].cellStyles.item(value);
                        continue
                        }
                    if (myProperties[k] == "[object ParagraphStyle]") {
                        value = cStyles_source.item(selectedCStyles[i]).properties[k].name;
                        newCStyle[k] =  app.documents[0].paragraphStyles.item(value);
                        continue
                        }
                    if (myProperties[k] == "[object StrokeStyle]" || myProperties[k] == "[object Color]") {
                        value = cStyles_source.item(selectedCStyles[i]).properties[k].name;
                        newCStyle[k] = value;
                        continue
                        }
                    newCStyle[k] = myProperties[k];
                        }
                    catch (e) {}
                    }
                }
            }                
        }
    }
On (

1):单元格样式(以及其他所有样式)不是通过样式数组中的名称或索引来标识,而是由文档范围的唯一标识符(id元素)标识,该标识符可在 InDesign 的所有本机基本对象中找到。这保证是整个文档中的唯一编号;它是 ID 本身的构建块之一。

尽管 basedOn 属性似乎指向单元格样式,但实际上它包含一个数字:basedOn单元格样式的 ID 号。

尽管两个文档中单元格样式的名称可以相同,但它们各自的 id 却并非如此。 id 是在构造新对象时在第一个服务的基础上创建的,但即使您以相同的方式同时构造两个文档,我也不会指望拥有相同的 id。

不过,您确实知道basedOn单元格样式的名称;如果另一个文档中存在同名的单元格样式,则可以使用以下方法解决它:

value = cStyle_source.basedOn.name;
cStyle_target.basedOn = app.documents[1].cellStyles.item(value);

关于(2):奇怪的是,显而易见的方法只复制单元格样式name,并且还需要一些复制才能获得其他元素:

alert (app.activeDocument.cellStyles[2].name);
myProperties = app.activeDocument.cellStyles[2].properties;
cs = app.documents[1].cellStyles.add();
for (i in myProperties)
try {
    cs[i] = myProperties[i];
} catch (e)
{
    // ..
}

它需要一个try..catch,因为并非所有属性都是可读/写的;它会在只读属性(如id)上失败。此外,引用其他 ID 对象的所有属性也不能复制(基于、颜色、线条样式等)。但它对剩下的东西很有效 - 线条粗细,一方面。


这是什么意思?

该功能应该与 InDesign 中现有的"称重传感器样式"选项非常相似,只是它应该能够在批处理文件夹上执行此操作,而不是一次一个文档。

您可以循环访问文件夹中的文件,并为每个文件执行app.activeDocument.import (ImportFormat.CELL_STYLES_FORMAT, from, globalStrategy)

有关参数的完整说明,请参阅 #importStyles。

唯一的问题是,似乎没有办法导入某些样式 - 您必须使用目标文件中所需的所有且仅单元格样式设置源文件。