javascript - 设计 : Copying cell style to new document

标签 javascript cell adobe-indesign extendscript

我想编写一个脚本(在 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

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

设置属性“basedOn”的值无效。需要 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):单元格样式(以及其他所有内容)不是由 name 或样式数组中的 index 标识,而是由文档范围内的唯一 < em>identifier -- id 元素,可以在 InDesign 的所有 native 基础对象中找到。这保证在整个文档中是一个唯一编号;它是 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 对象的属性也无法复制(basedOn、颜色、线条样式..)。但它对剩下的部分很有效——其中一个是线条粗细。


这是什么意思?

The functionality should be much like the existing 'Load cell styles' option in InDesign, except it should be able to do this on a batch folder instead of one document at a time.

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

参见 #importStyles参数的完整解释。

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

关于javascript - 设计 : Copying cell style to new document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19894845/

相关文章:

javascript - vuetify v-menu 组件中的 Activator slot 重新定义

iOS 网页错误通过蜂窝数据而不是通过 Wifi?最近更改了 AT&T 蜂窝网络?

python - 在 python 中使用 DictWriter/Reader 和 csv 模块

scripting - InDesign 将图层复制到另一个文档

javascript - 如何使用 JavaScript 将图像缩放到全屏?

javascript - 使用 Javascript 从 Ajax TabContainer 获取 ActiveTabIndex

javascript - 如何在 html 中将错误消息居中?

excel - 删除 Excel 单元格内的重复项

javascript - 使用 indesign javascript 查找下一个和上一个

Adobe InDesign CS5 脚本 - 对象不支持属性或方法 'resolution'