appscript.dev
Automation Intermediate Drive

Auto-convert uploaded Office files to Google formats

Normalise .docx and .xlsx files dropped in Northwind's inbox into Docs and Sheets.

Published Jul 28, 2025

Northwind shares a single Drive folder with clients and freelancers, and a fair share of what lands in it is Microsoft Office files — .docx briefs, .xlsx budgets, the odd .pptx. They open fine in Drive, but every edit prompts a clunky “convert a copy” dance, comments do not thread properly, and the team ends up with two versions of the same document.

This script watches an inbox folder, converts any Word or Excel file it finds into the matching Google format, drops the converted copy into a tidy folder, and bins the original. The result is one canonical, fully editable Google file per upload — no manual conversion, no duplicate clutter.

What you’ll need

  • An inbox Drive folder where Office files get dropped, and a separate converted folder for the Google-format results. You need both folder IDs.
  • The Drive API enabled in Advanced Services. In the Apps Script editor, open Services, add Drive API, and keep the identifier as Drive. The built-in DriveApp cannot convert file formats on its own — the advanced service handles the conversion when you create the file.

The script

// Folder where clients and freelancers drop Office files.
const INBOX = '1abcInboxId';

// Folder where the converted Google-format copies land.
const CONVERTED = '1abcConvertedId';

// Office MIME type -> the Google format it should become.
const CONVERSIONS = {
  [MimeType.MICROSOFT_WORD]: MimeType.GOOGLE_DOCS,
  [MimeType.MICROSOFT_EXCEL]: MimeType.GOOGLE_SHEETS,
};

/**
 * Scans the inbox folder, converts every Word and Excel file into the
 * matching Google format, files the copy in the converted folder, and
 * trashes the original.
 */
function convertOfficeFiles() {
  const files = DriveApp.getFolderById(INBOX).getFiles();
  let converted = 0;

  // 1. Walk every file currently sitting in the inbox folder.
  while (files.hasNext()) {
    const file = files.next();

    // 2. Look up the target format. Skip anything that is not a
    //    convertible Office file (PDFs, images, already-Google files).
    const targetMime = CONVERSIONS[file.getMimeType()];
    if (!targetMime) continue;

    // 3. Build the metadata for the new file: strip the .docx/.xlsx
    //    extension from the name and place it in the converted folder.
    const meta = {
      name: file.getName().replace(/\.[^.]+$/, ''),
      mimeType: targetMime,
      parents: [CONVERTED],
    };

    // 4. Creating the file with a target mimeType triggers Drive's
    //    server-side conversion of the uploaded blob.
    Drive.Files.create(meta, file.getBlob());

    // 5. Bin the original so the inbox does not pile up with duplicates.
    file.setTrashed(true);
    converted++;
  }

  Logger.log('Converted ' + converted + ' Office file(s).');
}

How it works

  1. convertOfficeFiles opens the inbox folder by ID and gets an iterator over every file inside it.
  2. For each file it reads the MIME type and looks it up in the CONVERSIONS map. A Word file maps to Google Docs, an Excel file maps to Google Sheets, and anything else returns undefined — so the loop continues past PDFs, images and files that are already in Google format.
  3. It builds a metadata object for the new file: the original name with the extension stripped off, the target Google MIME type, and the converted folder as the parent.
  4. Drive.Files.create does the real work. Passing a target mimeType alongside the file blob tells Drive to convert the upload server-side as it stores it — this is the step DriveApp alone cannot do.
  5. With the converted copy safely filed, the original Office file is moved to the trash so the inbox stays empty and ready for the next drop.
  6. A count is logged so you can confirm the run did something.

Example run

Before the run, the inbox folder holds a mix of uploads:

FileType
Q3-brief.docxMicrosoft Word
media-budget.xlsxMicrosoft Excel
logo.pngPNG image

After the run, the inbox is empty (the image is left alone) and the converted folder holds:

FileType
Q3-briefGoogle Doc
media-budgetGoogle Sheet

The logo.png stays in the inbox untouched, because it has no entry in the CONVERSIONS map.

Trigger it

A time-based trigger keeps the inbox draining without anyone thinking about it:

  1. In the Apps Script editor open Triggers (the clock icon).
  2. Add a trigger for convertOfficeFiles, Time-driven, Minutes timer, every 15 minutes.

Fifteen minutes is a reasonable balance — fast enough that a freshly dropped brief is editable before the next coffee, slow enough to stay well inside quota.

Watch out for

  • The original is trashed, not deleted forever. It sits in Drive’s bin for 30 days, which is a useful safety net — but if you would rather keep the Office copy, replace file.setTrashed(true) with a move to an archive/ folder.
  • Only Word and Excel are mapped. PowerPoint converts to Google Slides too (MimeType.MICROSOFT_POWERPOINT to MimeType.GOOGLE_SLIDES) — add a line to CONVERSIONS if Northwind receives .pptx files.
  • Conversion is not pixel-perfect. Complex Word layouts, macros and intricate Excel formulas can shift or drop on the way into Google formats. For high-stakes documents, spot-check the converted copy.
  • Large or numerous files can push the run past the six-minute execution limit. If the inbox regularly holds dozens of files, process a fixed batch per run and let the next trigger pick up the rest.
  • A file dropped mid-run may be picked up on the next pass instead — harmless, but worth knowing if someone reports a “missing” conversion that appears a quarter of an hour later.

Related