Fernando Falcao
Fernando Falcao

Reputation: 39

Is it possible to use Google Apps Script to save a doc as epub?

I know that it is possible to convert a Google Doc file to pdf using Google Apps Script.

Is there a similar syntax like the one below that works for epub?

docblob = DocumentApp.getActiveDocument().getAs('application/pdf');

Upvotes: 0

Views: 261

Answers (1)

Amit Agarwal
Amit Agarwal

Reputation: 11268

This code will convert your document and save it in your Drive root folder.

You have to enable Drive API using Service button.

  1. Open the Apps Script project.
  2. At the left, click Editor code.
  3. At the left, next to Services, click Add a service add.
  4. Select Drive API and click Add.

Enable advanced services Google Doc

function createEPubFile(fileId, filename, destination) {
  var mimeType = 'application/epub+zip';
  var exportLink = Drive.Files.get(fileId).exportLinks[mimeType];
  const params = { headers: { authorization: `Bearer ${ScriptApp.getOAuthToken()}` } };
  const blob = UrlFetchApp.fetch(exportLink, params).getBlob().setName(`${filename}.epub`);
  const createdFileId = destination.createFile(blob).getId();
}

Upvotes: 4

Related Questions