我不会使用OfficeJS更改Shape的可见性。由于我认为OfficeJS中没有形状,因此我需要使用OOXML。因此,我尝试更改v:shape标记的样式属性,然后通过body.insertOOXML(value,'Replace')插入OOXML。在Word Online中,我得到一个例外,即Word Online不允许这样做,并且Word 2016会崩溃。
我的代码:
var bodyOOXML = context.document.body.getOoxml();
// Synchronisierung des Dokumentes
return context.sync().then(function () {
if(bodyOOXML.value != null) {
var xml = $.parseXML(bodyOOXML.value);
_shapes.forEach(function(shape) {
if(shape.attr("id").startsWith(tag) && shape.attr("id") == tag + "_" + index) {
shape.attr("style", shape.attr("style").replace("visibility:hidden", "visibility:visible"));
var rect = $(xml).find("v\\:rect[id=" + shape.attr("id") + "]");
rect.attr("style", rect.attr("style").replace("visibility:hidden", "visibility:visible"));
}
else if(shape.attr("id").startsWith(tag)) {
shape.attr("style", shape.attr("style").replace("visibility:visible", "visibility:hidden"));
var rect = $(xml).find("v\\:rect[id=" + shape.attr("id") + "]");
rect.attr("style", rect.attr("style").replace("visibility:visible", "visibility:hidden"));
}
});
context.document.body.insertOoxml(_serializer.serializeToString(xml), 'Replace');
return context.sync();
}
});
_serializer是XMLSerializer,而_shapes是原始形状标签的集合,这些形状标签在Add In的开头进行了初始化。