我有一个 Google 文档文件(链接:Google 文档),其中包含图像和列表项。其中一张图像是一条垂直线。
当我尝试将此 Google 文档的内容复制到新文档中时,每当处理垂直线元素时,我都会遇到一条错误,提示“不允许操作”。检查该元素后,它似乎是一个 PARAGRAPH 元素,并且不包含任何子元素。
任何解决此问题的见解或解决方案将不胜感激。我用来复制文档的代码如下:
function myFunction() {
var sourceDoc = DocumentApp.getActiveDocument().getBody();
var targetDoc = DocumentApp.openById('1sZm8Z-p4_x2JBmTJZurKvDjWq8hmDi5YmxS68ld6atY');
var totalElements = sourceDoc.getNumChildren();
for (var j = 0; j < totalElements; j++) { //Reversed the loop to make sure paragraphs/lists are still in order when inserted to the target sheet.
var body = targetDoc.getBody()
var element = sourceDoc.getChild(j).copy();
var type = element.getType();
Logger.log(type);
if (type == DocumentApp.ElementType.PARAGRAPH) {
body.insertParagraph(0, element); //Always insert at the top
}
else if (type == DocumentApp.ElementType.LIST_ITEM) {
body.insertListItem(0, element.copy()); //Always insert at the top
}
}
targetDoc.saveAndClose()
}
如有任何帮助,我们将不胜感激
根据 https://developers.google.com/apps-script/guides/docs,图像,更准确地说是 InlineImages,可能是
的子级考虑到上述情况,如果您仍在确定哪些元素包含 InlineImage,除了获取 Body 子元素之外,您的代码还应该获取每个 Document Body 子元素的子元素并检查它们是否是 InlineImage,然后获取 InlineImage blob 并插入目标体对应位置。
注意:类 DriveApp 替换了类 DocList,但可能比替换类名更复杂。
function myFunction() {
var template = DocsList.getFileById(key);// get the template model
var newmodelName='testcopy';// define a new name, do what you need here...
var baseDocId = DocsList.copy(template,newmodelName).getId();// make a copy of firstelement and give it new basedocname build from the serie(to keep margins etc...)
var baseDoc = DocumentApp.openById(baseDocId);// this is the new doc to modify
var body = baseDoc.getActiveSection();
body.appendPageBreak();
var totalElements = body.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var element = body.getChild(j).copy();
var type = element.getType();
if (type == DocumentApp.ElementType.PARAGRAPH) {
if (element.asParagraph().getNumChildren() != 0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_IMAGE) {
var blob = element.asParagraph().getChild(0).asInlineImage().getBlob();
body.appendImage(blob);
}
else body.appendParagraph(element.asParagraph());
}
else if( type == DocumentApp.ElementType.TABLE )
body.appendTable(element);
else if( type == DocumentApp.ElementType.LIST_ITEM )
body.appendListItem(element);
}
}