0

I am linking a Google Apps script in my Drive to a Spreadsheet, how do I go about finding the script by ID.

I have used this: var ss=SpreadsheetApp.openById(ssid); to find the spreadsheet with the following: var ssid="xxx___xxx";

xxx___xxx being the as https://docs.google.com/spreadsheets/d/**xxx___xxx**/edit#gid=0.

How do I write the same components for a google apps script something like as follows:

var ss=**GoogleAppsScript Document name**.openById(**the id name for a Google Apps Script**)

With the following: var **the id name for a Google Apps Script**="xxx___xxx";?

Mahesh Waghmare
  • 726
  • 9
  • 27
Gav
  • 328
  • 5
  • 17
  • 4
    If you want to retrieve the script ID of the container-bound script from the filename and file ID of Spreadsheet (Google Docs), unfortunately, in the current stage, there are no methods for achieving this yet. (https://issuetracker.google.com/issues/111149037 , https://stackoverflow.com/q/54990478 If you want to retrieve the script ID at the container-bound script, you can retrieve it with `Logger.log(ScriptApp.getScriptId())`. [Ref](https://developers.google.com/apps-script/reference/script/script-app#getScriptId()) If I misunderstood your question, I apologize. – Tanaike Nov 27 '19 at 12:00

1 Answers1

0

Using UrlFetchApp, you can use the Apps Script API. You won't be able to manipulate the script in the same way that you can a Spreadsheet, but it might be sufficient for your purposes.

Here's how to get a project:

function getProject() {
  var scriptId = "";
  var url = "https://script.googleapis.com/v1/projects/" + scriptId;
  var params = {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}};
  var project = UrlFetchApp.fetch(url, params);
  Logger.log(project);
}

To use this correctly, you will need to first

  1. Switch to a standard GCP project
  2. Enable the Apps Script API in the project (*)
  3. Set explicit scopes for your project. The two listed below grant access to Apps Script API & UrlFetchApp, respectively:

    "oauthScopes": [
      "https://www.googleapis.com/auth/script.projects",
      "https://www.googleapis.com/auth/script.external_request"
    ]
    

(*) Here's a button that will create a GCP project and enable the API for you in one step. Then all you have to do is link your GCP project to the script. (After clicking the button, click the "API Console" link at the bottom of the modal to open it in Cloud Console.)

Diego
  • 9,261
  • 2
  • 19
  • 33