Hey everyone, I’m trying to set up a one-click solution to generate PDFs from my Google Sheet and save them to Drive. I’ve added an icon with a script, but I’m running into issues.
When I click the icon, I get this error:
Exception: Request failed for docs.google.com returned code 401. Truncated server response: body{height:100%;margin:0;width:100%}@media (max-height:350px){.button{font-size:10px}.button-...
I’ve encountered similar issues before. The problem might be related to authorization scopes. Make sure you’ve included the necessary scopes in your project settings. Go to ‘Resources’ > ‘Cloud Platform project’ and add ‘https://www.googleapis.com/auth/drive’ and ‘https://www.googleapis.com/auth/spreadsheets’. Also, try using DriveApp.createFile(pdfBlob) instead of the URL fetch method. This approach tends to be more reliable for creating and saving PDFs directly to Drive. If you’re still facing issues, double-check your script’s execution settings and ensure you’re running it from the correct Google account with appropriate permissions.
I’ve dealt with this exact problem before, and it can be frustrating. The 401 error suggests an authentication issue. One thing that worked for me was to use the SpreadsheetApp.getActiveSpreadsheet().getUrl() method instead of constructing the URL manually. This approach seems to handle authentication better.
Here’s a modified version of your script that might help: