Retrieving HTML File ID from Microsoft Docx File on Google Drive

This sample script converts from Microsoft Docx File on Google Drive to Google Spreadsheet, and converts to HTML file.

Drive APIs v2 and v3 are used for this. Please set as follows.

“Drive API v2” can be used at Google Apps Script by enabling Drive API of Advanced Google services and of Google API Console.

How to use it is as follows.

  1. In the script editor, select Resources > Advanced Google services

  2. In the dialog that appears, click the on/off switch for Drive API v2.

  3. At the bottom of the dialog, click the link for the Google API Console.

  4. In the console, click into the filter box and type part of the name of the API “Drive API”, then click the name once you see it.

  5. On the next screen, click Enable API. By this, Drive APIs v2 and v3 can be used.

  6. Close the Developers Console and return to the script editor. Click OK in the dialog. The advanced service you enabled is now available in autocomplete.

The detail information is https://developers.google.com/apps-script/guides/services/advanced.

Script

function doGet(e){
  var folderId = "### Folder ID ###";

  var file = DriveApp.getFileById(e.parameters.id);
  var docfileId = Drive.Files.insert(
    {
      "title": file.getName(),
      "mimeType": "application/vnd.google-apps.document",
      "parents":  [{"id": folderId}]
    },
    file.getBlob()
  ).id;
  var htmlId = DriveApp.getFolderById(folderId).createFile(
    UrlFetchApp.fetch(
      "https://www.googleapis.com/drive/v3/files/" + docfileId + "/export?mimeType=text/html",
      {
        headers : {Authorization: "Bearer " + ScriptApp.getOAuthToken()},
        muteHttpExceptions : true
      }
    ).getBlob()
  ).setName(file.getName() + ".html").getId();
  Drive.Files.remove(docfileId);
  return ContentService.createTextOutput(htmlId);
}

Check Script

In order to check above script, you can do it using curl as follows. When Docx File ID is sent to doGet() on Google, you can retrieve HTML file ID from Google.

curl -L https://script.google.com/macros/s/#####/exec?id=### Docx File ID ###

 Share!