最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to "Select Nothing" in Google Docs from Google Apps Script - Stack Overflow

programmeradmin0浏览0评论

I have a Google Apps Script that selects a range:

    const editingRange = body.findText(needle);
    if (editingRange) {
        const range = doc.newRange();
        range.addElement(editingRange.getElement().asText(),
                         editingRange.getStartOffset(),
                         editingRange.getEndOffsetInclusive())
        doc.setSelection(range.build())
    }

This works fine.

Now I'd like to select nothing (as if the user had just clicked somewhere in the document).

How do I do that?

Things I have tried

doc.setSelection(null);

gives an error saying argument range cannot be null.

doc.setSelection(doc.newRange().build());

gives

Exception: Invalid argument: range at clearSelection7(argh:473:9)

I have scoured the docs without finding anything.

(I see getSelection returns null if there is no selection, you'd think setSelection would then accept null to select nothing, but no.)

I have a Google Apps Script that selects a range:

    const editingRange = body.findText(needle);
    if (editingRange) {
        const range = doc.newRange();
        range.addElement(editingRange.getElement().asText(),
                         editingRange.getStartOffset(),
                         editingRange.getEndOffsetInclusive())
        doc.setSelection(range.build())
    }

This works fine.

Now I'd like to select nothing (as if the user had just clicked somewhere in the document).

How do I do that?

Things I have tried

doc.setSelection(null);

gives an error saying argument range cannot be null.

doc.setSelection(doc.newRange().build());

gives

Exception: Invalid argument: range at clearSelection7(argh:473:9)

I have scoured the docs https://developers.google.com/apps-script/reference/document/document without finding anything.

(I see getSelection returns null if there is no selection, you'd think setSelection would then accept null to select nothing, but no.)

Share Improve this question asked Feb 6 at 13:55 unhammerunhammer 4,7004 gold badges44 silver badges61 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

On a whim I tried setting the cursor, and it worked! This seems to do the equivalent of a "select none":

export function clearHighlight() {
    const doc = DocumentApp.getActiveDocument();
    const paragraphs = doc.getBody().getParagraphs();
    if(paragraphs) {
        // just pick a position at the first element of the document
        const pos = doc.newPosition(paragraphs[0].getChild(0), 0); 
        doc.setCursor(pos);
    }
}

On the downside, even though this function seems to do nearly nothing, the script.google.com Executions log says it often takes over a second :(

Here is a simple example of getting the cursor location. From that you can build whatever edit or reformat you like.

function getCursor() {
  try {
    let doc = DocumentApp.getActiveDocument();
    let cursor = doc.getCursor();
    let ui = DocumentApp.getUi();
    let text = cursor.getSurroundingText().getText();
    let offset = cursor.getOffset();
    ui.alert("the cursor is in the following text: ["+text+"] at offset: "+offset);
  }
  catch(err) {
    console.log("Error in getCursor: "+err);
  }
}

Reference:

  • Document.getCursor()
发布评论

评论列表(0)

  1. 暂无评论