表格单元格中的内联图像不会复制到新文档中

问题描述 投票:0回答:0

我有一个程序,可以在提交表格后将各种文档的内容复制到新创建的文档中。我在复制嵌入到表格中的内联图像时遇到问题。

提交表单时,我收到一封电子邮件提醒,在“var dstImg = dstParagraph.insertInlineImage(l, srcImg.getBlob());”这一行显示“服务不可用:文档”在下面的代码中。

我看到的所有 Stack Overflow 问题似乎都源于用户没有使用内联图像,而是使用固定图像这一事实。我已经仔细检查过我的图像实际上是内联的。我也启用了 Google Docs 和 Drive API。

这是我正在使用的文档的链接:

https://docs.google.com/document/d/1WFEvhnW2FDtfBrDPiPXH1U09DD8mg6PA_Q6afciSLgU/edit?usp=sharing

这是我收到错误消息时生成的文档的截图链接:

https://drive.google.com/file/d/1x36HFEAxiPWFOjLxLqkgDQYDnr0Fftem/view?usp=sharing


我意识到这不是一个新问题,我已经查看了以下资源以寻求帮助:

Google Apps 脚本 - appendParagraph with image

当嵌套在表格中并附加到新文档时,图像会消失。错误还是我错过了什么?

如何使用谷歌脚本(以编程方式)将图像从一个表格单元格复制到另一个表格单元格?

带有内联图像的段落破坏应用程序脚本将工作表数据合并到文档中

这里是相关代码:


function CreateDocumentInFolder(e) {

 var doc = DocumentApp.openById(main_file_ID)
 var body = DocumentApp.openById(main_file_ID).getBody();
 var question_twelve_answer = itemResponses[12].getResponse();
 var otherBody12 = DocumentApp.openById('1WFEvhnW2FDtfBrDPiPXH1U09DD8mg6PA_Q6afciSLgU').getBody();

 if (question_twelve_answer != 'm<12') {
    var totalElements = otherBody12.getNumChildren();
      for( var j = 0; j < totalElements; ++j ) {
        console.log(j);
        var element = otherBody12.getChild(j).copy();
        var type = element.getType();
        if( type == DocumentApp.ElementType.PARAGRAPH ) {
          body.appendParagraph(element);
        }
        else if( type == DocumentApp.ElementType.TABLE ) {
          var dstTable = body.appendTable(element);
          var srcTable = element.asTable();
          copyTable(srcTable, dstTable);
        }
        else if( type == DocumentApp.ElementType.LIST_ITEM )
          body.appendListItem(element);
        else
          throw new Error("Unknown element type: "+type);
    }
     body.appendPageBreak()
  }

function copyTable(srcTable, dstTable) {
  var row = srcTable.getNumRows();
  for (var i = 0; i < row; i++) {
    var col = srcTable.getRow(i).getNumCells();
    for (var j = 0; j < col; j++) {
      var cell = srcTable.getCell(i, j);
      var c1 = cell.getNumChildren();
      for (var k = 0; k < c1; k++) {
        var ty = cell.getChild(k).getType();
        if (ty === DocumentApp.ElementType.TABLE) {
          srcTable = cell.getChild(k).asTable();
          dstTable = dstTable.getCell(i, j).getChild(k).asTable();
          return copyTable(srcTable, dstTable);
        } else {
          var paragraph = cell.getChild(k).asParagraph();
          var c2 = paragraph.getNumChildren();
          for (var l = 0; l < c2; l++) {
            var child = paragraph.getChild(l);
            var t = child.getType();
            if (t === DocumentApp.ElementType.INLINE_IMAGE) {
              var srcImg = child.asInlineImage();
              var dstParagraph = dstTable.getCell(i, j).getChild(k).asParagraph();
              dstParagraph.getChild(l).asInlineImage().removeFromParent();
              // Issue is the line below!
              var dstImg = dstParagraph.insertInlineImage(l, srcImg.getBlob());
              dstImg.setWidth(srcImg.getWidth());
              dstImg.setHeight(srcImg.getHeight());
            }
          }
        }
      }
    }
  }
}


google-apps-script google-docs google-docs-api
© www.soinside.com 2019 - 2024. All rights reserved.