【问题标题】:Get user-selected text获取用户选择的文本
【发布时间】:2013-05-19 20:47:49
【问题描述】:

我想在 Google Doc 中通过鼠标选择单词或行,并通过脚本获取这些选定的单词或行。

例子:

  var doc = DocumentApp.getActiveDocument();
  var docText = doc.editAsText();
  var text = docText.getSelection();

我试过了,但没有找到像 VBA 那样的选择访问方法。

【问题讨论】:

  • 这个问题非常模糊。你试过什么?你在别处搜索过吗?
  • 好,现在看清楚了吗???

标签: google-apps-script google-docs


【解决方案1】:

昨天添加了使用光标位置和选定文本的功能,地址为Issue 2865: Get current user location & state information in Document。另请参阅blog post

事实证明,使用选择有一些技巧。我试图在这里展示它们 - 如果你发现任何其他的,请添加 cmets,我很乐意更新。

function onOpen() {
  DocumentApp.getUi().createMenu('Selection')
    .addItem("Report Selection", 'reportSelection' )
    .addToUi();
}

function reportSelection () {
  var doc = DocumentApp.getActiveDocument();
  var selection = doc.getSelection();
  var ui = DocumentApp.getUi();

  var report = "Your Selection: ";

  if (!selection) {
    report += " No current selection ";
  }
  else {
    var elements = selection.getSelectedElements();
    // Report # elements. For simplicity, assume elements are paragraphs
    report += " Paragraphs selected: " + elements.length + ". ";
    if (elements.length > 1) {
    }
    else {
      var element = elements[0].getElement();
      var startOffset = elements[0].getStartOffset();      // -1 if whole element
      var endOffset = elements[0].getEndOffsetInclusive(); // -1 if whole element
      var selectedText = element.asText().getText();       // All text from element
      // Is only part of the element selected?
      if (elements[0].isPartial())
        selectedText = selectedText.substring(startOffset,endOffset+1);

      // Google Doc UI "word selection" (double click)
      // selects trailing spaces - trim them
      selectedText = selectedText.trim();
      endOffset = startOffset + selectedText.length - 1;

      // Now ready to hand off to format, setLinkUrl, etc.

      report += " Selected text is: '" + selectedText + "', ";
      report += " and is " + (elements[0].isPartial() ? "part" : "all") + " of the paragraph."
    }
  }
  ui.alert( report );
}

【讨论】:

  • 非常感谢您提醒我有关新发行说明以及您的示例:)
【解决方案2】:

如果您想检索已突出显示的文本,可以尝试...

function findHighlighted() {
  var body = DocumentApp.getActiveDocument().getBody(),
      bodyTextElement = body.editAsText(),
      bodyString = bodyTextElement.getText(),
      char, len;

  for (char = 0, len = bodyString.length; char < len; char++) {
    if (bodyTextElement.getBackgroundColor(char) == '#ffff00') // Yellow
      Logger.log(bodyString.charAt(char))}
}

源自Jonathan's I/O example。 但是请注意,在撰写本文时,working with cursor position and selections 尚不可用。

更新: 光标选择现在可用,请参阅docs

【讨论】:

  • 感谢您的回答,但我需要用鼠标选择一些单词然后脚本获取这些单词
  • 视频中提到的鼠标/光标选择尚不可用
【解决方案3】:

你很接近。我想你想要findText() method

var text = docText.findText("some string of text in the document") // for example

我不熟悉 VBA,但这可以在文档中选择文本。

【讨论】:

  • 谢谢你的回答,但我想用鼠标选择一些单词和脚本得到这个单词
【解决方案4】:

为了添加到 Bryan 的答案,我写了这个来转换单个字符以返回已突出显示的单词或短语数组。

   function findHighlighted() {
    var results = [];
    var body = DocumentApp.getActiveDocument().getBody(),
            bodyTextElement = body.editAsText(),
            bodyString = bodyTextElement.getText(),
            char, len;
    for (char = 0, len = bodyString.length; char < len; char++) {
            if (bodyTextElement.getBackgroundColor(char) !== null)
                    results.push([char, bodyString.charAt(char)]);
    }
    return results;
}

function getWords() {
    var arr = findHighlighted();
    var wordList = [];
    var holding = [];
    var nextNum, sum;
    for (var i = 0; i < arr.length; i++) {
            if (arr[i + 1] === undefined) {
                    nextNum = 0;
            } else {
                    nextNum = arr[i + 1][0];
            }
            sum = (Number(arr[i][0]) + 1);
            if (nextNum === sum) {
                    holding.push(arr[i][1]);
            } else {
                    holding.push(arr[i][1]);
                    wordList.push(holding.join(""));
                    holding = [];
            }
    }
    Logger.log(wordList);
}

【讨论】:

    【解决方案5】:
    function getHighlightedText() {
    var selection = DocumentApp.getActiveDocument().getSelection();
    if (selection) {
      var elements = selection.getRangeElements();
      for (var i = 0; i < elements.length; i++) {
        var element = elements[i];
        // Only modify elements that can be edited as text; skip images and other non-text elements.
        if (element.getElement().editAsText) {
          var text = element.getElement().editAsText();
          return text;
        }
      }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-20
      • 2011-07-04
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 2011-02-19
      • 2014-10-09
      相关资源
      最近更新 更多