What is the process for transforming .docx files into Google Docs using Apps Script? (2024 - Drive API V3)

I attempted to follow a guide on converting .docx files to Google Docs and encountered an error when using the Drive API. Initially, I received this message:

TypeError: Drive.Files.insert is not a function

I noticed that the ‘insert’ method isn’t explained in the Drive API v3 documentation, so I switched to ‘Drive.Files.create’, but then received a different error:

Exception: The document is inaccessible. Please try again later

Here’s a portion of my original code:

var wordFile = DriveApp.getFilesByName(fileName).next();
var newGoogleDoc = Drive.newFile();
var fileBlob = wordFile.getBlob();
var createdFile = Drive.Files.insert(newGoogleDoc, fileBlob, {convert: true});

DocumentApp.openById(createdFile.id).setName(wordFile.getName());

I faced the same ‘insert’ error on this line:

var createdFile = Drive.Files.insert(newGoogleDoc, fileBlob, {convert: true});

When I tried replacing ‘insert’ with ‘create’, I encountered a new error:

Exception: The document is inaccessible. Please try again later

This error occurred at:

DocumentApp.openById(createdFile.id).setName(wordFile.getName());

I’m looking for advice on how to resolve these issues.

Here’s the entire code snippet I’m working with:

function UpdateFileLinks() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var fileListSheet = spreadsheet.getSheetByName("File List");
  var linkRange = fileListSheet.getRange('K1:M').getValues();
  for (var j = 0; j < linkRange.length; j++){
    if (linkRange[j][0] !== "" && linkRange[j][2] === ""){
      var filePath = linkRange[j][0];
      var fileName = filePath.toString().replace("File List_Files_/'',"");
      var wordFileId = DriveApp.getFilesByName(fileName).next().getId();
      Logger.log(wordFileId);

      var wordFile = DriveApp.getFilesByName(fileName).next();
      var newGoogleDoc = Drive.newFile();
      var fileBlob = wordFile.getBlob();
      var createdFile = Drive.Files.create(newGoogleDoc, fileBlob, {convert: true});

      DocumentApp.openById(createdFile.id).setName(wordFile.getName());

      Logger.log("New file created: " + createdFile.id);
    }
  }
}

The goal of my Apps Script is to upload Word documents to Google Drive through my app, and then create Google Docs based on these Word files, with plans to log the links in a spreadsheet. I’ve also discovered that the ‘convert:true’ option is not available in the V3 documentation. Attempting to use a provided solution that utilizes V2 still keeps the documents in Word format. I’ve also faced persistent conversion errors using:

.getAs('application/vnd.google-apps.document')

which results in:

Exception: Converting from application/vnd.openxmlformats-officedocument.wordprocessingml.document to application/vnd.google-apps.document is not supported.

What advice do you have for fixing these conversion issues?

It sounds like you’re running into some common misunderstandings with the Drive API and file conversion in Apps Script. One thing you might look into is using the advanced Drive service rather than the built-in DriveApp. The advanced service will give you access to more capabilities including file conversion. Here’s a potential solution that can help guide you through the process:

  1. Enable the Advanced Drive Service: Ensure that you’ve enabled the advanced Google Drive service in your script settings. You do this in the “Resources → Advanced Google services” menu.

  2. Correct API Usage: Instead of using Drive.Files.create() directly, you would use Drive.Files.copy(). This method allows you to specify that you want the file converted by setting the convert parameter to true as part of the request body when copying:

    var resource = {
      title: fileBlob.getName(),
      mimeType: "application/vnd.google-apps.document"
    };
    var copiedFile = Drive.Files.copy(resource, wordFileId);
    
  3. Error Handling: The ‘document is inaccessible’ error might be due to permissions or network issues. Make sure your script has the necessary authorization to access all the files, and that any operations dependent on conversion aren’t too time-sensitive.

  4. MIME Types: It’s crucial to match up the MIME types correctly. Sometimes inappropriate MIME types can cause conversion to fail, so double-check the conversion documentation to ensure they match what’s supported.

Try these steps and see if this resolves your issues with converting .docx files to Google Docs using Apps Script.