【问题标题】:Google Apps Scripts - Copy Inline Drawing from One Document to AnotherGoogle Apps 脚本 - 将内联绘图从一个文档复制到另一个文档
【发布时间】:2021-12-15 02:55:02
【问题描述】:

我正在尝试将整个文档从一个复制到另一个。

我只是想将 INLINE_DRAWING 从一个谷歌文档复制到另一个,但我不断收到此错误。如果我删除了附加图纸的尝试,那么就没有错误,但是在我完成的文档中没有图纸。如何正确地将内联图从一个文档复制到另一个文档?

// fromFile & toFile are objects after calling .getBody()
function copyFileContentsFromTo(fromFile,toFile){

    for(var i=0; i<fromFile.getNumChildren();i++){ //run through the elements of the template doc's Body.
      var child = fromFile.getChild(i);
      switch (fromFile.getChild(i).getType()) { //Deal with the various types of Elements we will encounter and append.
        case DocumentApp.ElementType.PARAGRAPH:
          // Look for child elements within the paragraph. Inline Drawings are children.
          if(child.asParagraph().getNumChildren() !=0 && child.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_DRAWING) {
            console.log("Found an Inline Drawing");
            var drawing = child.asParagraph();
            toFile.append(drawing.copy()); // This causes the error!
          }else{
            toFile.appendParagraph(child.copy());
          }
          break;
        case DocumentApp.ElementType.TABLE:
          toFile.appendTable(child.copy());
          break;
        default:
          console.log("Whoops. Did not handle this Element Type when Copy Data from Template to Main: " + fromFile.getChild(i).getType());
          break;
      }
    }
}

我已经到处搜索了,找不到任何资源,这是最接近的,但在我的屏幕截图中仍然产生错误:Google script InlineDrawing class

【问题讨论】:

    标签: google-apps-script google-docs


    【解决方案1】:

    在你的脚本中,需要修改如下。

    发件人:

    toFile.append(drawing.copy());
    

    收件人:

    toFile.appendParagraph(drawing.copy());
    

    重要提示:

    • 在当前阶段,当复制包含内嵌图的段落时,请禁用V8运行时。当上面修改的脚本与 V8 运行时一起使用时,会出现类似Exception: Action not allowed 的错误。所以请注意这一点。这已在 Google 问题跟踪器中报告。 Ref 我相信这个错误可以在未来的更新中删除。

    参考:

    【讨论】:

    • 哦,天哪...我不知道 V8 运行时会导致该问题。如果我禁用这是否意味着我需要将我的所有代码转换为不使用 javascript 类和其他语法?还有其他解决方法吗?
    • @Sliffcak 感谢您的回复。关于您对If I disable does that mean I need to convert all my code away from using javascript Classes and other syntax? 的附加问题,是的。我对此深表歉意。如果您只想运行函数copyFileContentsFromTo 而没有V8 运行时,作为一种变通方法,通过Web 应用程序执行该函数怎么样?这样,您可以通过分离函数 copyFileContentsFromTo 来将当前脚本与 V8 运行时一起使用。
    • 好的,我明白了。现在我将只转换我的代码库的语法。我不知道带有内联图的 V8 错误。标记为已解决。再次感谢您!
    • @Sliffcak 感谢您的回复。我希望这个错误将在未来的更新中得到解决。所以在现阶段,作为一种简单的方式,我认为不使用V8运行时的脚本可能比使用Web Apps的解决方法更简单。
    猜你喜欢
    • 2023-02-02
    • 1970-01-01
    • 2013-02-13
    • 2014-09-22
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多