Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created May 16, 2024 02:03
Show Gist options
  • Save tanaikech/3f03dac24179f750c013abc39703c4d2 to your computer and use it in GitHub Desktop.
Save tanaikech/3f03dac24179f750c013abc39703c4d2 to your computer and use it in GitHub Desktop.
Checking API Enabled with Advanced Google Services using Google Apps Script

Checking API Enabled with Advanced Google Services using Google Apps Script

Overview

This script checks if the desired API is enabled or disabled in the Advanced Google Services section of Google Apps Script.

Introduction

As of December 11, 2023, Drive API v3 became available for use in Advanced Google Services. Ref This means you can now choose between v2 and v3 in your scripts. However, when Drive API is enabled, version 3 is automatically selected. This caused compatibility issues with previously published libraries that relied on v2.

To address this issue and ensure compatibility, I've created a sample Google Apps Script to check the enabled Drive API version in Advanced Google Services. Also, the script can use all APIs of Advanced Google services. So, you can check if the desired API is enabled or disabled in the Advanced Google Services section of Google Apps Script. I will be including this script in my future library updates.

Main script

This is the main script. This function returns an object with "api" and "version" properties by taking the API name as input, such as "Drive" or "Sheet".

When "api" is set to "disable" for a given API name (e.g., "Drive"), it indicates that the Drive API has not been enabled. Conversely, if "api" is set to "enable" (again, for a specific API name like "Drive"), it indicates that the Drive API has already been enabled. In this case, the API version can be found in the "version" property.

Important: This script does not use any scopes. This allows you to confirm API enablement without requiring any permissions.

/**
 * ### Description
 * Check whether Drive API is enabled at Advanced Google services, and return it as true or false and the version.
 *
 * @param {String} apiName API name you want to check.
 * @returns {Object} Object including "api" and "version" properties.
 */
function isAPIAtAdvancedGoogleServices_(apiName) {
  if (!apiName || apiName == "" || typeof apiName != "string") {
    throw new Error("Please set a valid API name.");
  } else if (!/^[A-Z]+$/g.test(apiName[0])) {
    const [t, ...b] = apiName;
    apiName = [t.toUpperCase(), ...b].join("");
  }
  const obj = { apiName, api: "disable" };
  if (typeof this[apiName] !== "undefined") {
    obj.api = "enable";
    obj.version = this[apiName].getVersion();
  }
  return obj;
}

Testing

When you test this script, please do the following steps.

1. Create a Google Apps Script project

Please create a Google Apps Script project. You can use both the standalone script and the container-bound script.

2. Sample script

Please copy and paste both the above isAPIAtAdvancedGoogleServices_ function and the following function.

function sample() {
  const res1 = isAPIAtAdvancedGoogleServices_("Drive");
  const res2 = isAPIAtAdvancedGoogleServices_("Sheets");
  console.log(res1);
  console.log(res2);
}

When the function sample is run, res1 and res2 show { apiName: 'Drive', api: 'disable' } and { apiName: 'Sheets', api: 'disable' }, respectively. Because Drive API and Sheets API have not been enabled.

3. Enable Drive API

Please enable Drive API at Advanced Google services. Ref

Under this condition, when the function sample is run, res1 and res2 show { apiName: 'Drive', api: 'enable', version: 'v3' } and { apiName: 'Sheets', api: 'disable' }, respectively.

From this result, you can confirm that Drive API v3 is enabled. In the current stage, when Drive API is enabled, version 3 is automatically selected.

4. Enable Sheets API

Please enable Sheet API at Advanced Google services. Ref

Under this condition, when the function sample is run, res1 and res2 show { apiName: 'Drive', api: 'enable', version: 'v3' } and { apiName: 'Sheets', api: 'enable', version: 'v4' }, respectively.

From this result, you can confirm that both Drive API v3 and Sheets API v4 are enabled.

Applying to Google Apps Script Library

This document describes a scenario to illustrate how Google Apps Script library dependencies work.

1. Create two standalone Google Apps Script projects:

  • A library script named "lib".
  • A client script.

2. Library Script:

Copy and paste the following script into the library. This script uses the Drive API v2.

function sample() {
  return Drive.Files.insert({ title: "sample" });
}

And, deploy this script as a library.

3. Client Script and Error:

Copy and paste the following script into the client. This script uses the Drive API v2.

function myFunction() {
  lib.sample();
}

Running this script initially will result in a ReferenceError: DriveApp is not defined because the Drive API is not enabled for the client script.

4. Enable Drive API (v3) in Client Script:

Enable the Drive API in the client script's "Advanced Google services" settings. However, this will likely set the version to v3 by default. Running myFunction again will likely cause a TypeError: DriveApp.getFiles().create is not a function because the library script uses DriveApp (v2) and the client script has v3 enabled.

5. Enable Drive API (v2) in Library Script:

Enable Drive API v2 in the library script's "Advanced Google services" settings.

6. Run myFunction Again:

Now, running myFunction in the client script should work without errors. This is because the library script uses Drive API v2, which is now enabled for the library project.

7. Applying this method in this report

Here, in order to test this method, please disable Drive API at both the client script and the library script. And, replace the script in the library script as follows.

/**
 * ### Description
 * Check whether Drive API is enabled at Advanced Google services, and return it as true or false and the version.
 *
 * @param {String} apiName API name you want to check.
 * @returns {Object} Object including "api" and "version" properties.
 */
function isAPIAtAdvancedGoogleServices_(apiName) {
  if (!apiName || apiName == "" || typeof apiName != "string") {
    throw new Error("Please set a valid API name.");
  } else if (!/^[A-Z]+$/g.test(apiName[0])) {
    const [t, ...b] = apiName;
    apiName = [t.toUpperCase(), ...b].join("");
  }
  const obj = { apiName, api: "disable" };
  if (typeof this[apiName] !== "undefined") {
    obj.api = "enable";
    obj.version = this[apiName].getVersion();
  }
  return obj;
}

function sample() {
  const checkAPI = isAPIAtAdvancedGoogleServices_("Drive");
  if (
    checkAPI.api == "disable" ||
    (checkAPI.api == "enable" && checkAPI.version == "v3")
  ) {
    throw new Error("Please enable Drive API v2 at Advanced Google services.");
  }
  return Drive.Files.insert({ title: "sample" });
}

By this modification, when the function myFunction on the client script is run, it occurred a custom error like Please enable Drive API v2 at Advanced Google services..

Key Point:

As a key point, in Google Apps Script, libraries are deployed as separate projects. The enabled APIs for a script depend on the project where the script resides, not the script calling it. This means that even if the Drive API v3 is enabled in the client script, the library script will continue to use DriveApp (v2) as long as v2 is enabled for the library project. Also, even when Drive API is disabled on the client script, the function myFunction works fine when Drive API v2 is enabled in the library script. Namely, it was found that when the library is used, the APIs at Advanced Google services can be managed at the library side.

Note

  • The top abstract image was created by giving this report to Gemini.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment