1

Here's what I want: In Google Sheets, when I'm editing the contents of a cell, I want to be able to select some of contents (by clicking and dragging), and then run a macro that will modify the selected text in some way (say, add something before and after the selection). Note that I don't want the contents of the whole cell, just the selected text in that cell.

I've searched a lot and I can't quite find how to do this with the APIs and a script. It seems maybe possible with Google Docs, but I'm looking for this in Google Sheets.

Is there a way to do this?

edit: more specifically, is there an API function to get the currently selected text?

I've seen these (and many other) posts and they are not what I'm asking:

  1. How can I get text from a cell using the select statement of the query function in Google Sheets?
  2. Google apps script :: Is it possible to get the cursor position or get the selected text from Google Document
YungHummmma
  • 113
  • 5
  • StackOverFlow is not a code writing service. You write your own code and when you have a problem you can come here for help. – Cooper May 19 '20 at 20:19
  • Welcome to StackOverFlow please take this opportunity to take the [tour] and learn how to [ask], [format code](https://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks), [mcve] and [Tag Info](https://stackoverflow.com/tags/google-apps-script/info) – Cooper May 19 '20 at 20:20
  • @Cooper I'm not asking for someone to write my code. I'm asking if it's *possible* (i.e., if there's an API function) to get selected text in Google Sheets with a script. – YungHummmma May 19 '20 at 20:28
  • Have your read the selected answer on you second link? – Cooper May 19 '20 at 20:30
  • @Cooper I have. Did you see that the answer was in 2013 and a lot of the API has changed since then? – YungHummmma May 19 '20 at 20:33
  • Here's a more recent answer: https://stackoverflow.com/questions/45538378/is-it-possible-to-capture-the-selected-portion-of-text-within-a-spreadsheet-cell – Cooper May 19 '20 at 20:57
  • You might be able to export the the cell text into a textarea in a dialog and make your selection there using the technique describe here:https://stackoverflow.com/questions/275761/how-to-get-selected-text-from-textbox-control-with-javascript – Cooper May 19 '20 at 21:01
  • @Cooper I saw that as well (not realizing it was you) during my search. I decided to ask again in case something had changed since then. To be clear, in your post, the selection is only able to be done in a table in Google Docs, but not in Google sheets itself? Thanks. – YungHummmma May 19 '20 at 21:02
  • As far as I know it can't be done in a spreadsheet cell. You can do it with a Google Document although you find it is a little more complicated than you might first think. But you could capture the text in a cell and place it into a html textarea make you selecion from a dialog. – Cooper May 19 '20 at 21:05

1 Answers1

1

The Google Sheets API doesn't include "active" methods, only the Spreadsheet Service from Google Apps Script. While the last have getCurrentCell() and getActiveRange(), it doesn't has a "getSelectedText" or similar method.

Reference

Rubén
  • 24,097
  • 9
  • 55
  • 116