Michael T.
Michael T.

Reputation: 48

Is there a way in the Google Sheets API to access the script editor?

For example, I'd like to write a (Python, say) program that generates a Google sheet, then writes some custom .gs code into the Apps Script attached to that sheet, then writes values into the sheet itself using formulas defined in the Apps Script. What I currently do is use Tools > Script Editor, then manually copy-paste the relevant Apps Script code.

Upvotes: 1

Views: 277

Answers (1)

Kristkun
Kristkun

Reputation: 5963

As mentioned by @Tanaike, You can use Apps Script API to add container-bound script.

(OPTION 1: Writing the function manually in the script content)

What to do:

  1. Create a container-bound script using projecs.create method.

To set a container-bound script, you need to assign the spreadsheet file id to the parentId parameter

Sample Request Body:

{
  "title": "new",
  "parentId": "spreadsheet file id"
}
  1. Get the newly created container-bound script id in the project response body of projecs.create method.

Sample Response Body:

{
  "scriptId": "newly created script id",
  "title": "new",
  "parentId": "spreadsheet file id",
  "createTime": "2020-12-25T23:33:48.026Z",
  "updateTime": "2020-12-25T23:33:48.026Z"
}
  1. Update the content of the newly created bound script using projects.updateContent method and include your function.

Sample Request Body:

{
  files: [{
      name: 'hello',
      type: 'SERVER_JS',
      source: 'function helloWorld() {\n  console.log("Hello, world!");\n}'
    }, {
      name: 'appsscript',
      type: 'JSON',
      source: "{\"timeZone\":\"America/New_York\",\"" +
      "exceptionLogging\":\"CLOUD\"}"
    }]
}

(OPTION 2: Copy an existing script and paste it as bound script)

  1. Create a standalone script that will contain your custom functions.
  2. Get the newly created standalone script project content using projects.getContent which will return a content resource

scriptId can be seen in your standalone script project under File -> Project Properties

  1. Create a container-bound script using projecs.create method.

To set a container-bound script, you need to assign the spreadsheet file id to the parentId parameter

Sample Request Body:

{
  "title": "new",
  "parentId": "spreadsheet file id"
}
  1. Get the newly created container-bound script id in the project response body of projecs.create method.

Sample Response Body:

{
  "scriptId": "newly created script id",
  "title": "new",
  "parentId": "spreadsheet file id",
  "createTime": "2020-12-25T23:33:48.026Z",
  "updateTime": "2020-12-25T23:33:48.026Z"
}
  1. Update the content of the newly created bound script using projects.updateContent method.

Use the content resource returned in Step 2 as the request body. Make sure to replace the script id based on the newly created bound script id that is obtained in Step 4.


Example Standalone Script:

enter image description here

Example Result of the Workaround:

enter image description here


You can now use the custom function in the Google Sheets

enter image description here

Upvotes: 3

Related Questions