首页 > 解决方案 > 谷歌驱动器应用程序脚本,在合并文档时将图像复制到每个文档的最后一页

问题描述

我正在创建一个谷歌 API 附加脚本。有一个 google sheet 有很多 google 文档链接。我检索这些链接打开文件,浏览其内容并将其附加到新文档中。需要对此列表中的所有文档执行此操作。在挣扎了一段时间后,我得到了所有的工作,除了定位的图像。要么我根本没有得到图像,要么图像被复制进来,但从那时起,它被复制并粘贴在文档的每一页上。我可能只是在编码过程中的某个地方失败了,但似乎找不到我自己的缺陷。请注意,我在 IT 部门工作,但不是作为开发人员,所以是的,代码写得很糟糕:'(

我反复尝试并重新编辑了我的代码,以确保我正确地拾取定位的图像(例如,通过放入文本而不是图像等,这一切似乎都正常工作,但是定位的图像在整个文档中不断重复。 )

请注意,下面我只包括了坏函数和我正在调用的主函数,而不是所有从 excel 表中获取的东西,因为它工作正常。

更新:我已经更新了我的代码。它现在成功地找到、设置布局等并放置定位的图像。

但是,从那时起,在合并文档中较早放置的定位图像将在每个附加文档的最后一段重复。

function main() {
  var spreadsheetid = "Fill_this_with_spreadsheet_ID"; 

  var DocIDS = []; 
  DocIDS = getDocIDFromSpreadsheet(spreadsheetid);
  mergeGoogleDocs(DocIDS);
  //remove_blank(DocIDS);
}

/*Function to append contents of document to another document
  for (var i = 1; i < List.length; ++i ) {
    var otherBody = DocumentApp.openById(List[i]).getActiveSection();
    var totalElements = otherBody.getNumChildren();
    baseDoc.saveAndClose()
    var baseDoc = DocumentApp.openById(DocIDS[0]);
    var body = baseDoc.getActiveSection();
    for( var j = 0; j < totalElements; ++j ) {
      var element = otherBody.getChild(j).copy();
      var type = element.getType();
      if( type == DocumentApp.ElementType.PARAGRAPH ){
          var positionedImages = element.getPositionedImages();
          if ( positionedImages == "PositionedImage") {
          //This is required because the positionedImage needs a "new" pragraph to anchor to...
          var paragraph = body.appendParagraph("");
          var tempvalue = positionedImages[0].getLayout();
          var tempheight = positionedImages[0].getHeight();
          var tempwidth = positionedImages[0].getWidth();
          var tempblob = positionedImages[0].getBlob();
          paragraph.addPositionedImage(tempblob);
          var positionedImages2 = paragraph.getPositionedImages();
          positionedImages2[0].setLayout(tempvalue);
          positionedImages2[0].setLayout(tempvalue);
          positionedImages2[0].setHeight(tempheight);
          positionedImages2[0].setWidth(tempwidth);          
          //To-do, fix repeated multiplication of positioned Images on all following documents at the final paragraph.
          }
        else {
        body.appendParagraph(element);
        }
        }
      else if( type == DocumentApp.ElementType.TABLE ){
        body.appendTable(element);}
      else if( type == DocumentApp.ElementType.LIST_ITEM ) {
        glyphType = element.getGlyphType();
        body.appendListItem(element);
        element.setGlyphType(glyphType);
        }
      else
        throw new Error("Unknown element type: "+type);
    }
    body.appendPageBreak()

  }

我希望将所有文档合并到一个文档中。虽然它确实合并了所有内容,但它在定位的图像上出错了。当我运行代码时,我会在第一次插入后的每个新附加文档上复制定位图像。它只需要包含一次,而不是每个文档。

我已经挣扎了几个晚上,迫切需要指针和/或适当的修复。

标签: google-drive-api

解决方案


推荐阅读