2
votes

I've read through the API documentation at https://developers.google.com/drive/v2/reference/ however I cannot find the answer to my question. And attempts to google a solution have failed.

I have a series of previously uploaded small HTML files sitting in Google Drive. What I want to do is write a short application to convert each of these to native Google Document format (mime type "application/vnd.google-apps.document").

I want to do this using Java code and not using GAS code.

The approach I used was to query drive for the File object corresponding to the item I want to convert. Then I pull that file's content as a string. Then I create a new file of mime type "application/vnd.google-apps.document" and upload it with the HTML content. Not surprisingly it didn't work.

So then I tried a different approach: Upload the content as "text/html" but set the "convert" flag to "true". Well I didn't see any direct API to set the convert flag to true. So I tried:

File oBody = new File() ;
oBody.setTitle ( sTitle ) ;
oBody.setDescription ( sDescription ) ;
oBody.setMimeType ( sMimeType ) ;
oBody.set("convert", bConvert);

This did not fail. But it did not create a Google Document either. It just created a text file identical to the original file.

How do I upload a document containing "text/html" content and get Google Drive to convert it automatically to a Google Document?

1

1 Answers

1
votes

The convert flag has to be set in the files.insert request and not the File resource. Using the snippet in the files.insert documentation as reference, this is what you should do:

...
File file = service.files().insert(body, mediaContent).setConvert(true).execute();
...