Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

60%
+1 −0
Q&A How to solve the "ReferenceError: service is not defined"?

Meaning If the script is using any of the advanced services, they must be enabled first. If the service is not enabled, it will not be added to the global scope (as a value of a global variable), ...

posted 10mo ago by Oleg Valter‭

Answer
#1: Initial revision by user avatar Oleg Valter‭ · 2023-08-02T00:13:08Z (10 months ago)
**Meaning**

If the script is using any of the [advanced services](https://developers.google.com/apps-script/guides/services/advanced), they must be enabled first. If the service is not enabled, it will not be added to the global scope (as a value of a global variable), resulting in an instance of `ReferenceError` being thrown (as the script is referencing a non-existent variable). 

**Reproducing**

To get a reference error when calling a method on an advanced service, declare and run the following function in a new script or one that does not have the [Advanced Drive Service](https://developers.google.com/apps-script/advanced/drive) enabled:

```typescript
const driveServiceIsNotDefined = () => {
  try {
    Drive.Files.list({ q: "" })
  } catch(e) {
    console.log(e)
  }
}
```

**Fixing**

If you are using Google's online IDE, since the overhaul of it, simply open the "Editor" tab from the left sidebar, click on the "Add a service" button under the "Services" section, and then choose the appropriate service.

Note that the modal allows you to override the _identifier_ of the service to a name of your liking (the default name matches how Google's documentation references the service). If you change it, the script will have to reference that identifier instead of the service name.

> Historical aside: in the old IDE, one needed to go to "Resources -> Advanced Google Services" menu and enable the referenced service there (the identifier could be overridden too).

If you are developing locally and using [CLASP](https://github.com/google/clasp) to push the source code to the project, you will have to explicitly specify the advanced services used in the [manifest file](https://developers.google.com/apps-script/concepts/manifests). In fact, this is what is done under the hood when you enable the service in the GUI.

In the manifest, advanced services are specified under the `dependencies` field in the `enabledAdvancedServices` (which is a JSON array of service definitions), for example (enabling the Advanced Drive Service):

```typescript
"dependencies": {
    "enabledAdvancedServices": [
      {
        "userSymbol": "Drive", // identifier to reference
        "version": "v2", // version of the service to use
        "serviceId": "drive" // Google's service identifier
      }
    ]
}
```