Created
August 25, 2026 09:45
-
-
Save phillypb/00f5be8e0bc6f1ef18a63c8079bf75f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Define the exact scopes your project uses. | |
| * Replace these with the scopes from your appsscript.json file or using the Project Details view. | |
| */ | |
| // add scopes from appsscript.json | project details, below | |
| var REQUIRED_SCOPES = [ | |
| "https://www.googleapis.com/auth/spreadsheets", | |
| "https://www.googleapis.com/auth/script.container.ui" | |
| // Add others as needed, e.g., "https://www.googleapis.com/auth/drive" | |
| ]; | |
| function verifyPermissions() { | |
| var authInfo = ScriptApp.getAuthorizationInfo(ScriptApp.AuthMode.FULL); | |
| var grantedScopes = authInfo.getAuthorizedScopes() || []; | |
| // filter out the scopes that are required but missing from the granted list | |
| var missingScopes = REQUIRED_SCOPES.filter(scope => !grantedScopes.includes(scope)); | |
| // if nothing is missing, return true to let the script run | |
| if (missingScopes.length == 0) { | |
| return true; | |
| } else { | |
| // if scopes are missing, generate the fix URL | |
| var authUrl = authInfo.getAuthorizationUrl(); | |
| }; | |
| try { | |
| // attempt to pop-up a clean UI dialogue with a clickable authorisation link | |
| var htmlOutput = HtmlService.createHtmlOutput( | |
| `<div style="font-family: sans-serif; padding: 10px;"> | |
| <p>This tool requires additional permissions to function correctly. You previously granted partial access.</p> | |
| <br> | |
| <a href="${authUrl}" target="_blank" style="background: #1a73e8; color: white; padding: 10px 15px; text-decoration: none; border-radius: 4px; display: inline-block;">Click here to grant access</a> | |
| </div>` | |
| ).setWidth(350).setHeight(190); | |
| SpreadsheetApp.getUi().showModalDialog(htmlOutput, "Missing Permissions"); | |
| } catch (e) { | |
| // the CATCH-22: if the user explicitly denied the UI or Spreadsheet scopes, SpreadsheetApp.getUi() will fail. We fall back to a standard error. | |
| throw new Error("Action required! Please copy and paste this URL into your browser to grant the missing permissions: " + authUrl); | |
| } | |
| return false; // Stop the script execution | |
| }; | |
| /** | |
| * Main Function that performs the initial permissions check. | |
| * Then runs the rest of your code as normal. | |
| */ | |
| function mainFunction() { | |
| // 1. Run the check before anything else | |
| if (!verifyPermissions()) return; | |
| // 2. Your actual code then goes below for the rest of the functionality | |
| SpreadsheetApp.getActiveSpreadsheet().toast("All permissions granted! Running script..."); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment