4
votes

Is it possible to create a new document (either using a template or completely dynamic) using the Google Drive APIs? I am working with a client that requires generation of word documents. I tired looking up but I couldn't find sufficient documentation on how to "create and format" documents using the APIs.

For example, can I include the client's company logo on the top of the doc programmatically? I'd like whatever I am doing for this client to be generic (i.e including logo at the top etc) to be dynamic, so I can re-purpose this for other clients as well.

I also am having difficulty finding documentation on any kind of formatting we could do on the documents (bold, italic, new paragraph) etc

3

3 Answers

5
votes

The Google Drive API only deals with whole file operations. It has no understanding of the content of the files, including formatting.

You have two options:-

  1. Use Apps Script (ie. not the Drive API) which has document manipulation features. See https://developers.google.com/apps-script/reference/document/

  2. Create your templates in HTML (or Word, OpenOffice), then upload them to a new file, setting the option to 'convert to google docs'

1
votes

It is possible to create new documents as well as make a copy of already existing documents (templates) Some time ago I've created simple example how to work with templates:

On your Google drive create folder Templates

Inside this folder create formated document with images, texts etc... Replace dynamic values with {A},{B},{C} representing spreadsheet column names in this document

Than create spreadsheet and fill some data in it. Inside this spreadsheet navigate to Tools->Script gallery and search for fast template generator -> Install it

Close document and re-open it. Script will automatically execute onOpen function which will add Template generator to spreadsheet menu. Now just choose row from which you would like to populate values in template and launch Template generator-> Generate from template.

A new document with filled values will be generated from chosen template.

You can find source code and customize it in according your need in Tools->Script editor in this spreadsheet, so result should be in PDF or sent via email etc...

0
votes

EDIT: Seems this approach does not work, as Google Drive API only allows copying files across current user's Drive.

I found for me suitable approach was to create a document in my own Google Drive. Style it accordingly, add base text etc. and give it read-only permission to everyone. Then from the Google Drive API use copy file functionality and copy it into the current users Drive.