0

I'm writing a merging program. Instead of creating one document per input row, I want the merged (template with input values replaced) output from each input row added to a single document.

And the template is not just text. It's another Google doc with a standalone table.

I started with the mail merge example and it works fine but produces a separate doc for each input row. I've written a helper function that I call for each output doc, with "dest_doc" set to my summary doc.

Sadly, this does not work since the API seems only to permit unstyled text insertion, not contents-from-a-Doc.

It works fine when using 'hello world ' -- this is properly replicated in the output file. But when I replace it with body or body['contents'] it fails.

Is there any way of doing this? I'd balk at writing a contents-tree-to-UpdateRequest translator.

def append_contents(dest_doc, source_doc):
    """Copy the contents of the source doc to the end of the destination doc
    """
    document = DOCS.documents().get(documentId=source_doc).execute()
    body = document.get('body')
    
    requests = [
         {
            'insertText': {
                'endOfSegmentLocation': {
                    'segmentId': '',
                },
                'text': 'hello world '
            }
        },
    ]
    
    DOCS.documents().batchUpdate(documentId=dest_doc, body={'requests': requests}).execute()
    return

Thanks for your time.

bjmckenz
  • 23
  • 1
  • 3

1 Answers1

1

Unfortunately you can't use the insertText request to append a complex object (as the Body of another Document) to your Document. This request only accepts text.

I'm afraid that the only solution here would be to parse the elements of your documents and create a request translator for each one of their types.

Even in Apps Script as you can see in this example, the merge is done element per element according to its type (Text, Table, Image, Paragraph, etc.).

However, if this feature is really important to you, you should definitely submit a feature request here

Alessandro
  • 2,848
  • 1
  • 8
  • 16