Rio
Rio

Reputation: 14872

Get selection from Google Docs

Is there a way around the fact that

window.getSelection()

does not seem to work in Google Docs? I want to capture text from a highlighted selection from Google Docs. My implementation is with Chrome Extensions, but that is not as relevant as the fact that window.getSelection() doesn't seem to work there (though it does elsewhere).

Thanks!

Upvotes: 1

Views: 1330

Answers (2)

EzequielS121
EzequielS121

Reputation: 11

I got the selected text with this:

let selection = DocumentApp.getActiveDocument().getSelection();
console.log(selection.getRangeElements()[0].getElement().asText().getText());

But it gives you the whole paragraph.

Upvotes: 0

ephemient
ephemient

Reputation: 204668

As mentioned on Docs Blog - What’s different about the new Google Docs?, Google Docs eschews the browser's native capabilities and implements everything — including text formatting and positioning, cursor movement, and selection handling — in its own Javascript.

Since Docs doesn't provide a public in-browser API to the document currently being edited, you'll either have to dig through its private innards or give up.

Upvotes: 3

Related Questions