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?