Full-text search of Google Apps Script Projects using Google Apps Script

Gists

These are sample scripts for achieving the full-text search of Google Apps Script projects using Google Apps Script. I have the case that I want to search a value from Google Apps Script projects using a Google Apps Script. In this post, I would like to introduce the sample scripts for achieving the full-text search of Google Apps Script projects.

1. Full-text search from all Google Apps Script Projects of standalone type in Google Drive

Before you use this script, please enable Drive API at Advanced Google services. In the case of the Google Apps Script projects of the standalone type, the full-text search can be achieved using Drive API as follows.

function sample1() {
  const searchText = "###"; // Please set the search text.

  const { items } = Drive.Files.list({
    q: `fullText contains '"${searchText}"' and mimeType='application/vnd.google-apps.script' and trashed=false`,
    fields: "items(selfLink,title)",
    maxResults: 1000,
  });
  if (!items || items.length == 0) return;
  const res = items.map(({ id, title }) => ({
    url: `https://script.google.com/home/projects/${id}/edit`,
    name: title,
  }));
  console.log(res);
}

When this script is run, Google Apps Script projects are searched using a value of searchText. In this sample script, the filename and the file URL are returned.

Unfortunately, in the current stage, the Google Apps Script projects of the container-bound type cannot still be retrieved using Drive API and Apps Script API. This has already been reported as a future request on the Google issue tracker on Jul 5, 2018. Ref By this, the above sample script can be used for only the Google Apps Script projects of the standalone type.

2. Full-text search from a Google Apps Script Project

In the current stage, we can achieve the full-text search from a Google Apps Script project of both the standalone type and the container-bound type. The sample script is as follows.

2-1. For only standalone type

In this sample script, only a standalone type can be used. Please be careful about this. The setting for using this script is easy.

Before you use this script, please enable Drive API at Advanced Google services. And also, please set the variables of scriptId and searchText. When this script is run, the value of searchText is searched from a standalone script of scriptId. When the value is found, the script file names in the Google Apps Script project are returned.

// Retrieve scripts, parse it and search the text.
function search_(url, searchText) {
  const reg = new RegExp(searchText, "g");
  const res = UrlFetchApp.fetch(url, {
    headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() },
  });
  const { files } = JSON.parse(res.getContentText());
  return files
    .filter(({ source }) => source.match(reg))
    .map(({ name }) => name);
}

// For only container-bound script type.
function sample2() {
  const scriptId = "###"; // Please set file ID of Google Apps Script project.
  const searchText = "###"; // Please set the search text.

  const url = `https://www.googleapis.com/drive/v3/files/${scriptId}/export?mimeType=application%2Fvnd.google-apps.script%2Bjson`;
  const res = search_(url, searchText);
  console.log(res);
}

2-2. For both standalone type and container-bound type

In this sample script, both standalone type and container-bound type can be used. The setting for using this script might be a bit difficult.

Before you use this script, please link Google Cloud Platform Project to Google Apps Script Project, and enable Apps Script API at the API console. You can see how to do this at this document. In the current stage, in order to retrieve the Google Apps Script project of the container-bound type, it is required to link Google Cloud Platform Project to Google Apps Script Project. This is the current specification.

And also, please set the variables of scriptId and searchText. When this script is run, the value of searchText is searched from a standalone script of scriptId. When the value is found, the script file names in the Google Apps Script project are returned.

// Retrieve scripts, parse it and search the text.
function search_(url, searchText) {
  const reg = new RegExp(searchText, "g");
  const res = UrlFetchApp.fetch(url, {
    headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() },
  });
  const { files } = JSON.parse(res.getContentText());
  return files
    .filter(({ source }) => source.match(reg))
    .map(({ name }) => name);
}

// For both container-bound script type and standalone script type.
function sample3() {
  const scriptId = "###"; // Please set file ID of Google Apps Script project.
  const searchText = "###"; // Please set the search text.

  const url = `https://script.googleapis.com/v1/projects/${scriptId}/content`;
  const res = search_(url, searchText);
  console.log(res);
}

References

 Share!