Is it possible to automate a query to export to a google sheet?

You’d need to give the Google Script an API Key. You can restrict that API Key to only be allowed to run data explorer queries, and you can further restrict it to only be able to run specific queries. You are correct, Google Script (or anything external) cannot simply run a data explorer query.

Check out /admin/api/keys/new and the “Granular” scope level. Scroll down to see the data explorer scope. See docs.discourse.org for details on using the API.