Google apps script 从Google文档中获取特定表中的InlineImage

Google apps script 从Google文档中获取特定表中的InlineImage,google-apps-script,google-docs,Google Apps Script,Google Docs,我遇到了以下问题。我有一个Google文档,它包含一堆表对象,其中一些表本身包含内联图像 使用Body.getImages()函数,我应该能够获得整个文档的图像(对吗?)。但是,有没有办法从特定的表中获取图像,或者有没有办法确定Body.getImages()方法检索到的图像位于哪些表中 如果您想知道这是用来做什么的:My Google Doc用于存储多个选择题,每个问题都由一个表表示。我试图编写一个脚本将这些问题导出为特定格式,但遇到了一个问题,即其中一些问题包含图像。正确-正文。getIma

我遇到了以下问题。我有一个Google文档,它包含一堆表对象,其中一些表本身包含内联图像

使用
Body.getImages()
函数,我应该能够获得整个文档的图像(对吗?)。但是,有没有办法从特定的表中获取图像,或者有没有办法确定
Body.getImages()
方法检索到的图像位于哪些表中


如果您想知道这是用来做什么的:My Google Doc用于存储多个选择题,每个问题都由一个表表示。我试图编写一个脚本将这些问题导出为特定格式,但遇到了一个问题,即其中一些问题包含图像。

正确-
正文。getImages()
将返回一个图像数组

我们可以使用这个图像数组来查找每个图像对应的表。如果我们对每个图像使用递归函数,我们可以
getParent()
沿着文档树向上移动,直到找到特定图像的父表,然后列出该表的元素编号(ChildIndex)。如果表中有“问题#”标题,我们可以搜索它并返回所定位表的问题编号

    function myFunction() {
      var doc = DocumentApp.getActiveDocument();
      var body = doc.getBody();
      var tables = body.getTables();
      var images = doc.getBody().getImages();
      
      Logger.log("Found " + images.length + " images");
      Logger.log("Found " + tables.length + " tables");
      
      //list body element #'s for each tables
      let tableList = []
      tables.forEach(table => tableList.push(String(table.getParent().getChildIndex(table))))
      Logger.log("Tables at body element #s: ", tableList); 
      
      function findQuestionNumber (element, index) {
        parent = element.getParent() 
        //IF found the parent Table
        if (parent.getType() == DocumentApp.ElementType.TABLE) {
          //Find the question # from the Table
          let range = parent.findText("Question")
          //Output where this image was found. (The childindex)
          Logger.log("found Image", String(index + 1), "in ", range.getElement().getParent().getText(), " at body element #", String(parent.getParent().getChildIndex(parent)));
          return
          //use recursion to continue up the tree until the parent Table is found
        } else {
          findQuestionNumber(parent, index)
        }
      }
     
      //Run Function for each image in getImages() Array
      images.forEach((element, index) => findQuestionNumber(element, index));
      
    }


我认为,提供您当前的脚本以及脚本发布的细节将帮助用户考虑解决方案。非常感谢!我完全没有注意到有一个getParent()函数!你的解释很有帮助!