Google Apps Script复制Google Docs嵌入式绘图失败问题求助
问题:Google Apps Script复制Google Docs嵌入式绘图报错"Action not allowed"
我在通过Google Apps Script将一份Google Docs中的内容复制到另一份文档时,文本、表格等元素都能正常复制,但复制嵌入式绘图时出现问题。
补充背景
代码关联Google表单,会根据表单提交结果把特定页面复制到新文档中。
报错详情
报错信息为Action not allowed,触发位置是代码行body.appendParagraph(drawing)。我拥有所有源文档及代码的所有权,提交时该错误会通过邮件发送给我。我了解嵌入式绘图是段落元素的子元素,尝试过相关修复方案但无效。
原问题代码
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); } } } }
临时解决方案(已验证有效)
将所有嵌入式绘图截图替换为图片后,以下代码可以正常运行:
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); // 调整图片尺寸 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){ // 图片最大宽度限制 newW = 480; newH = parseInt(newW/ratio); } blobbody.setWidth(newW).setHeight(newH); // 调整尺寸 } else { body.appendParagraph(element); } }
该方案虽然繁琐(尤其是文档数量较多时),但确实能解决问题。保留此问题,希望有人能提供无需替换绘图为图片的原生修复方法,同时也希望我的临时方案能帮助到遇到同类问题的开发者。
内容的提问来源于stack exchange,提问作者Actualvarric
相关产品推荐
相关产品推荐

