NBor94
NBor94

Reputation: 23

Copying Paragraph from one document to another and pasting it at the top

I'm currently working with Google Apps Script and trying to achieve the following:

  1. Have a source document with a paragraph
  2. Have a target document
  3. Run the function to copy the paragraph from the source documeent into the target document
  4. Run the function again to paste new content always at the top of the document, prior to the previous addition.

I've been able to achieve steps 1 to 3 with the following script I came across:

function menuItem1() {
  DocumentApp.getUi() // Or DocumentApp or FormApp.
  var sourceDoc = DocumentApp.getActiveDocument().getBody();
  var targetDoc = DocumentApp.openById('doc id goes here');
  var totalElements = sourceDoc.getNumChildren()
  for( var j = 0; j < totalElements; ++j ) {
    var body = targetDoc.getBody()
    var element = sourceDoc.getChild(j).copy();
    var type = element.getType();
    if( type == DocumentApp.ElementType.PARAGRAPH ){
      body.appendParagraph(element);
    }
    else if( type == DocumentApp.ElementType.LIST_ITEM){
      body.appendListItem(element);
      }
    }
  targetDoc.saveAndClose()
  DocumentApp.getUi().alert('New template added');
}

`

However it always pastes the new content at the bottom of the document. I now understand that this is due to the appendParagraph method and that insertParagraph is what I should be using. However I'm struggling to understand what index I should be using in order to have my text always paste at the top of the page.

Upvotes: 0

Views: 1049

Answers (1)

SputnikDrunk2
SputnikDrunk2

Reputation: 4048

SUGGESTION

You can use the insertParagraph & insertListItem methods as seen on this tweaked script below.

Tweaks made:

  • Reversed your loop to make sure copied paragraphs are still in order when being inserted to the top of the target docs file.
  • Used insertParagraph instead of appendParagraph.
  • Used insertListItem instead of appendListItem.

Script

function menuItem1() {
  DocumentApp.getUi() // Or DocumentApp or FormApp.
  var sourceDoc = DocumentApp.getActiveDocument().getBody();
  var targetDoc = DocumentApp.openById('DOCS ID');
  var totalElements = sourceDoc.getNumChildren();
  for (var j = (totalElements - 1); j >= 0; j--) { //Reversed the loop to make sure paragraphs/lists are still in order when inserted to the target sheet.
    var body = targetDoc.getBody()
    var element = sourceDoc.getChild(j).copy();
    var type = element.getType();
    if (type == DocumentApp.ElementType.PARAGRAPH) {
      body.insertParagraph(0, element); //Always insert at the top
    }
    else if (type == DocumentApp.ElementType.LIST_ITEM) {
      body.insertListItem(0, element.copy());; //Always insert at the top
    }
  }
  targetDoc.saveAndClose()
  DocumentApp.getUi().alert('New template added');
}

Demo

  • source docs file.

enter image description here

  • After running the script, here's the target docs file.

enter image description here

  • When the source docs file gets updated.

enter image description here

  • After running the script again, here's the target docs file.

enter image description here

Upvotes: 0

Related Questions