Inline Drawings not Copying From One Google Doc to Another (Apps Script)

huangapple go评论69阅读模式
英文:

Inline Drawings not Copying From One Google Doc to Another (Apps Script)

问题

以下是您要翻译的内容:

"I am trying to copy inline drawings from one document to another. I have been successful in copying text, tables, etc. but am having issues with inline drawings.

For additional context, the code is linked to a Google Form and copies certain pages to a new document based on the results of the submission.

The error message simply reads 'Action not allowed' at the line 'body.appendParagraph(drawing)'. I own all of the source materials/code. The error message is emailed to me upon submission.

I have seen from other responses that the inline drawing element is a child of the paragraph element and have tried to use the suggested fix but for some reason, it is not working for me.

Relevant code is shown below. Please let me know if you need any additional information and I would be happy to provide it! I have little to no coding background so I am sure there is just a silly mistake somewhere."

"EDIT: FIXED ISSUE WITH WORKAROUND

So, I just took screenshots of every drawing and replaced the drawings in the document with the images instead. Then, this code worked for me:

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();
var styles = {};
styles[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
var blobbody = body.appendImage(blob);
blobbody.getParent().setAttributes(styles);
//resizing the image
var width = blobbody.getWidth();
var newW = width;
var height = blobbody.getHeight();
var newH = height;
var ratio = width/height;
Logger.log('w='+width+' h='+height+' ratio='+ratio);
if(width>480){
//max width of image
newW = 480;
newH = parseInt(newW/ratio);
}
blobbody.setWidth(newW).setHeight(newH); //resizes the image
}
else {
body.appendParagraph(element);
}
}

Kind of a pain to do this, especially with a large number of documents, but it works. Leaving this question up in case anyone can successfully fix this without a workaround later and so that my workaround may help others :)"

希望这有助于您理解所提出的问题和解决方案。

英文:

I am trying to copy inline drawings from one document to another. I have been successful in copying text, tables, etc. but am having issues with inline drawings.

For additional context, the code is linked to a Google Form and copies certain pages to a new document based on the results of the submission.

The error message simply reads "Action not allowed" at the line "body.appendParagraph(drawing)". I own all of the source materials/code. The error message is emailed to me upon submission.

I have seen from other responses that the inline drawing element is a child of the paragraph element and have tried to use the suggested fix but for some reason, it is not working for me.

Relevant code is shown below. Please let me know if you need any additional information and I would be happy to provide it! I have little to no coding background so I am sure there is just a silly mistake somewhere.

if (question_two_answer != 'correct_answer') {
    var totalElements = otherBody2.getNumChildren();
      for( var k = 0; k < totalElements; ++k ) {
        var element = otherBody2.getChild(k).copy();
        var type = element.getType();
        if( type == DocumentApp.ElementType.PARAGRAPH ) {
          if(element.asParagraph().getNumChildren() !=0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_DRAWING) {
            var drawing = element.asParagraph().copy();
            body.appendParagraph(drawing);
          }
          else {
          body.appendParagraph(element);
          }
        }
      }
    }

EDIT: FIXED ISSUE WITH WORKAROUND

So, I just took screenshots of every drawing and replaced the drawings in the document with the images instead. Then, this code worked for me:

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();
        var styles = {};
        styles[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] = DocumentApp.HorizontalAlignment.CENTER;
        var blobbody = body.appendImage(blob);
        blobbody.getParent().setAttributes(styles);
        //resizing the image
       var width = blobbody.getWidth();
       var newW = width;
       var height = blobbody.getHeight();
       var newH = height;
       var ratio = width/height;
       Logger.log('w='+width+'h='+height+' ratio='+ratio);
       if(width>480){ 
          //max width of image
          newW = 480; 
          newH = parseInt(newW/ratio);
       }
       blobbody.setWidth(newW).setHeight(newH); //resizes the image
      }
      else {
      body.appendParagraph(element);
      }
    }

Kind of a pain to do this, especially with a large number of documents, but it works. Leaving this question up in case anyone can successfully fix this without a workaround later and so that my workaround may help others Inline Drawings not Copying From One Google Doc to Another (Apps Script)

答案1

得分: 1

你正尝试从一个文档复制内联图纸到另一个文档,但在 body.appendParagraph(drawing) 这一行收到错误信息“不允许的操作”。

你的代码基于Google Apps Scripts - 复制一个文档中的内联图纸到另一个文档,并参考了从一个文档附加图纸到另一个文档

