How to get user selected text in a Google Doc?

1,490 views
Skip to first unread message

David Handel

unread,
Jul 2, 2021, 12:15:38 PM7/2/21
to Google Apps Script Community
Hi Guys,

The getSelection() method can be used to return the user selected text but is restricted to bound scripts. Is there a method for doing the same for standalone scripts that can then be used in a published add-on? If there is a solution, will it work when Docs changes rendering to canvas?

Cheers!

Clark Lind

unread,
Jul 3, 2021, 9:32:37 AM7/3/21
to Google Apps Script Community
Unless you are interacting with the underlying html framework at the browser level (e.g., a browser addon like Grammarly), switching to Canvas should be transparent to everyone. As for a published addon, once installed, shouldn't it act like a bound script? (I don't know... so asking the dumb question... lol).

Clark Lind

unread,
Jul 3, 2021, 9:34:41 AM7/3/21
to Google Apps Script Community
I ask since this quickstart shows user-selected text: https://developers.google.com/workspace/add-ons/editors/docs/quickstart/translate

Clark Lind

unread,
Jul 3, 2021, 9:35:29 AM7/3/21
to Google Apps Script Community
Nevermind, it is a bound script!  Doh!  Sorry.

Ed Bacher

unread,
Jul 3, 2021, 1:20:04 PM7/3/21
to Google Apps Script Community
The example uses DocumentApp.getActiveDocument(), which works for a bound document, but you may be able to use DocumentApp.openByUrl() from a standalone script.

I do this in my Docs to Markdown add-on, which exists as a bound script, but is now published as an add-on. It has code to use the openByUrl() function, but in practice it using just uses getActiveDocument since an add-on acts like a bound script. You can see the selection handling in this part of the source. It's a little bit different from what they're doing in the translation example, but it works well for partial selections.

David Handel

unread,
Jul 3, 2021, 4:45:32 PM7/3/21
to Google Apps Script Community
Thanks, Ed! You have been very helpful.

Cheers,
David

Hyun A Kim

unread,
Aug 3, 2023, 5:43:52 AM8/3/23
to Google Apps Script Community
Hi, it's rather late but how did you handle the problem of DocumentApp.openByUrl() opening a document in a different tab? I couldn't figure out the problem :(

cwl...@gmail.com

unread,
Aug 3, 2023, 12:37:53 PM8/3/23
to Google Apps Script Community
Not sure if it can be done since the script is not bound:

Gets the user's selection in the active document. A script can only access the selection of the user who is running the script, and only if the script is bound to the document.
Message has been deleted

Ed Bacher

unread,
Aug 3, 2023, 5:00:12 PM8/3/23
to Google Apps Script Community
Yes, I think that's correct: an add-on is effectively a container-bound script (since the user installed it and gave the script access to Drive documents). So, that code I have for DocumentApp.openByUrl() is probably unnecessary (unless you make a standalone script and feed it a document URL). I may try to get rid of that code to avoid any confusion (but will test it out before releasing any change -- the code works well as-is).

There's some more information about container-bound scripts here: https://developers.google.com/apps-script/guides/bound. It's not crystal clear, but add-ons seem to be effectively container-bound.

Ed

Hyun A Kim

unread,
Aug 5, 2023, 12:29:56 PM8/5/23
to Google Apps Script Community
Thank you so much for your reply!
Reply all
Reply to author
Forward
0 new messages