无法让DocumentBodySection.appendImage(InlineImage)正常工作? [英] Unable to get DocumentBodySection.appendImage(InlineImage) to function properly?
问题描述
https://developers.google.com/apps-script/class_documentbodysection#appendImage 我基本上打开一个文档并获取它的DocumentBodySection,然后遍历这些元素,复制它们(Element.copy())以获得深度分离的副本然后将该元素附加到另一个文档中。 这适用于段落和其他元素类型,但当涉及内联图像时,我会在结果中看到一个断开的链接文档。 任何人都可以使用它? 这是一段代码 希望它也能帮助你,请让我知道 从代码中调整,只需添加/更改Blob行避免重复的内联图像 https://developers.google.com/apps-script/class_documentbodysection#appendImage I basically open a document and get its DocumentBodySection, I then iterate through the elements, copy them (Element.copy()) to get a deep detached copy and then append the element to another document. This works well for paragraph and other element types, but when it comes to inline images, I get a broken link in the resulting document. Anyone got that to work? Here is a snippet Thanks in advance. I got appendImage to work for me in not the same, but similar case. Hopefully it will do for you as well, please let me know Adapting from your code, just add / change the Blob lines to avoid repeated inline images EDIT 1: as commented below, when looping through the bodySection, any INLINE_IMAGE will be embedded in a PARAGRAPH. The code below will work for any INLINE_IMAGE that is not wrapped with text. If that was the case, you need to dig deeper.
这篇关于无法让DocumentBodySection.appendImage(InlineImage)正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
函数appendSection(doc,section){
for(i = 0; i< section.getNumChildren(); i ++){
var el = section.getChild(i);
if(el.getType()== DocumentApp.ElementType.PARAGRAPH)
{
if(el.getNumChildren()!= 0){
var el_child = el.getChild (0);
if(el_child.getType()== DocumentApp.ElementType.INLINE_IMAGE)
{
doc.appendImage(el_child.asInlineImage()。copy());
Logger.log(Image);
}
}
doc.appendParagraph(el.copy());
Logger.log(Paragraph);
}
}
return doc
}
$ b
$ p $ if(el_child.getType()== DocumentApp.ElementType.INLINE_IMAGE)
{
var blob = el_child.asInlineImage()。getBlob();
doc.appendImage(blob);
编辑1:如下所述,当循环bodySection时,任何INLINE_IMAGE都会嵌入到段落中。下面的代码适用于任何未包含文本的INLINE_IMAGE。如果是这样,您需要深入挖掘。
for(var elem = 0; elem< bodySection.getNumChildren( ); elem ++){
var theElem = bodySection.getChild(elem).copy();
if(theElem.getType()== DocumentApp.ElementType.PARAGRAPH){
if(theElem.asParagraph()。getNumChildren()!= 0&& theElem.asParagraph()。getChild() 0).getType()== DocumentApp.ElementType.INLINE_IMAGE){
var blob = theElem.asParagraph()。getChild(0).asInlineImage()。getBlob();
targetDoc.appendImage(blob);
}
else targetDoc.appendParagraph(theElem.asParagraph()); (theElem.getType()== DocumentApp.ElementType.LIST_ITEM)targetDoc.appendListItem(theElem.asListItem()。copy());
if
if(theElem.getType()== DocumentApp.ElementType.TABLE)targetDoc.appendTable(theElem.asTable());
}
function appendSection(doc, section) {
for (i=0; i<section.getNumChildren(); i++) {
var el = section.getChild(i);
if (el.getType() == DocumentApp.ElementType.PARAGRAPH)
{
if (el.getNumChildren() != 0) {
var el_child = el.getChild(0);
if (el_child.getType() == DocumentApp.ElementType.INLINE_IMAGE)
{
doc.appendImage(el_child.asInlineImage().copy());
Logger.log("Image");
}
}
doc.appendParagraph(el.copy());
Logger.log("Paragraph");
}
}
return doc
}
if (el_child.getType() == DocumentApp.ElementType.INLINE_IMAGE)
{
var blob = el_child.asInlineImage().getBlob();
doc.appendImage(blob);
}
for (var elem = 0; elem < bodySection.getNumChildren(); elem++) {
var theElem = bodySection.getChild(elem).copy();
if (theElem.getType() == DocumentApp.ElementType.PARAGRAPH) {
if (theElem.asParagraph().getNumChildren() != 0 && theElem.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_IMAGE) {
var blob = theElem.asParagraph().getChild(0).asInlineImage().getBlob();
targetDoc.appendImage(blob);
}
else targetDoc.appendParagraph(theElem.asParagraph());
}
if (theElem.getType() == DocumentApp.ElementType.LIST_ITEM) targetDoc.appendListItem(theElem.asListItem().copy());
if (theElem.getType() == DocumentApp.ElementType.TABLE) targetDoc.appendTable(theElem.asTable());
}