如何使用Google脚本(以编程方式)将图像从一个表单元格复制到Google文档中的另一个表单元格中? [英] How to copy image from one table cell to another within google documents using a google script (programmatically)?

查看:53
本文介绍了如何使用Google脚本(以编程方式)将图像从一个表单元格复制到Google文档中的另一个表单元格中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个Google文档的内容复制到另一个.内容包括文本,表格和图像.

I would like to copy the content of one google doc to another. The content includes text, tables and images.

我的代码复制文本和表格.但是,表单元格中包含的所有内容都不会被复制.

My code copies the text and the tables. However, whatever is contained in table cells is not copied.

我在以下位置提供了代码和单个文档的简化版本:

I made a simplified version of the code and single document accessible here: https://docs.google.com/document/d/1hcQzBuMA6E15u8VtW2lWGL7XCcU3qVsDhn-5jiznQP4/edit?usp=sharing.

该代码只是复制粘贴Google文档的内容,该文档包括一个包含表/图像的表.发生相同的问题.单元格的内容不会被复制.查看屏幕截图

The code simply copy-pastes the content of the google document which includes a table containing a table/images. The same problem occurs. The content of the cell is not copied. see screenshot

这是代码的简化版:

function test() {
  
  // Make Copy of template file
  doc = DocumentApp.getActiveDocument();
  body =doc.getBody();
  

  
  /// Copy elements from source file to target file one bich one
  var totalElements = body.getNumChildren();
  var types = new Array;
  
  for( var iel = 0; iel < totalElements; iel++ ) {
     current_body = doc.getBody();
    var element = body.getChild(iel).copy();
    var type = element.getType();
    types.push(type);
    
    switch(type){
      case DocumentApp.ElementType.PARAGRAPH:
        body.appendParagraph(element);
        break;
      case DocumentApp.ElementType.TABLE:
        var newTable =body.insertTable( body.getNumChildren()-1, element );
        CopyTableCellByCell( element, newTable );
        break;
      case DocumentApp.ElementType.LIST_ITEM:
        body.appendListItem(element);
        break;
      case DocumentApp.ElementType.INLINE_IMAGE:
        body.appendImage(element); 
        break;
        
    }
  }
  doc.saveAndClose();
}

// recursive function that replaces each cell by first clearing it and Copying the content from the original table
function CopyTableCellByCell( srcTable, dstTable ) {
  var numRows = srcTable.getNumRows();
  var srcRow, numCells, dstCell, icell;
  var types = new Array;
  
  for ( var irow = 0; irow < numRows; irow++ ) {// EACH ROW
    srcRow = srcTable.getRow( irow );
    dstRow = dstTable.getRow( irow );
    numCells = srcRow.getNumCells();
    
    for ( icell = 0; icell < numCells; icell++ ) { // EACH CELL
      dstCell = dstRow.getCell( icell );
      dstCell.clear();
      var srcCell = srcTable.getCell( irow, icell );
      var numCellChildren = srcCell.getNumChildren(); // ==> outputs 1 paragraph child instead of a paragraph and a table.
      
      
      for ( var ich = 0; ich < numCellChildren; ich++ ) { // EACH CHILD
        var cellChild = srcCell.getChild( ich );
        var childCopy = cellChild.copy();
        var type = childCopy.getType();
        types.push(type);


        switch( type ){
            
          case DocumentApp.ElementType.PARAGRAPH:
            dstCell.insertParagraph( ich, childCopy );
            break;
            
          case DocumentApp.ElementType.LIST_ITEM:
            var atts    = childCopy.getAttributes();
            var newListItem = dstCell.insertListItem( ich, childCopy );
            newListItem.setAttributes( atts );
            break;
            
          case DocumentApp.ElementType.TABLE:

            var newTable = dstCell.insertTable( ich, childCopy );
            CopyTableCellByCell( cellChild, newTable );
            break;
            
          case DocumentApp.ElementType.INLINE_IMAGE:
            var parpar = childCopy.getParent();
            var ttt = parpar.getType();
            destImg = parpar.insertInlineImage(l, childCopy.getBlob()); 
            dstImg.setWidth(childCopy.getWidth());
            dstImg.setHeight(childCopy.getHeight());
            break;

        }
        
      }

    }
  }
}

感谢您的帮助.

推荐答案

此修改如何?我也遇到过同样的情况.当时,我使用了这种解决方法.我认为针对您的情况可能有几种解决方案.因此,请将此视为其中之一.

How about this modification? I have experienced the same situation with you. At that time, I used this workaround. I think that there might be several solutions for your situation. So please think of this as one of them.

  • 在复制表的情况下,当包含图像的单元格时,它将图像从源表复制到目标表.由此,可以看到图像.流程如下.
  • In the case that a table is copied, when there are the cells including images, it copies the images from source table to the target table. By this, the image can be seen. The flow is as follows.
  1. 将源表复制到目标表.
  2. 检索单元格中的段落.
  3. 检索图像.
  4. 从目标单元格中​​删除图像.
  5. 将图像从源单元格复制到目标单元格.

请进行如下修改.

else if( type == DocumentApp.ElementType.TABLE){
  body.appendTable(element);
}

至:

else if( type == DocumentApp.ElementType.TABLE){
  var dstTable = body.appendTable(element);
  var srcTable = element.asTable();
  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 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().clear();
            var dstImg = dstParagraph.insertInlineImage(l, srcImg.getBlob());
            dstImg.setWidth(srcImg.getWidth());
            dstImg.setHeight(srcImg.getHeight());
          }
        }
      }
    }
  }
}

参考文献:

  • 类表
  • TableCell类
  • InlineImage类
  • 尽管在我的环境中,我可以确认将问题中的图像用作示例情况时,此修改后的脚本可以工作,但是,如果在您的环境中不可行,我很抱歉.

    Although in my environment, I could confirm that this modified script worked when the image in your question was used as the sample situation, if this didn't work in your environment, I'm sorry.

    对于您添加的情况,请按如下所示修改脚本.

    For your added situation, please modify your script as follows.

    else if( type == DocumentApp.ElementType.TABLE){
      body.appendTable(element);
    }
    

    收件人:

    else if( type == DocumentApp.ElementType.TABLE){
      var dstTable = body.appendTable(element);
      var srcTable = element.asTable();
      copyTable(srcTable, dstTable);
    }
    

    请添加以下功能.

    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().clear();
                  var dstImg = dstParagraph.insertInlineImage(l, srcImg.getBlob());
                  dstImg.setWidth(srcImg.getWidth());
                  dstImg.setHeight(srcImg.getHeight());
                }
              }
            }
          }
        }
      }
    }
    

    对于您添加的情况,请按如下所示修改脚本.

    Edit 2:

    For your added situation, please modify your script as follows.

    else if( type == DocumentApp.ElementType.TABLE){
      body.appendTable(element);
    }
    

    收件人:

    else if( type == DocumentApp.ElementType.TABLE){
      var dstTable = body.appendTable(element);
      var srcTable = element.asTable();
      copyTable(srcTable, dstTable);
    }
    

    请添加以下功能.

    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();
                  var dstImg = dstParagraph.insertInlineImage(l, srcImg.getBlob());
                  dstImg.setWidth(srcImg.getWidth());
                  dstImg.setHeight(srcImg.getHeight());
                }
              }
            }
          }
        }
      }
    }
    

    这篇关于如何使用Google脚本(以编程方式)将图像从一个表单元格复制到Google文档中的另一个表单元格中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