1
votes

I'm reading a Google Doc with landscape orientation using the Google Drive API v3, and trying to replace a few words in the doc with something else.

For example, if the document contains "Text_to_replace" I search for that string and replace that by "Hello World!"

But every time I update the Google document's content using the Google Drive API, the document's orientation always changes to portrait. Even when the document's orientation was originally set to landscape.

I'm not adding code to this post since I'm able to replicate this issue using any http client (like https://www.hurl.it/)

Here's how to replicate the issue:

I) Setting up the document

In order to replicate the issue, you need a document with orientation = landscape.

enter image description here

II) What the code is doing

1) We first log in through the API using Service Account (https://developers.google.com/identity/protocols/OAuth2ServiceAccount)

2) We get the FileResource instance of the document (https://developers.google.com/drive/v3/reference/files/get)

3) We get the HTML export of the file (GET HTML request to https://docs.google.com/feeds/download/documents/export/Export?id=(document id)&exportFormat=html)

4) We replace some words in the document's HTML (for example, we replace the text "Text_to_replace" by "Hello World!")

5) We make an update through the API.

https://www.googleapis.com/upload/drive/v3/files/(document id)?uploadType=media

User-Agent apex-google-api
Authorization Bearer (service account authentication token)
Content-Type text/html

This is when the document loses the landscape orientation (it's changed to portrait)

I've tried to only do the update, skipping steps 2 to 4, and I still see the issue (orientation goes to portrait).

Maybe the problem is that I'm sending the entire HTML back to the document when updating? Is there a way to update the document's content without losing the orientation?

Thanks in advanced

1
There's a ticket raised regarding setting the page orientation. Its suggested that Drive API doesn't support the features like changing orientation. What may be happening is once an update is done, its automatically changing it to portrait as well. If you check the API references as well, there are no properties to change the orientation.adjuremods
It's true, there's no properties or any way to set the document's orientation through the API. But it's weird that the orientation is already landscape, and it's being changed back to portrait. Since there's no way to change orientation through the API, this is a really surprising and unnexpected behaviour.Daniel Terraza

1 Answers

1
votes

You are exporting as HTML and then updating which does a complete replace rather than a minor change to a few words. HTML doesn't really have a page orientation setting so orientation is effectively lost in translation. You could try exporting/updating via another format like OpenOffice, PDF or MS Word.