Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 无法获取DocumentBodySection.appendImage(InlineImage)以正常工作?_Google Apps Script_Google Docs - Fatal编程技术网

Google apps script 无法获取DocumentBodySection.appendImage(InlineImage)以正常工作?

Google apps script 无法获取DocumentBodySection.appendImage(InlineImage)以正常工作?,google-apps-script,google-docs,Google Apps Script,Google Docs,我基本上打开一个文档并获取它的DocumentBodySection,然后迭代元素,复制它们(Element.copy())以获得一个深度分离的副本,然后将元素附加到另一个文档中 这适用于段落和其他元素类型,但当涉及到内联图像时,结果文档中会出现断开的链接 有人用这个吗 这里是一个片段 function appendSection(doc, section) { for (i=0; i<section.getNumChildren(); i++) { var el = sect

我基本上打开一个文档并获取它的DocumentBodySection,然后迭代元素,复制它们(Element.copy())以获得一个深度分离的副本,然后将元素附加到另一个文档中

这适用于段落和其他元素类型,但当涉及到内联图像时,结果文档中会出现断开的链接

有人用这个吗

这里是一个片段

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
}
功能附录部分(文档,部分){

对于(i=0;i我得到了appendImage在不同但相似的情况下为我工作

希望它也能对你有帮助,请让我知道

根据您的代码进行调整,只需添加/更改Blob行,以避免重复的内联图像

  if (el_child.getType() == DocumentApp.ElementType.INLINE_IMAGE)
  {
    var blob = el_child.asInlineImage().getBlob();
    doc.appendImage(blob);
  } 
编辑1:如下所述,当在bodySection中循环时,任何内联图像都将嵌入到段落中。下面的代码将适用于任何没有用文本包装的内联图像。如果是这种情况,您需要深入挖掘

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());
        }
for(var elem=0;elem
我让appendImage为我工作的情况不一样,但很相似

希望它也能对你有帮助,请让我知道

根据您的代码进行调整,只需添加/更改Blob行,以避免重复的内联图像

  if (el_child.getType() == DocumentApp.ElementType.INLINE_IMAGE)
  {
    var blob = el_child.asInlineImage().getBlob();
    doc.appendImage(blob);
  } 
编辑1:如下所述,当在bodySection中循环时,任何内联图像都将嵌入到段落中。下面的代码将适用于任何没有用文本包装的内联图像。如果是这种情况,您需要深入挖掘

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());
        }
for(var elem=0;elem
我已经尝试了我能想象到的所有可能的方法,我也总是得到同样的断开链接…我找到的唯一解决办法是使用DocsList复制整个文档。复制并删除我必须从副本中删除的内容。谢谢Serge,我也尝试了我能想到的所有方法,我计划在下一个办公时间提出这个问题(我也没有发现脚本问题)然后,如果他们不知道该做什么,就创建一个问题…是的,我将使用DocList.copy…InlineDrawings的组合,这些组合工作良好,并用图像块替换模板键。感谢尝试。请在问题跟踪器中提出此问题。好的,谢谢Jan,将在当天晚些时候完成…现在是上午5点;)如果我可以问:你是如何添加内联图的?我已经尝试了我能想象到的所有可能的方法,我也总是得到同样的断开链接…我找到的唯一解决方法是使用DocsList.copy复制整个文档,并从副本中删除我必须的内容。谢谢Serge,我也尝试了我能想到的所有方法,我计划在下一个办公时间提出这个问题(我也没有发现脚本问题)然后,如果他们不知道该做什么,就创建一个问题…是的,我将使用DocList.copy…InlineDrawings的组合,这些组合工作良好,并用图像块替换模板键。感谢尝试。请在问题跟踪器中提出此问题。好的,谢谢Jan,将在当天晚些时候完成…现在是上午5点;)如果我可以问:您是如何添加内联图形的?嗨,Fausto,您能看一看并告诉我为什么内联图像没有显示吗?我使用了您的代码片段,但运气不好;-)我得到了一个*取而代之的是…我将编辑我的答案并提供更多详细信息,但是代码中的问题(问题14252185)因为内联图片总是嵌入到新的段落中,所以你必须挖掘一步(对于没有周围文本的图片)才能得到实际的图片。谢谢,我会处理它并让你知道我发现了什么。完全有效!非常感谢…太糟糕了,我不能“投票”两次^^^嗨Fausto,你能看一看并告诉我为什么内联图像没有出现吗?我使用了你的代码片段,但没有运气;-)我得到了一个*取而代之的是…我将编辑我的答案并提供更多细节,但是代码中的问题(问题14252185)是内联图像总是嵌入到新段落中,所以你必须挖掘一步(对于没有周围文字的图像)以获得实际图像。谢谢,我会玩它,并让你知道我发现了什么。完全工作!非常感谢…太糟糕了,我不能“投票”两次^^