Updated: January 22, 2023
This sample script is for the interconversion between Google Docs (document, spreadsheet and presentation) and Microsoft Docs (word, excel and powerpoint). The feature is to convert them without Advanced Google Services.
Since Advanced Google Services is not used for this, if you publish your script with this script, you are not necessary to explain how to install Advanced Google Services. This script converts between Google Docs and Microsoft Docs using UrlFetchApp.fetch()
. Although Drive API v3 is used, Drive API is automatically enabled by the recent update on Google. Ref (I’m sorry. This is Japanese language.) So you are not necessary to explain about the use of Drive API. By this, users will be easy to use the scripts that Drive API is used. This is very important for a lot of users.
Script
In order to use both script, please give file ID to each method. Each method creates the converted file and return the created file ID.
If you want to give the file ID from the filename, please use following sample.
var filename = "sample";
var fileId = DriveApp.getFilesByName(filename).next().getId();
From Google Docs to Microsoft Docs
function convToMicrosoft(fileId) {
if (fileId == null) throw new Error("No file ID.");
var file = DriveApp.getFileById(fileId);
var mime = file.getMimeType();
var format = "";
var ext = "";
switch (mime) {
case "application/vnd.google-apps.document":
format = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
ext = ".docx";
break;
case "application/vnd.google-apps.spreadsheet":
format = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
ext = ".xlsx";
break;
case "application/vnd.google-apps.presentation":
format = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
ext = ".pptx";
break;
default:
return null;
}
var url = "https://www.googleapis.com/drive/v3/files/" + fileId + "/export?mimeType=" + format;
var blob = UrlFetchApp.fetch(url, {
method: "get",
headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true
}).getBlob();
var filename = file.getName();
var id = DriveApp.createFile(blob).setName(~filename.indexOf(ext) ? filename : filename + ext).getId();
return id;
};
From Microsoft Docs to Google Docs
Here, Multipart-POST Request Using Google Apps Script was used for using Drive File Create.
function convToGoogle(fileId) {
if (fileId == null) throw new Error("No file ID.");
var file = DriveApp.getFileById(fileId);
var filename = file.getName();
var mime = file.getMimeType();
var ToMime = "";
switch (mime) {
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
ToMime = "application/vnd.google-apps.document";
break;
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
ToMime = "application/vnd.google-apps.spreadsheet";
break;
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
ToMime = "application/vnd.google-apps.presentation";
break;
default:
return null;
}
var metadata = {
name: filename,
mimeType: ToMime
};
var fields = "id,mimeType,name";
var url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=" + encodeURIComponent(fields);
var boundary = "xxxxxxxxxx";
var data = "--" + boundary + "\r\n";
data += "Content-Type: application/json; charset=UTF-8\r\n\r\n";
data += JSON.stringify(metadata) + "\r\n";
data += "--" + boundary + "\r\n";
data += "Content-Type: " + mime + "\r\n\r\n";
var payload = Utilities.newBlob(data).getBytes().concat(file.getBlob().getBytes()).concat(Utilities.newBlob("\r\n--" + boundary + "--").getBytes());
var res = UrlFetchApp.fetch(url, {
method: "post",
headers: {
"Authorization": "Bearer " + ScriptApp.getOAuthToken(),
"Content-Type": "multipart/related; boundary=" + boundary
},
payload: payload,
muteHttpExceptions: true
}).getContentText();
var id = JSON.parse(res).id;
return id;
};