将图片替换为Google文档中的表格 [英] Replace image with table in google doc

查看:80
本文介绍了将图片替换为Google文档中的表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Google文档中包含该图像的表替换选定的图像.当我运行代码时,没有表格插入到Google文档中.

I want to replace a selected image with a table containing that image in a Google Doc. No table is inserted into the google doc when I run the code.

    function insertImage(ID, caption) {
    var selection = DocumentApp.getActiveDocument().getSelection();
    if (selection) {
        var elements = selection.getSelectedElements();
        var tImg = elements[0].getElement();
        var cells = [
            [tImg.asInlineImage(), ID+': '+caption]
        ];
        var parentElement = tImg.getParent();
        parentElement.insertTable(parentElement.getChildIndex(tImg) + 1, cells)
        tImg.removeFromParent();
    }
}

不会删除图像,也不会添加表格.谢谢!

The image isn't removed and the table isn't added. Thanks!

推荐答案

  • 您要将选定的图像替换为表格.
  • 您要将选定的图像移到插入表的单元格的第一列.文本放在第二栏中.
  • 如果我对您的问题的理解是正确的,那么该修改如何?

    If my understanding of your question is correct, how about this modification?

    • insertTable()的方法是类主体的方法.
    • 需要检索图像的位置作为文档正文的子索引.
    • 在插入表格后,图像将插入到插入表格的单元格中.
    • 在此修改中,当删除图像时,它将删除图像的段落.这样,可以在不包括换行符的情况下将图像替换到表格中.这种情况与在单元格中插入图像相同.
    • getSelectedElements() 已弃用.因此,请使用getRangeElements().
    • Method of insertTable() is the method of Class Body.
    • The place of image is required to be retrieved as the child index of the document body.
    • Image is inserted to the cell of the inserted table after the table was inserted.
    • In this modification, when the image is deleted, it deletes the paragraph of image. By this, the image can be replaced to the table without including the line break. This situation is the same with inserting the image in the cell.
    • getSelectedElements() was deprecated. So please use getRangeElements().

    当以上几点反映到脚本中时,它如下所示.

    When above points are reflected to the script, it becomes as follows.

    function insertImage(ID, caption) {
    //  ID = "sample ID"; // for sample
    //  caption = "sample caption"; // for sample
    
      var doc = DocumentApp.getActiveDocument(); // Added
      var body = doc.getBody(); // Added
      var selection = doc.getSelection(); // Modified
      if (selection) {
        var elements = selection.getRangeElements(); // Modified
        var e = elements[0].getElement(); // Modified
        if (e.getType() == DocumentApp.ElementType.INLINE_IMAGE) { // Added
          var tImg = e.asInlineImage(); // Modified
          var parentElement = tImg.getParent();
          var cells = [["", ID + ': ' + caption]]; // Modified
          var table = body.insertTable(body.getChildIndex(parentElement), cells); // Modified
          var cell = table.getCell(0, 0); // Added
          cell.insertImage(0, tImg.getBlob()).getParent().asParagraph().getNextSibling().removeFromParent(); // Added
          tImg.getParent().asParagraph().removeFromParent(); // Modified
        }
      }
    }
    

    输入:

    Input:

    • 在脚本中,使用了所选元素的第一个图像.所以我遵循了这一点.
    • insertTable()
    • insertImage()
    • getRangeElements()

    如果这不是您想要的,请告诉我.我想修改它.

    If this was not what you want, please tell me. I would like to modify it.

    这篇关于将图片替换为Google文档中的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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