When you run it in the console, does it ask for permission to run while testing? If so, then you may have to add some form of authorization to the script.
Base on the
Docs, you need at least "view" access on the sheet. Also, ensure you have the correct
scope to access Drive in your manifest file.
Make sure the manifest is visible:
(Project settings --> Show "appsscript.json" manifest file in editor)
In the manifest (appsscript.json file), you may see something like this:
More than likely, your manifest looks something more like this:
Don't worry about what is in my example, yours will be different. Just add the scope:
To ensure the script has been reauthorized, create a simple function to run, and debug it. It will prompt you to authorize/reauthorize the new scope.
function authorize() {
console.log("Hello. The script should prompt for authorization");