Troubleshooting (original) (raw)
Even the most experienced developer rarely writes code correctly on the first try, making troubleshooting an important part of the development process. This section covers techniques to find, understand, and debug errors in your scripts.
Error messages
When your script encounters an error, an error message appears with a line number. There are two basic types of errors: syntax errors and runtime errors.
Syntax errors
Syntax errors occur when code doesn't follow JavaScript grammar and are detected when you save the script. For example, the following snippet contains a syntax error:
function emailDataRow(rowNumber) {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var rowData = data[rowNumber-1].join(" ";
MailApp.sendEmail('john@example.com',
'Data in row ' + rowNumber,
rowData);
}
The issue is a missing ) character at the end of line 4. When you save the script, the following error appears:
Missing ) after argument list. (line 4)
These errors are found immediately, making them straightforward to troubleshoot. Only valid code is saved into your project.
Runtime errors
Runtime errors occur when a function or class is used incorrectly and are detected when the script runs. For example, the following code causes a runtime error:
function emailDataRow(rowNumber) {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
var rowData = data[rowNumber-1].join(" ");
MailApp.sendEmail('john',
'Data in row ' + rowNumber,
rowData);
}
While the code is formatted correctly, "john" is an invalid email address. The following error is thrown:
Invalid email: john (line 5)
These errors are challenging because data is often pulled from external sources like spreadsheets or forms. Use debugging techniques to identify the cause.
Common errors
The following is a list of common errors and their causes.
Service invoked too many times:
This error indicates you exceeded your daily quota for an action, such as sending too many emails. Quotas vary by account type and are subject to change. View limits in theApps Script quota documentation.
Server not available. or Server error occurred, please try again.
Possible causes include:
- A Google server is temporarily unavailable. Wait and try again.
- An error in your script lacks a corresponding message. Try debugging to isolate the problem.
- A bug exists in Google Apps Script. Search for and file bug reports inBugs.
The script lacks the authorization needed to run. When a script runs from a trigger or as a service, an authorization dialog cannot be presented.
To authorize the script, open the script editor and run any function. If the script uses new unauthorized services, you must re-authorize it.
Triggers that fire before authorization or after expiration often cause this error. If an add-on causes this, use the add-on again to re-authorize. Remove problematic triggers:
- In the Apps Script project, click Triggers.
- Next to the trigger, click More > Delete trigger.
Alternatively, uninstall the add-on.
Other actions, such as a user selectively denying required scopes during authorization on a surface that supports granular permissions, can also cause these errors. If a script runs in the background (such as on an installable trigger) and tries to use a service that the user did not authorize, it fails immediately. See the authorization scopes page to learn how to protect trigger executions.
Access denied: DriveApp or The domain policy has disabled third-party Drive apps
Google Workspace administrators can disable theDrive API for their domain, which prevents users from using Drive apps or Apps Script add-ons that use theDrive service.
If an add-on or web app is published fordomain-wide installationand installed by an administrator, the script functions even if the Drive API is disabled.
The script does not have permission to get the active user's identity.
The active user's identity and email are unavailable. This results from calls to Session.getActiveUser()or Session.getEffectiveUser()in authorization modes other than AuthMode.FULL. If your script runs on a trigger, you can find the authorization mode in theauthMode property of the Apps Script event object.
Troubleshoot this based on the authorization mode:
- In AuthMode.FULL, consider usingSession.getEffectiveUser()instead.
- In AuthMode.LIMITED, ensure that the owner has authorized the script.
- In other authorization modes, avoid calling either method.
- If you are a Google Workspace customer newly experiencing this warning from aninstallable trigger, ensure that the trigger is running as a user within your organization.
Library is missing
A library might be reported as missing if too many people access it simultaneously. To resolve this:
- Copy the library's code directly into your script.
- Copy and deploy the library from your own account.
- If the library isn't required for your script to function, remove the library from your script project.
Error occurred due to a missing library version or a deployment version. Error code Not_Found
This error message indicates one of the following:
- The script version used by a deployment was deleted. To resolve this,edit the deploymentand select a different script version.
- A library version used by the script was deleted. To resolve this, in the script editor under "Libraries," find the library and update to a different version or remove the library. To update, click the version number and select a different version. To remove, click More> Remove.
- A library includes another library, and that library's version was deleted. To resolve this, contact the library's author or use a different version of the library that your script uses.
Error 400: invalid_scope when calling Google Chat API with the advanced service
If you encounter Error 400: invalid_scope with the error messageSome requested scopes cannot be shown, it means you haven't specified any authorization scopes in the Apps Script project's appsscript.json file. In most cases, Apps Script automatically determines what scopes a script needs, but when you use the Chat advanced service, you must manually add the authorization scopes that your script uses to your Apps Script project's manifest file. SeeSetting explicit scopes.
To resolve the error, add the appropriate authorization scopes to the Apps Script project's appsscript.json file as part of the oauthScopes array. For example, to call thespaces.messages.createmethod, add the following:
"oauthScopes": [
"https://www.googleapis.com/auth/chat.messages.create"
]
UrlFetch calls to are not permitted by your admin
Google Workspace administrators can use an allowlist to control external domain access. Contact your administrator to add the URL to the allowlist.
Permissions policy violation
This error occurs when an application usingHTMLService attempts to execute Web APIs that require sensitive permissions, such as navigator.mediaDevices.getUserMedia()for camera or microphone access. The Apps Script sandboxed environment restricts these features to protect user security.
Host the functionality that requires these permissions on a separate domain (outside of Apps Script) and open it in a new window or tab. You can then post the captured data or responses back to your Apps Script application as shown in this example.
Code.gs
function doGet(e) { return HtmlService.createHtmlOutputFromFile('Index') .setTitle('Media Devices Example'); } function processCameraData(data) { Logger.log('Received data from client-side: ' + data); // Process data as needed }