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:

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:

  1. In the Apps Script project, click Triggers.
  2. 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:

Library is missing

A library might be reported as missing if too many people access it simultaneously. To resolve this:

Error occurred due to a missing library version or a deployment version. Error code Not_Found

This error message indicates one of the following:

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 }

Index.html

Open Camera in New Window