9

When I try to use Google drive reference it always shows ReferenceError: "Drive" is not defined. (line 16, file "Code").

Sour LeangChhean
  • 7,089
  • 6
  • 37
  • 39

2 Answers2

20

I've solves this error by go to:

Resources -> Advanced Google Services -> Enable Drive Api

enter image description here

Note: you need to Enable Drive Api in Google Console

Sour LeangChhean
  • 7,089
  • 6
  • 37
  • 39
0

In addition to Sour's answer:

If you have multiple libraries, it seems that you have to enable the Drive API in every Google Apps Script Project (not to be confused with a Google Cloud Project) that might use the Drive API, even if the actual call to the API occurs in a different Apps Script Project.

For example, say you have two Apps Script Projects, Util and DoStuffWithSheets. Util contains a function with a call to the Drive API. DoStuffWithSheets imports Util (by going to Resources -> Libraries). DoStuffWithSheets has a function that is triggered by an edit to a Sheet or whatever, and it calls the Util function that uses the Drive API.

It is NOT sufficient for the Drive API to be enabled in Util; it has to be enabled in DoStuffWithSheets.

This goes for Google Apps Script libraries which belong to the same Google Cloud Project in which the Drive API is also enabled. I haven't tested it for libraries which belong to different GC Projects.

Scofflaw
  • 152
  • 2
  • 10