concatenating documents

107 views
Skip to first unread message

TempleSnr

unread,
Sep 2, 2024, 9:50:40 AMSep 2
to Google Apps Script Community
I'm interested to know if anyone can give me the gist of a better way to concatenate documents using apps script? 

The various references I've seen say that this is not actually possible and one has to break down the second document into its elements (link) then copy them one by one. Of course, we now have "Tabs" as well as the "Body" of a document but (AFAIK) I can't copy and paste a Tab (I haven't had to deal with one yet) or Body, I have to iterate through the elements of the second document instead.

Is this right, or do we really have to get into iterative, recursive copying of element types to do such a simple thing?

Thanks in advance ....

DimuDesigns

unread,
Sep 2, 2024, 1:57:38 PMSep 2
to Google Apps Script Community
Looking at both the Google Docs API documentation and GAS Docs built-in service documentation I don't see a way around it.

However, it shouldn't be too hard for you to work out a solution if you're familiar with tree-traversal algorithms such as DFS(Depth-First Search) and BFS(Breath-First Search). 

Google Docs have a tree-like structure with a hierarchy of parent and child nodes (see https://developers.google.com/docs/api/concepts/structure), so look up methods that allow you to efficiently extract data from tree-like data structures and see if you can apply them to your use case.

Keith Andersen

unread,
Sep 2, 2024, 5:02:40 PMSep 2
to google-apps-sc...@googlegroups.com

Why not convert to PDF and then combine PDFs?



My website: https://sites.google.com/view/klaweb/
Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/dcc76657-76db-48c2-8e8e-9e09a8764c6fn%40googlegroups.com.

TempleSnr

unread,
Sep 3, 2024, 6:13:37 AMSep 3
to Google Apps Script Community
thank you @keith and @dimudesigns - I'll do some further learning (DFS and BFS) and research and see what I can do.
It's good to have my general understanding confirmed though.

Jonathan Butler

unread,
Sep 3, 2024, 8:48:42 AMSep 3
to google-apps-sc...@googlegroups.com
The best I have done about this is download both documents as HTML, combine this HTML with Cheerio, save the new document as an HTML File, and then convert the HTML to a Google Doc using Drive.Files.Insert. It would work mostly depending on the complexity of the document. Unfortunately, I did this at a company that is no longer around and the code is lost to time.

Disclaimers apply, for full details see: https://hackney.gov.uk/email-disclaimer

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.

Ed Robinson

unread,
Sep 4, 2024, 9:22:05 AMSep 4
to Google Apps Script Community
There are three approaches you can use:
1. Simple. As Jonathan + Kieth suggest: use an export mechanism (pdf/html/docx) to export the two documents from Google Drive, combine them outside of Google Drive, then import the combined doc
2. Text only. As DimuDesigns suggests: With document2, walk the "child" element array, create a copy of each paragraph, then append the paragraph into document1 using appendParagraph
3. Full formatting. This is super complicated, because there are so many edge cases

I've implemented formatting copy from one doc to another in my "Lets Go Templates!" add-on. There are a huge number of quirks you have to work around. For example: paragraphs with footnotes, tables containing images, smart chips - these can't be appended as-is into the destination document, and have to be special-cased.

For a quick solution, I recommend 1. or 2. 

TempleSnr

unread,
Sep 4, 2024, 9:33:30 AMSep 4
to Google Apps Script Community
thank you @Ed and @Jonathan too - I'm glad it looks as though we're on the same page (sorry!) - yes.
I "discovered" that I could do a search and replace in an entire `body` thus keeping all formatting untouched, which is great for a mail merge.
But I wanted to do a (traditional) mail merge to a single document .... doing a mail merge to multiple documents is easy, you take a copy of the template, do your repeat search and replace, then write the document and make the next copy until you've finished all the merge rows.
Having done that, I wanted to merge every copy into a single document (yes, there are limits though) so that there's not hundreds of documents after the merge. That's when I found out it wasn't easy and I totally agree with @Ed that having to process every type of element (including those that you can't do anything with) recursively is a whole world of pain.
I don't really find options 1 & 2 very attractive - they're not quite what I'm looking to do.
Never mind.

Reply all
Reply to author
Forward
0 new messages