Apps Script onChange Handler: Auto-Export to PDF

The onChange event handler is the core of event-driven PDF automation in Apps Script. This page covers how to write a robust handler that converts the right files at the right time.

Handler function signature

The handler receives a DriveEvent object: function onDriveChange(e). The event has e.changeType and e.fileId. Start by logging both values during development to understand what your trigger receives and when.

Getting the file and checking its type

Use DriveApp.getFileById(e.fileId) to get the file. Check file.getMimeType() === MimeType.GOOGLE_DOCS before attempting to export. This prevents errors when the trigger fires for Sheets, Slides, or uploaded files in the same Drive.

Running the export

Call your PDF export function with e.fileId. Wrap it in a try-catch and log any errors. The onChange handler must complete quickly because trigger functions have a time limit. If the export takes too long, move the actual conversion to a separate function triggered by a short delay.

Frequently Asked Questions

Can the onChange handler access who made the change?

Not directly from the event object. The trigger runs under the account that installed it, not the account that made the change. To identify the editor, use the Drive API to fetch the file's revision history.

Ready to Try It?

Install the free Chrome extension and start converting your Google Docs to PDF in one click.

Install Free Extension