Search code examples
javascriptgoogle-docsgoogle-docs-api

Copying text from one google Doc, to another active google Doc


I have a body of text in a master document(google Doc) that is located in an Admin Folder, that I would like to copy to the active google Doc that I am working on (that will be blank, if that helps).

I am quite new at JS, and have been trying to use the getFileById(fileId).makeCopy, but am struggling.

Any pointers as to what tutorials I should look at or how I could go about doing this would be of great help.


Solution

  • Open script.google.com from the document:

    Tools -> Script Editor

    Use this function:

    function textCopy() {
      var doc = DocumentApp.openById('ID_OF_DOC_TO_BE_COPIED').getText();
      var body = DocumentApp.getActiveDocument().getBody();
      var text = body.editAsText();
      // Insert text at the beginning of the document.
      text.insertText(0, doc);
    }
    

    Run it. Some permissions will be asked, be sure to allow it.

    More info about doc text manipulation can be found in Class Text.