【问题标题】:Merging Google Docs from links in a Google Spreadsheet, selecting only some of them从 Google 电子表格中的链接合并 Google 文档,仅选择其中一些
【发布时间】:2021-03-31 14:51:10
【问题描述】:

所以,我有一个 Google 电子表格,其中我为每个条目(使用模板)创建了一个 Google 文档,并在每行的末尾添加了指向相应文档的链接。到现在为止还挺好。问题是现在我希望能够将几个文档合并为一个,但我似乎无法管理,因为我希望能够导出它们的不同组合。在我发现的所有示例中(如this one),您需要输入 DocumentID 或合并all the documents in a folder 或电子表格.. 我想要一些可以在我想要组合的条目的第一列中写“OK”的东西(我的状态栏)然后用脚本说

“如果状态列显示“OK”,则从 DocID 列检索 ID,并将其与所有其他也显示“OK”的 DocID 组合,无论它们有多少”.

到目前为止,我设法做到这一点的唯一方法是手动输入 DocID:

function mergeGoogleDocs() {

  var docIDs = ['docID_1','docID_2','docID_3','docID_4'];
  var baseDoc = DocumentApp.openById(docIDs[0]);

  var body = baseDoc.getActiveSection();

  for (var i = 1; i < docIDs.length; ++i ) {
    var otherBody = DocumentApp.openById(docIDs[i]).getActiveSection();
    var totalElements = otherBody.getNumChildren();
    for( var j = 0; j < totalElements; ++j ) {
      var element = otherBody.getChild(j).copy();
      var type = element.getType();
      if( type == DocumentApp.ElementType.PARAGRAPH )
        body.appendParagraph(element);
      else if( type == DocumentApp.ElementType.TABLE )
        body.appendTable(element);
      else if( type == DocumentApp.ElementType.LIST_ITEM )
        body.appendListItem(element);
    }
  }
}

但是,如果我想合并超过 4 或 5 个文件,这很乏味,我必须手动获取表中的信息,而不是过滤我的数据,在状态列中写入 OK 并让 Google Scripts 合并所有文件我。

有人知道怎么做吗? 谢谢!

【问题讨论】:

    标签: google-apps-script google-sheets merge google-docs


    【解决方案1】:

    解决办法:

    在合并之前,首先使用 Sheets API 使用此代码从电子表格中填充您的 docID 数组。这假设您在 col A 中有 OK 状态列,在 col B 中有 ID。

    示例代码:

    function mergeGoogleDocs() {
      // Populate document IDs
      var docIDs = [];
      var ss = SpreadsheetApp.getActiveSheet();
      var range = ss.getRange(1,1,ss.getLastRow(),2).getValues();
    
      for (j = 0; j < range.length; j++) {
        if (range[j][0] == 'OK') {
          docIDs.push(range[j][1]);
        }
      }
    
      // Merge documents
      var baseDoc = DocumentApp.openById(docIDs[0]);
    
      var body = baseDoc.getActiveSection();
    
      for (var i = 1; i < docIDs.length; ++i ) {
        var otherBody = DocumentApp.openById(docIDs[i]).getActiveSection();
        var totalElements = otherBody.getNumChildren();
        for( var j = 0; j < totalElements; ++j ) {
          var element = otherBody.getChild(j).copy();
          var type = element.getType();
          if( type == DocumentApp.ElementType.PARAGRAPH )
            body.appendParagraph(element);
          else if( type == DocumentApp.ElementType.TABLE )
            body.appendTable(element);
          else if( type == DocumentApp.ElementType.LIST_ITEM )
            body.appendListItem(element);
        }
      }
    }
    

    样本数据:

    参考资料:

    Class Sheet

    【讨论】:

    • 像魅力一样工作!你太棒了,非常感谢!!
    • 非常感谢! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    相关资源
    最近更新 更多