How to Batch Convert Office Files to Google Docs & Sheets (Without Getting Timed Out)

If you've ever migrated an organization from Microsoft Office to Google Workspace, you know the headache of dealing with hundreds of legacy .docx, .doc, .xlsx, and .xls files. Sure, Google Drive lets you open them, but you lose out on real-time collaboration features, Apps Script automation, and seamless cloud formatting until they are fully converted into native Google Docs and Sheets.
Doing this manually file-by-file is a nightmare.
In this article, I’ll share two powerful Google Apps Script solutions that will scan your entire Google Drive, find every single Microsoft Office file, and automatically convert them into native Google formats right in their original folders.
Best of all? Both scripts include a built-in checkpoint system so they won't crash if you hit Google’s strict 6-minute execution limit.
The Secret Sauce: Overcoming Google's 6-Minute Timeout
Google Apps Script limits standard account scripts to run for a maximum of 6 minutes per execution. If you have thousands of files, a normal script will crash halfway through, and running it again would cause duplicate conversions.
To solve this, these scripts use PropertiesService. Every time a file is successfully converted, its unique File ID is tagged in the script's memory. If the script nears the 5-minute mark, it safely pauses. When you run it again, it skips everything it has already processed and picks up exactly where it left off.
Part 1: Convert Microsoft Excel to Google Sheets
This script searches your entire Drive for .xlsx and .xls files, converts them to Google Sheets, drops the new file in the exact same parent folder, and tracks its progress.
function convertAllExcelToGoogleSheets() {
var startTime = new Date().getTime();
var scriptProperties = PropertiesService.getScriptProperties();
Logger.log("INITIALISING: Running Global Drive Search for all Excel files...");
// Finds every .xls and .xlsx file across your Google Drive
var query = "title contains '.xlsx' or title contains '.xls'";
var files = DriveApp.searchFiles(query);
var totalFound = 0;
try {
while (files.hasNext()) {
// 6-minute timeout safety check
var currentTime = new Date().getTime();
if (currentTime - startTime > 300000) {
throw new Error("TIMEOUT");
}
var excelFile = files.next();
var fileName = excelFile.getName();
var fileId = excelFile.getId();
// Verification: Make sure it truly ends with the extension
if (!fileName.toLowerCase().endsWith('.xlsx') && !fileName.toLowerCase().endsWith('.xls')) {
continue;
}
totalFound++;
Logger.log("FOUND FILE (" + totalFound + "): " + fileName);
// Skip if tracked in memory from a previous timeout run
if (scriptProperties.getProperty(fileId) === "converted_sheet") {
Logger.log("SKIPPED: " + fileName + " was already converted.");
continue;
}
// Get the exact parent folder where the original file lives
var parents = excelFile.getParents();
var parentId = parents.hasNext() ? parents.next().getId() : "root";
var blob = excelFile.getBlob();
// Google Drive API v3 Metadata Structure for Sheets
var resource = {
name: fileName.replace(/\.xlsx\(/i, "").replace(/\.xls\)/i, ""),
mimeType: MimeType.GOOGLE_SHEETS,
parents: [parentId] // Keeps it in the exact same folder
};
try {
// Requires Drive API service to be added via the left sidebar
Drive.Files.create(resource, blob);
Logger.log("SUCCESS: Converted " + fileName + " to Google Sheets.");
scriptProperties.setProperty(fileId, "converted_sheet");
} catch (e) {
Logger.log("FAILED: " + fileName + " | Reason: " + e.toString());
}
}
if (totalFound === 0) {
Logger.log("RESULT: Zero files ending in .xlsx or .xls were found anywhere in your Drive.");
} else {
Logger.log("ALL DONE: Processed " + totalFound + " Excel files total.");
scriptProperties.deleteAllProperties();
}
} catch (e) {
if (e.message === "TIMEOUT") {
Logger.log("TIMEOUT REACHED: Processed " + totalFound + " files so far. Progress saved! Please click 'Run' again to continue converting the rest.");
} else {
Logger.log("CRITICAL ERROR: " + e.toString());
}
}
}
Part 2: Convert Microsoft Word to Google Docs
This script follows the exact same logic but targets .docx and .doc files, transforming them into cloud-native Google Docs.
function convertAllWordToGoogleDocs() {
var startTime = new Date().getTime();
var scriptProperties = PropertiesService.getScriptProperties();
Logger.log("INITIALISING: Running Global Drive Search for all Word files...");
// Finds every .doc and .docx file across your Google Drive
var query = "title contains '.docx' or title contains '.doc'";
var files = DriveApp.searchFiles(query);
var totalFound = 0;
try {
while (files.hasNext()) {
// 6-minute timeout safety check
var currentTime = new Date().getTime();
if (currentTime - startTime > 300000) {
throw new Error("TIMEOUT");
}
var docxFile = files.next();
var fileName = docxFile.getName();
var fileId = docxFile.getId();
// Verification: Make sure it truly ends with the extension
if (!fileName.toLowerCase().endsWith('.docx') && !fileName.toLowerCase().endsWith('.doc')) {
continue;
}
totalFound++;
Logger.log("FOUND FILE (" + totalFound + "): " + fileName);
// Skip if tracked in memory from a previous timeout run
if (scriptProperties.getProperty(fileId) === "converted") {
Logger.log("SKIPPED: " + fileName + " was already converted.");
continue;
}
// Get the exact parent folder where the original file lives
var parents = docxFile.getParents();
var parentId = parents.hasNext() ? parents.next().getId() : "root";
var blob = docxFile.getBlob();
// Google Drive API v3 Metadata Structure
var resource = {
name: fileName.replace(/\.docx\(/i, "").replace(/\.doc\)/i, ""),
mimeType: MimeType.GOOGLE_DOCS,
parents: [parentId] // Keeps it in the exact same location
};
try {
// Updated syntax for Advanced Drive Service v3
Drive.Files.create(resource, blob);
Logger.log("SUCCESS: Converted " + fileName);
scriptProperties.setProperty(fileId, "converted");
} catch (e) {
Logger.log("FAILED: " + fileName + " | Reason: " + e.toString());
}
}
if (totalFound === 0) {
Logger.log("RESULT: Zero files ending in .docx or .doc were found anywhere in your Drive.");
} else {
Logger.log("ALL DONE: Processed " + totalFound + " files total.");
scriptProperties.deleteAllProperties();
}
} catch (e) {
if (e.message === "TIMEOUT") {
Logger.log("TIMEOUT REACHED: Processed " + totalFound + " files so far. Progress saved! Please click 'Run' again to continue converting the rest.");
} else {
Logger.log("CRITICAL ERROR: " + e.toString());
}
}
}
Step-by-Step Setup Guide
Because these scripts use the advanced Google Drive API v3, you need to enable the Drive Service in your Apps Script dashboard before running the code. Here is exactly how to do it:
Step 1: Open Google Apps Script
Go to script.google.com.
Click New Project in the top left corner.
Rename your project to something memorable, like Office File Converter.
Step 2: Enable the Advanced Drive Service (Crucial)
By default, Apps Script uses a simplified Drive service. Since we are changing file types natively via metadata, we need to activate the advanced Drive API.
On the left sidebar of the Apps Script editor, look for the Services section and click the
+(Add a service) button.Scroll through the list and select Drive API.
Keep the identifier as
Driveand ensure the version is set to v3.Click Add.
Step 3: Paste and Save the Code
Delete any default code in the
Code.gsfile.Copy and paste both functions provided above into the editor.
Click the Save disk icon at the top.
Step 4: Run and Authorize
Select the function you want to run (e.g.,
convertAllExcelToGoogleSheets) from the dropdown menu next to the "Debug" button.Click Run.
An "Authorization Required" prompt will pop up. Click Review Permissions, select your Google account, click Advanced (at the bottom), and then click Go to Office File Converter (unsafe) to grant the necessary Drive access.
Note: The "unsafe" warning simply means Google hasn't reviewed your private script. Since you wrote/pasted it yourself, it is okay to proceed.
Step 5: Check the Logs
Open the Execution Log at the bottom of the editor. You will see a real-time printout of every file found, skipped, or successfully converted. If the script reaches the 5-minute timeout mark, it will safely save your progress and tell you to click run again.
Wrapping Up
With these two scripts in your toolkit, you can migrate thousands of legacy files into Google Workspace seamlessly without lifting a finger or worrying about script crashes.
Have any questions about customizing the search query to target specific folders instead of your entire Drive? Let me know in the comments below!
Happy Coding! 🚀
If you found this article helpful, don't forget to give it a reaction and follow for more automation tips!


