Using Apps Script to transfer a specific part from one Google Doc to another

I’ve got a script that can move an entire Google Doc to another one. But now I need to move just a part of it. Here’s my problem:

I want to copy text that starts after ‘Current Week’ and ends right before ‘ARCHIVE’. I’m not sure how to pinpoint these sections in the code.

Here’s a basic script I made that copies everything:

function copyDocContent() {
  const sourceDoc = DocumentApp.openById('SOURCE_ID').getBody();
  const targetDoc = DocumentApp.openById('TARGET_ID').getBody();
  let insertIndex = 0;

  sourceDoc.getChildren().forEach(element => {
    switch(element.getType()) {
      case DocumentApp.ElementType.PARAGRAPH:
        targetDoc.insertParagraph(insertIndex++, element.copy());
        break;
      case DocumentApp.ElementType.LIST_ITEM:
        targetDoc.insertListItem(insertIndex++, element.copy());
        break;
      case DocumentApp.ElementType.TABLE:
        targetDoc.insertTable(insertIndex++, element.copy());
        break;
    }
  });
}

How can I tweak this to copy just the part I need? Any ideas?

hey samuel, i think u can modify ur script to search for ‘Current Week’ and ‘ARCHIVE’ in the source doc. then only copy the content between those markers. you’ll need to use something like findText() to locate the phrases and getChildIndex() to get their positions. good luck with ur project!

I’ve dealt with similar document manipulation tasks before. Here’s a potential solution:

Modify your function to use the findText() method to locate ‘Current Week’ and ‘ARCHIVE’. Then, extract the content between these markers.

You’ll need to determine the element indices where these phrases appear, then copy only the elements within that range. Be sure to handle cases where markers aren’t found.

Consider using a while loop to iterate through elements, starting after ‘Current Week’ and stopping before ‘ARCHIVE’. This approach offers more precise control over the transferred content.

Remember to test thoroughly, as document structure can sometimes cause unexpected behavior. If you encounter issues, you might need to fine-tune the element selection process.

I’ve tackled a similar challenge before, and here’s what worked for me:

Modify your existing function to include a search for the specific text markers. You can use the findText() method to locate ‘Current Week’ and ‘ARCHIVE’, then extract the content between them.

Here’s a rough outline of how you might approach this:

  1. Find the start and end indices using findText()
  2. Use a loop to iterate through elements between these indices
  3. Copy only the elements within this range to the target document

Remember to handle cases where the markers aren’t found or are in unexpected positions. Also, you might need to adjust for any formatting or structural elements that could affect the copying process.

This approach should give you more precise control over what gets transferred between documents. Let me know if you need any clarification on implementing these changes.