错误信息是由于 V8 运行时中的一个错误。引用 @Tanaike 的话说:“[...] 复制包含内联图纸的段落时,请禁用 V8 运行时。当使用带有 V8 运行时的上述修改脚本时,会发生类似“Exception: Action not allowed”的错误。”

以下代码(基于 OP 代码)运行完美。


运行代码

function copySource2Target() {
  var sourceDoc = DocumentApp.getActiveDocument()
  var sourceBody = sourceDoc.getBody()

  var targetDoc = DocumentApp.openById('<<insert doc id>>');
  var targetBody = targetDoc.getBody()


  //if (question_two_answer != 'correct_answer') {
    var totalElements = sourceBody.getNumChildren();
      for( var k = 0; k < totalElements; ++k ) {
        var element = sourceBody.getChild(k).copy();
        var type = element.getType();
        if( type == DocumentApp.ElementType.PARAGRAPH ) {
          if(element.asParagraph().getNumChildren() !=0 && element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_DRAWING) {
            // Logger.log("DEBUG: k:"+k+", Type:"+type+", child type:"+ element.asParagraph().getChild(0).getType())
            var drawing = element.asParagraph().copy()
            targetBody.appendParagraph(drawing)
            // Logger.log("DEBUG: k:"+k+" - appended drawing")
          }
          else {
              // Logger.log("DEBUG: k:"+k+", Type:"+type)
              targetBody.appendParagraph(element.asParagraph().copy())
              // Logger.log("DEBUG: k:"+k+" - appended element")
          }
        }
      }
  //}
}

项目设置

[![settings][1]][1]


[![source][2]][2]


目标 - 在

[![target_before][3]][3]


目标 - 之后

[![target_after][4]][4]


<details>
<summary>英文:</summary>

You are trying to copy inline drawings from one document to another but you get an error message &quot;Action not allowed&quot; at the line `body.appendParagraph(drawing)`. 

Your code is based on [Google Apps Scripts - Copy Inline Drawing from One Document to Another](https://stackoverflow.com/a/70358126/1330560) and informed by [Append drawing from one doc to another](https://stackoverflow.com/a/75325275/1330560).

The error message is because of a bug in V8 runtime. To quote @Tanaike, &quot;[...] when the paragraph including the inline drawings is copied, please disable V8 runtime. When the above-modified script is used with V8 runtime, an error like Exception: Action not allowed occurs.&quot;

The following code (based on the OP code) works perfectly.

----------

**Working Code**

    function copySource2Target() {
      var sourceDoc = DocumentApp.getActiveDocument()
      var sourceBody = sourceDoc.getBody()
    
      var targetDoc = DocumentApp.openById(&#39;&lt;&lt;insert doc id&gt;&gt;&#39;);
      var targetBody = targetDoc.getBody()
    
    
      //if (question_two_answer != &#39;correct_answer&#39;) {
        var totalElements = sourceBody.getNumChildren();
          for( var k = 0; k &lt; totalElements; ++k ) {
            var element = sourceBody.getChild(k).copy();
            var type = element.getType();
            if( type == DocumentApp.ElementType.PARAGRAPH ) {
              if(element.asParagraph().getNumChildren() !=0 &amp;&amp; element.asParagraph().getChild(0).getType() == DocumentApp.ElementType.INLINE_DRAWING) {
                // Logger.log(&quot;DEBUG: k:&quot;+k+&quot;, Type:&quot;+type+&quot;, child type:&quot;+ element.asParagraph().getChild(0).getType())
                var drawing = element.asParagraph().copy()
                targetBody.appendParagraph(drawing)
                // Logger.log(&quot;DEBUG: k:&quot;+k+&quot; - appended drawing&quot;)
              }
              else {
                  // Logger.log(&quot;DEBUG: k:&quot;+k+&quot;, Type:&quot;+type)
                  targetBody.appendParagraph(element.asParagraph().copy())
                  // Logger.log(&quot;DEBUG: k:&quot;+k+&quot; - appended element&quot;)
              }
            }
          }
      //}
    }


----------
**Project settings**

[![settings][1]][1]


----------
**Source**

[![source][2]][2]


----------
**Target - BEFORE**

[![target_before][3]][3]

----------

**Target - AFTER**

[![target_after][4]][4]


  [1]: https://i.stack.imgur.com/moHJn.jpg
  [2]: https://i.stack.imgur.com/p7P9g.jpg
  [3]: https://i.stack.imgur.com/pK9ci.jpg
  [4]: https://i.stack.imgur.com/Jzei7.jpg

</details>



huangapple
  • 本文由 发表于 2023年2月19日 10:15:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定