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

发布于 2025-01-16 22:20:47 字数 898 浏览 1 评论 0原文

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

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;
    }
  }

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

I've successfully used this code to copy the entirety of one doc into another 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;
    }
  }

However, I now need to copy a portion of a doc into another doc, and I can't figure it out. If I knew how to get the body index of any element I could do it the same way, but I don't know if that's even possible. The text I need to copy out will always be preceded by a specific text ("Current Week") and end immediatly before a specific text ("ARCHIVE").

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故笙诉离歌 2025-01-23 22:20:47

描述

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

测试文档

在此处输入图像描述

脚本

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());
      }
      else if( child.getType() == DocumentApp.ElementType.LIST_ITEM ) {
        if( start ) {
          let listItem = child.asListItem();
          let glyphType = listItem.getGlyphType();
          listItem = copy.appendListItem(listItem.copy());
          listItem.setGlyphType(glyphType)
        }
      }
    }
  }
  catch(err) {
    console.log("Error in myFunction - "+err)
  }
}

参考

Description

Here is a simple example of how to copy between certain text. I've only covered paragraphs and tables but any other type of Element can be handled.

Test Document

enter image description here

Script

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());
      }
      else if( child.getType() == DocumentApp.ElementType.LIST_ITEM ) {
        if( start ) {
          let listItem = child.asListItem();
          let glyphType = listItem.getGlyphType();
          listItem = copy.appendListItem(listItem.copy());
          listItem.setGlyphType(glyphType)
        }
      }
    }
  }
  catch(err) {
    console.log("Error in myFunction - "+err)
  }
}

Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文