迁移至V8运行时后Slides App调用getActivePresentation报错
Fix "Exception: Action not allowed" in V8 Runtime for Cross-Account Slides Scripts
Let's break down why this is happening and how to fix it—this is a common gotcha when switching to V8 from the old ES5 runtime, especially with non-container-bound scripts and cross-account scenarios.
Why This Error Occurs
The V8 runtime enforces stricter permission boundaries than the deprecated ES5 mode. Here's the core issue:
- Non-container-bound scripts run under the authorization context of the Google account they're tied to, not the account that has the Slides presentation open.
- When you use
SlidesApp.getActivePresentation()(or evenopenById()with a presentation owned by a different account), V8 blocks the action because the script's account doesn't have explicit permission to access that presentation—even if you can see it in your browser. - In ES5, this permission check was more lenient, which is why it worked before.
Solution 1: Migrate to a Container-Bound Script (Simplest Fix)
This is the most reliable approach since container-bound scripts inherit the permission context of the presentation they're attached to:
- Open the Google Slides presentation you're working with.
- Go to Extensions > Apps Script to create a new container-bound script tied directly to this presentation.
- Copy your existing
imageToSlidefunction into this new script. - Update your HTML template's
google.script.runcall to point to this bound script's function. NowSlidesApp.getActivePresentation()will work seamlessly, even across accounts, as long as the user has edit access to the presentation.
Solution 2: Fix Permissions for Non-Container-Bound Scripts
If you can't switch to a container-bound script, you'll need to explicitly grant permissions and adjust your code:
Step 1: Configure Correct OAuth Scopes
- In your Apps Script editor, go to Project Settings > Show "appsscript.json" manifest file.
- Open
appsscript.jsonand make sure youroauthScopesinclude the necessary permissions for Slides access:"oauthScopes": [ "https://www.googleapis.com/auth/presentations", "https://www.googleapis.com/auth/script.container.ui" ]
Step 2: Grant the Script's Account Access to the Presentation
- Share the target Slides presentation with the Google account that owns your non-container-bound script, and give it Edit permissions. This ensures the script's account can access the presentation programmatically.
Step 3: Pass the Presentation ID Explicitly
- From your HTML template, extract the presentation ID from the Slides URL (it's the string between
/d/and/edit). - Modify your
google.script.runcall to pass this ID as a parameter:// HTML side: Extract ID from the current Slides URL const presentationId = window.location.href.split('/d/')[1].split('/')[0]; google.script.run .withSuccessHandler(sidebar.onSuccess) .withFailureHandler(sidebar.onAddDocumentError) .imageToSlide(presentationId, url, link, width, height); - Update your
imageToSlidefunction to useopenById()with the passed ID:function imageToSlide(presentationId, url, link, width, height) { try { const presentation = SlidesApp.openById(presentationId); // Your existing logic to add the image goes here return "Image added successfully"; } catch (err) { console.error("Error accessing presentation:", err); throw err; } }
内容的提问来源于stack exchange,提问作者Yanko Shterev
相关产品推荐
相关产品推荐

