Error Handling in Apps Script PDF Conversion
A conversion script without error handling fails silently. A document throws an error, the script stops, and you have no idea which files were processed. Good error handling logs failures, keeps the batch running, and notifies you when something needs attention.
Wrapping conversions in try-catch
Wrap each individual file conversion in its own try-catch block. On error, log the file name and error message with Logger.log(), push the file to an errors array, and continue to the next file. This way one bad document does not stop the rest of the batch.
Logging to a Google Sheet
Logger.log() output disappears after the execution ends. For persistent logs, write status rows to a Google Sheet using SpreadsheetApp. Each row records the timestamp, file name, success or failure, and the error message if applicable. This gives you a running audit trail of every conversion.
Sending a failure summary email
At the end of the batch function, check whether the errors array has any entries. If it does, send an email with the list of failed files using MailApp.sendEmail(). Include the file ID so you can find and investigate the problematic document quickly.
Frequently Asked Questions
What are the most common errors in Apps Script PDF conversion?
Common errors include insufficient permissions on a file, rate limit exceeded from converting too many files too quickly, and invalid file IDs. Check the error message text to identify which category applies.
How do I retry failed conversions?
Store the IDs of failed files in a PropertiesService property or a Sheet row marked as failed. Add a retry function that reads only the failed IDs and attempts conversion again, with a longer delay between attempts using Utilities.sleep().
Related article
Set up a workflow to convert Google Docs to PDF →Ready to Try It?
Install the free Chrome extension and start converting your Google Docs to PDF in one click.
Install Free Extension