Home > database >  Call App Script function from Angular/Javascript
Call App Script function from Angular/Javascript

Time:10-26

I am new to Google docs app script.

Actually I am looking for insert the text in document from third party application by calling AppScript function by APIs but the getCursor function is getting null so i am not ableto get cursor location. Below is my script:

DocumentApp.getActiveDocument().getCursor().insertText("Test...")

Response:

{ "done": true, "error": { "message": "ScriptError", "code": 3, "details": [ { "errorType": "ScriptError", "errorMessage": "TypeError: Cannot read property 'insertText' of null", "@type": "type.googleapis.com/google.apps.script.v1.ExecutionError", "scriptStackTraceElements": [ { "function": "myFuction", "lineNumber": 31 } ] } ] } }

Is there any way to do that? Is there any JS library to call Appscript function?

Thanks - Sahi

CodePudding user response:

Situation:

If I understand your situation correctly:

  • You have deployed a script that is bound to a Google Docs.
  • You are calling this deployment using a 3rd party.

Issue:

The problem here is that you cannot get information on a user's cursor (and hence cannot use getCursor) in this context. That's because there's no client associated with this script execution. Even if the user has the document opened on a browser window, the script execution doesn't "know" about this, both circumstances are unrelated to each other.

Think for example about the possibility of opening the document on a second tab, and putting the cursor in a different place than the first one. Which location should getCursor point to? None of them, because the script is not getting called from there.

Now, if you called your function from that opened tab, then you could get the cursor information. For example, you could have a custom menu call it. Or maybe you could be using this as an add-on. Or you could just click Extensions > Apps Script and call the function directly from your script editor.

When calling this from a third party, even if the request is authenticated with the same user who has a browser tab with the document opened, there's no way you can get that tab's cursor information. The script can get the active document because it is bound to it, but no other data that is specifically related to the opened tab (so something like getSelection would also be inaccessible).

Workarounds:

So, if you want to add text to the document, you have to use methods that don't use data specifically related to an open tab. For example, you could use Body.appendParagraph to append text to the end of your document, or use findText to first find a certain word in your document and append your text there. That is, you have to use methods that relate to the document as it is, not to the document being opened in your browser.

Another option would be not calling this using a 3rd party, and use this instead as an add-on, or what have you. But that depends on the specific circumstances and requirements of your project.

  • Related