0

I'm looking for a way to programmatically export a Google Document, to be able to re-import it later, exactly as it was in the first place.

DriveApp.getFileById(file.id).makeCopy(filename) won't do the job since I need to send the document to an external server between export and re-import.

const file = DriveApp.getFileById(item.id);
const blob = file.getBlob(); // <- This line seems to convert the doc to pdf
const blobStr = blob.getDataAsString();

// Then later...
const newBlob = Utilities.newBlob(blobStr, blob.getContentType(), `COPY OF ${item.title}`);
const newFile = DriveApp.createFile(newBlob); // Create a new file from a blob

My problem here is that file.getBlob() converts the document to pdf. Which I don't want. I need it to still be a Google Document when I create the file.

Am I missing something ? maybe Google documents can't be represented as a string or byte at all...?

To give a little bit of context, I'm sending the document for translation to an external service. When translated strings come back, I need to map them back into the original document keeping the formatting and everything exactly as it was.

Serhii Rohoza
  • 4,287
  • 2
  • 16
  • 29
gkpo
  • 2,623
  • 2
  • 28
  • 47
  • This line suggests that it was a pdf to begin with `const newBlob = Utilities.newBlob(blobStr, blob.getContentType(), `COPY OF ${item.title}`);` Try using '' for the content type. – Cooper Apr 16 '21 at 16:00
  • Thanks for your reply. Same results with empty string `''` for content type. Actually it's not a pdf to begin with. It's a Google Document that I created myself. – gkpo Apr 16 '21 at 17:26
  • The pdf that it creates for me has something wrong with it and can't be opened. – Cooper Apr 16 '21 at 17:40
  • I found this question: https://stackoverflow.com/questions/36274597/getblob-is-converting-files-to-pdf – Cooper Apr 16 '21 at 17:50
  • Sorry I can't help. I'll be following the question. Perhaps another volunteer will have better answer. – Cooper Apr 16 '21 at 17:53
  • Unfortunately, I cannot understand about your goal from `I'm looking for a way to programmatically export a Google Document, to be able to re-import it later, exactly as it was in the first place.`. I apologize for my poor English skill. In order to correctly understand your goal, can you provide the sample input and output situation as the images? – Tanaike Apr 17 '21 at 00:51

0 Answers0