Create a New MS Word File in Google Drive
Posted By: Harish

We have seen a way to create a excel file and a comma separated values(CSV) file in google drive directly.
In this post, we will learn a way to create a Microsoft Word file with data in a google drive folder using apps script.
Open Google Apps Script editor and create a new project.
Replace the default function (which already exists) with the below function and run it. Visit creating and executing a Google Apps Script post for more info.
function createMSDocFileinGoogleDrive() {
try {
let folder;
const folderName = "Docs Folder"; //<-- change folder name of your choice
const docsFolder = DriveApp.getFoldersByName(folderName);
const isFolder = docsFolder.hasNext();
if (!isFolder) {
folder = DriveApp.createFolder(folderName);
} else {
folder = docsFolder.next();
};
const createdFile = folder.createFile(
"productiveGuser Docx File",
"Add Microsoft word content here", //<-- add Doc content here
MimeType.MICROSOFT_EXCEL //MimeType.MICROSOFT_WORD_LEGACY
);
const url = createdFile.getUrl()
Logger.log(url)
} catch (err) {
Logger.log(err)
}
}
By running the above function, a new Microsoft Word(.docx) file will be created inside the folder.
If you want an old Word doc extension i.e, .doc
format, use MimeType.MICROSOFT_WORD_LEGACY
.
Above function can be used to create a simple excel file with less data.
After successful execution of the above function, the url of the newly created MS Doc file will be printed out in the execution log window. Use that link to directly open the file.