使用 Apps 脚本将部分从 Google Docs 复制到另一个 Doc

斯宾塞

我已成功使用此代码将一个文档的全部内容复制到另一个文档中:

const newestFile = DocumentApp.openById("ID").getBody();
const archive = DocumentApp.openById("ID").getBody();
let index = 12;
  let el, type;
  for (let i = 0; i < newestFile.getNumChildren(); i++){
    el = newestFile.getChild(i);
    type = el.getType();
    switch (type){
      case DocumentApp.ElementType.PARAGRAPH:
        archive.insertParagraph(index,el.copy());
        index++;
        break;
      case DocumentApp.ElementType.LIST_ITEM:
        archive.insertListItem(index,el.copy());
        index++;
        break;
      case DocumentApp.ElementType.TABLE:
        archive.insertTable(index,el.copy());
        index++;
        break;
    }
  }

但是,我现在需要将文档的一部分复制到另一个文档中,但我无法弄清楚。如果我知道如何获取任何元素的主体索引,我可以用同样的方式来做,但我不知道这是否可能。我需要复制的文本总是以特定文本(“当前周”)开头,并在特定文本(“ARCHIVE”)之前立即结束。

智者

描述

这是一个如何在某些文本之间复制的简单示例。我只介绍了段落和表格,但可以处理任何其他类型的元素。

测试文件

在此处输入图像描述

脚本

function myFunction() {
  try {
    let doc = DocumentApp.getActiveDocument();
    let body = doc.getBody();
    let count = body.getNumChildren();
    doc = DocumentApp.create("dummy");
    let copy = doc.getBody();
    let start = false;

    for( let i=0; i<count; i++ ) {
      let child = body.getChild(i);
      if( child.getType() == DocumentApp.ElementType.PARAGRAPH ) {
        if( child.asParagraph().findText("Current Week") ) start = true; 
        if( start ) copy.appendParagraph(child.asParagraph().copy());
        if( child.asParagraph().findText("ARCHIVE") ) break;
      }
      else if( child.getType() == DocumentApp.ElementType.TABLE ) {
        if( start ) copy.appendTable(child.asTable().copy());
      }
    }
  }
  catch(err) {
    console.log("Error in myFunction - "+err)
  }
}

参考

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 Google Apps 脚本的另一个附加组件 Google Docs

将工作表复制到另一个电子表格[Google Apps脚本]

Google Apps脚本,使用格式将一个电子表格复制到另一电子表格

使用 terraform 将容器映像从一个 Google 项目复制到另一个项目

使用Apps脚本在云端硬盘上将文件从一个文件夹复制到另一个文件夹

在Google Apps脚本中使用google docs api

使用Apache Airflow将文件从一个Google Cloud Storage Bucket复制到另一个文件

如何将一个Google文档复制到另一个

使用 Django 将目录从 Google Cloud Storage Bucket 递归复制到另一个 Google Cloud Storage Bucket

使用 MFC 将一个结构复制到另一个结构

使用Python使用Google API将电子表格复制到另一个电子表格

将过滤器数据从一个电子表格文件复制到另一个电子表格-Google Apps脚本

使用Google Doc Apps脚本缓存选定的文本元素

在Google Doc Apps脚本中使用RegEx替换文本

如何根据条件将单元格值复制到另一个工作表(Apps 脚本)

Google Apps Script - 将公式从一个单元格复制到新单元格中的另一个递增 A1 表示法

使用Google Apps脚本将图像从Google表格复制到Google文档

使用xsl 2.0将元素从1个xml复制到另一个

无法使用Google Docs API复制Google Doc(无“ copy”属性)

将单元格值复制到另一个 Google 工作表的 Google 脚本

Apps脚本中的Google Docs API

Google Docs Apps脚本getBackgroundColor(Offset)

使用Apps脚本将非连续单元格从Google表格中的一列复制到另一列

如何将一个Google工作表复制到另一个Google工作表

使用Javascript将元素从1个对象部分复制到另一个对象

使用 C++ 将文件内容复制到另一个文件

在python中使用条件将列表的元素复制到另一个列表

使用python将文件复制到另一个文件夹

使用django将文件复制到另一个文件夹吗?