G
G
Grigory Boev2019-12-17 11:31:33
Google Apps Script
Grigory Boev, 2019-12-17 11:31:33

How to clone the contents of a GoogleDocs document into it?

Good afternoon. I'm making a script to fill in a document template, a typical task. Usually I do this: in the document template there are unique pieces of text that are replaced by other text from the "database". For example {{Passport number}} is replaced by 0123 456789 .
The problem is that you need to combine all the output data into one file. Those. duplicate the template. I'm trying to do this:

var doc = DocumentApp.openById(docId);  //Открываем документ по Id
    var docBody = doc.getBody(); // Получаем содержимое
    var paragraphes = docBody //Получение абзацев текста
        .getParagraphs()
        .map(function(el){return el.copy()}); //

// Замена текста в docBody 
//docBody.replaceText(findText, replace[rep][match]);

//Копирование абзацев в docbody
      for (var i=0;i<paragraphes.length;i++){
        try{
        docBody.appendParagraph(paragraphes[i]);
           }catch (e){
             Logger.log(e.message + " rep=" + rep + " paragr=" + i);
//rep - номер документа
//paragr - номер абзаца
           };   
      };

The first copy is fine, but then errors are obtained:
[19-12-17 00:06:22:760 PST] Необходимо открепить элемент. rep=1 paragr=0
[19-12-17 00:06:22:761 PST] Необходимо открепить элемент. rep=1 paragr=1
[19-12-17 00:06:22:762 PST] Необходимо открепить элемент. rep=1 paragr=2
[19-12-17 00:06:22:763 PST] Необходимо открепить элемент. rep=1 paragr=3
[19-12-17 00:06:22:764 PST] Необходимо открепить элемент. rep=1 paragr=4
[19-12-17 00:06:22:765 PST] Необходимо открепить элемент. rep=1 paragr=5
[19-12-17 00:06:22:766 PST] Необходимо открепить элемент. rep=1 paragr=6
[19-12-17 00:06:22:767 PST] Необходимо открепить элемент. rep=1 paragr=7
[19-12-17 00:06:22:768 PST] Необходимо открепить элемент. rep=1 paragr=8
[19-12-17 00:06:22:769 PST] Необходимо открепить элемент. rep=1 paragr=9
[19-12-17 00:06:22:970 PST] Необходимо открепить элемент. rep=1 paragr=10
[19-12-17 00:06:23:372 PST] Необходимо открепить элемент. rep=1 paragr=11
[19-12-17 00:06:23:973 PST] Необходимо открепить элемент. rep=1 paragr=12
[19-12-17 00:06:24:775 PST] Необходимо открепить элемент. rep=1 paragr=13
[19-12-17 00:06:25:776 PST] Необходимо открепить элемент. rep=1 paragr=14
...

This is the first time I am facing this issue. How to copy the contents of a document into it? Google didn't help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Stoyanov, 2020-06-01
@stomaks

This code copies all content from the current document to another.

var doc_prototype = DocumentApp.getActiveDocument();
var otherBody = doc_prototype.getBody();

// Это можно заменить на текущий документ
var doc = DocumentApp.openById(  "document_id" );
var body = doc.getBody();
      
var totalElements = otherBody.getNumChildren();

// Тут можно вставить цикл для вставки содержимого нужное количество раз
for( var j = 0; j < totalElements; ++j ) {
        var element = otherBody.getChild(j).copy();
        var type = element.getType();
        if( type == DocumentApp.ElementType.PARAGRAPH )
          body.appendParagraph(element);
        else if( type == DocumentApp.ElementType.TABLE )
          body.appendTable(element);
        else if( type == DocumentApp.ElementType.LIST_ITEM )
          body.appendListItem(element);
        else if( type == DocumentApp.ElementType.INLINE_IMAGE )
          body.appendImage(element);
        // add other element types as you want
        
        else
          throw new Error("According to the doc this type couldn't appear in the body: "+type);
      }
      
body.appendPageBreak();

---
Maxim Stoyanov (stomaks), developer of Google Apps Script .
g-apps-script.com
stomaks.me

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question