Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

60%
+1 −0
Q&A How to solve the "Cannot call from this context" error?

Meaning This error happens on context mismatch and is specific to container-bound scripts. The reason for the error is attempting to call a method only available in one document type (usually, ge...

posted 10mo ago by Oleg Valter‭  ·  edited 10mo ago by Oleg Valter‭

Answer
#5: Post edited by user avatar Oleg Valter‭ · 2023-07-30T15:41:23Z (10 months ago)
minor readability improvement
  • **Meaning**
  • This error happens on **context mismatch** and is specific to [container-bound scripts](https://developers.google.com/apps-script/guides/bound).
  • The reason for the error is attempting to call a method only available *in one document type* (usually, `getUi()` as its [`Ui`](https://developers.google.com/apps-script/reference/base/ui) class is shared between services) from another (i.e., `DocumentApp.getUi()` from a spreadsheet-bound script) or from a [standalone script](https://developers.google.com/apps-script/guides/standalone).
  • **Reproducing**
  • To get a container-bound script context mismatch error, declare and run this function in a either a standalone script or one that is container-bound to a document other than a Google Docs one:
  • ```javascript
  • const cannotCallFromThisContext = () => {
  • try {
  • DocumentApp.getUi();
  • } catch(e) {
  • console.log(e);
  • }
  • };
  • ```
  • **Fixing**
  • There is no fix for the error _per se_ other than removing all calls for container-bound methods.
  • **Meaning**
  • This error happens on **context mismatch** and is specific to [container-bound scripts](https://developers.google.com/apps-script/guides/bound).
  • The reason for the error is attempting to call a method only available *in one document type* (usually, `getUi()` as its [`Ui`](https://developers.google.com/apps-script/reference/base/ui) class is shared between services) from a script bound to a document of other type (i.e., `DocumentApp.getUi()` from a spreadsheet-bound script) or from a [standalone script](https://developers.google.com/apps-script/guides/standalone).
  • **Reproducing**
  • To get a container-bound script context mismatch error, declare and run this function in a either a standalone script or one that is container-bound to a document other than a Google Docs one:
  • ```javascript
  • const cannotCallFromThisContext = () => {
  • try {
  • DocumentApp.getUi();
  • } catch(e) {
  • console.log(e);
  • }
  • };
  • ```
  • **Fixing**
  • There is no fix for the error _per se_ other than removing all calls for container-bound methods.
#4: Post edited by user avatar Oleg Valter‭ · 2023-07-30T15:39:33Z (10 months ago)
reduced unnecessary verbosity
  • **Meaning**
  • This error happens on **context mismatch** and is specific to [container-bound scripts](https://developers.google.com/apps-script/guides/bound).
  • The primary reason for the error is attempting to call a method only available *in one document type* (usually, `getUi()` as its [`Ui`](https://developers.google.com/apps-script/reference/base/ui) class is shared between services) from another (i.e., `DocumentApp.getUi()` from a spreadsheet-bound script) or from a [standalone script](https://developers.google.com/apps-script/guides/standalone).
  • **Reproducing**
  • To get a container-bound script context mismatch error, declare and run this function in a script project in either a standalone script or one that is container-bound to a document other than a Google Docs one:
  • ```javascript
  • const cannotCallFromThisContext = () => {
  • try {
  • DocumentApp.getUi();
  • } catch(e) {
  • console.log(e);
  • }
  • };
  • ```
  • **Fixing**
  • There is no fix for the error _per se_ other than removing all calls for container-bound methods.
  • **Meaning**
  • This error happens on **context mismatch** and is specific to [container-bound scripts](https://developers.google.com/apps-script/guides/bound).
  • The reason for the error is attempting to call a method only available *in one document type* (usually, `getUi()` as its [`Ui`](https://developers.google.com/apps-script/reference/base/ui) class is shared between services) from another (i.e., `DocumentApp.getUi()` from a spreadsheet-bound script) or from a [standalone script](https://developers.google.com/apps-script/guides/standalone).
  • **Reproducing**
  • To get a container-bound script context mismatch error, declare and run this function in a either a standalone script or one that is container-bound to a document other than a Google Docs one:
  • ```javascript
  • const cannotCallFromThisContext = () => {
  • try {
  • DocumentApp.getUi();
  • } catch(e) {
  • console.log(e);
  • }
  • };
  • ```
  • **Fixing**
  • There is no fix for the error _per se_ other than removing all calls for container-bound methods.
#3: Post edited by user avatar Oleg Valter‭ · 2023-07-30T15:37:14Z (10 months ago)
removed outdated info on custom functions
  • **Meaning**
  • This error happens on **context mismatch** and is specific to [container-bound scripts](https://developers.google.com/apps-script/guides/bound).
  • The primary reason for the error is attempting to call a method only available *in one document type* (usually, `getUi()` as its [`Ui`](https://developers.google.com/apps-script/reference/base/ui) class is shared between services) from another (i.e., `DocumentApp.getUi()` from a spreadsheet-bound script) or from a [standalone script](https://developers.google.com/apps-script/guides/standalone).
  • A secondary case is attempting to access a service not listed as a [supported service](https://developers.google.com/apps-script/guides/sheets/functions#using_services) in a [custom function](https://developers.google.com/apps-script/guides/sheets/functions#using_services) (a function marked by a special JSDoc-style comment `@customfunction` that can be used as a formula in Google Sheets).
  • **Reproducing**
  • To get a container-bound script context mismatch error, declare and run this function in a script project in either a standalone script or one that is container-bound to a document other than a Google Docs one:
  • ```javascript
  • const cannotCallFromThisContext = () => {
  • try {
  • DocumentApp.getUi();
  • } catch(e) {
  • console.log(e);
  • }
  • };
  • ```
  • **Fixing**
  • There is no fix for the error _per se_ other than removing all calls for container-bound methods.
  • **Meaning**
  • This error happens on **context mismatch** and is specific to [container-bound scripts](https://developers.google.com/apps-script/guides/bound).
  • The primary reason for the error is attempting to call a method only available *in one document type* (usually, `getUi()` as its [`Ui`](https://developers.google.com/apps-script/reference/base/ui) class is shared between services) from another (i.e., `DocumentApp.getUi()` from a spreadsheet-bound script) or from a [standalone script](https://developers.google.com/apps-script/guides/standalone).
  • **Reproducing**
  • To get a container-bound script context mismatch error, declare and run this function in a script project in either a standalone script or one that is container-bound to a document other than a Google Docs one:
  • ```javascript
  • const cannotCallFromThisContext = () => {
  • try {
  • DocumentApp.getUi();
  • } catch(e) {
  • console.log(e);
  • }
  • };
  • ```
  • **Fixing**
  • There is no fix for the error _per se_ other than removing all calls for container-bound methods.
#2: Post edited by user avatar Oleg Valter‭ · 2023-07-30T15:29:39Z (10 months ago)
added info on the secondary reason for the error
  • **Meaning**
  • This error happens on **context mismatch** and is specific to [container-bound scripts](https://developers.google.com/apps-script/guides/bound).
  • The primary reason for the error is attempting to call a method only available *in one document type* (usually, `getUi()` as its [`Ui`](https://developers.google.com/apps-script/reference/base/ui) class is shared between services) from another (i.e., `DocumentApp.getUi()` from a spreadsheet-bound script) or from a [standalone script](https://developers.google.com/apps-script/guides/standalone).
  • **Reproducing**
  • To get a container-bound script context mismatch error, declare and run this function in a script project in either a standalone script or one that is container-bound to a document other than a Google Docs one:
  • ```javascript
  • const cannotCallFromThisContext = () => {
  • try {
  • DocumentApp.getUi();
  • } catch(e) {
  • console.log(e);
  • }
  • };
  • ```
  • **Fixing**
  • There is no fix for the error _per se_ other than removing all calls for container-bound methods.
  • **Meaning**
  • This error happens on **context mismatch** and is specific to [container-bound scripts](https://developers.google.com/apps-script/guides/bound).
  • The primary reason for the error is attempting to call a method only available *in one document type* (usually, `getUi()` as its [`Ui`](https://developers.google.com/apps-script/reference/base/ui) class is shared between services) from another (i.e., `DocumentApp.getUi()` from a spreadsheet-bound script) or from a [standalone script](https://developers.google.com/apps-script/guides/standalone).
  • A secondary case is attempting to access a service not listed as a [supported service](https://developers.google.com/apps-script/guides/sheets/functions#using_services) in a [custom function](https://developers.google.com/apps-script/guides/sheets/functions#using_services) (a function marked by a special JSDoc-style comment `@customfunction` that can be used as a formula in Google Sheets).
  • **Reproducing**
  • To get a container-bound script context mismatch error, declare and run this function in a script project in either a standalone script or one that is container-bound to a document other than a Google Docs one:
  • ```javascript
  • const cannotCallFromThisContext = () => {
  • try {
  • DocumentApp.getUi();
  • } catch(e) {
  • console.log(e);
  • }
  • };
  • ```
  • **Fixing**
  • There is no fix for the error _per se_ other than removing all calls for container-bound methods.
#1: Initial revision by user avatar Oleg Valter‭ · 2023-07-30T15:26:56Z (10 months ago)
**Meaning**

This error happens on **context mismatch** and is specific to [container-bound scripts](https://developers.google.com/apps-script/guides/bound). 

The primary reason for the error is attempting to call a method only available *in one document type* (usually, `getUi()` as its [`Ui`](https://developers.google.com/apps-script/reference/base/ui) class is shared between services) from another (i.e., `DocumentApp.getUi()` from a spreadsheet-bound script) or from a [standalone script](https://developers.google.com/apps-script/guides/standalone).

**Reproducing**

To get a container-bound script context mismatch error, declare and run this function in a script project in either a standalone script or one that is container-bound to a document other than a Google Docs one:

```javascript
const cannotCallFromThisContext = () => {
  try {
    DocumentApp.getUi();
  } catch(e) {
    console.log(e);
  }
};
```

**Fixing**

There is no fix for the error _per se_ other than removing all calls for container-bound methods.