I have tried printing text from the cursor, which works, but I basically am letting the user highlight text, then that text gets used in a program, but I want it to print text where the end of the highlight is, but I can only print things from where the cursor is, not highlighted things. This is my highlight code, ripped straight from google:
function getSelectedText() {
const selection = DocumentApp.getActiveDocument().getSelection();
const text = [];
if (selection) {
const elements = selection.getSelectedElements();
for (let i = 0; i < elements.length; ++i) {
if (elements[i].isPartial()) {
const element = elements[i].getElement().asText();
const startIndex = elements[i].getStartOffset();
const endIndex = elements[i].getEndOffsetInclusive();
text.push(element.getText().substring(startIndex, endIndex + 1));
} else {
const element = elements[i].getElement();
// Only translate elements that can be edited as text; skip images and
// other non-text elements.
if (element.editAsText) {
const elementText = element.asText().getText();
// This check is necessary to exclude images, which return a blank
// text element.
if (elementText) {
text.push(elementText);
}
}
}
}
}
if (!text.length) throw new Error('Please select some text.');
return text;
}
I have tried a couple things, such as moving the cursor to unhighlight text, but it doesnt work.
Any help would be greatly appreciated.
Tried: Move cursor to unhighlight text print straight from cursor, but it doesn't exist when something is highlighted.