1

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.

1
  • 2
    Unfortunately, Sheets API cannot manage Google Apps Script in the Spreadsheet. In that case, how about using Apps Script API? Ref
    – Tanaike
    Dec 25, 2020 at 22:40

1 Answer 1

3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.