0

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.

  • 1
    I have to apologize for my poor English skill. Unfortunately, I cannot understand your question. In order to correctly understand your question, can you provide the sample input and output situations you expect? First, I would like to correctly understand your question. – Tanaike Apr 20 '23 at 22:57
  • For example, you know how you can click and hold to highlight text in blue? When you do that, at the very edge of that highlight, append text. So If I had the text "Hello", and I highlight He, I could add World, and it would look like HeWorldllo. – Colin Wooldridge Apr 21 '23 at 00:44
  • Thank you for replying. I would like to support you. But, I have to apologize for my poor English skill, again. Unfortunately, from your reply, I cannot still understand your question. But I would like to try to understand it. When I could correctly understand it, I would like to think of a solution. I would be grateful if you can forgive my poor English skill. – Tanaike Apr 21 '23 at 00:55
  • On google docs, you can highlight text in blue. I want to add text to the end of the highlighted text. If this is still unclear, would you mind telling me what language you do speak? – Colin Wooldridge Apr 21 '23 at 13:12
  • Have you read [How can I move the cursor to the end of the inserted text with Google Apps Script for Docs?](https://stackoverflow.com/a/47503896/1330560) – Tedinoz Apr 23 '23 at 05:10
  • `If this is still unclear, would you mind telling me what language you do speak` @Tanaike is not the only user wondering exactly what _you_ mean and his spoken language is irrelevant. May I respectfully suggest that you edit your question to provide an explanation of your scenario showing text to be inserted, a snapshot of a **before** document including a highlighted selection (in blue) where the text is to be inserted, and a snapshot of an **after** document showing the inserted text. – Tedinoz Apr 23 '23 at 05:21

0 Answers0