【问题标题】:Change the color of the word where the cursor is with Google Apps Script in a Google Document使用 Google 文档中的 Google Apps 脚本更改光标所在单词的颜色
【发布时间】:2020-02-04 08:05:56
【问题描述】:

我正在尝试更改当前 google docs 光标所在单词的颜色(执行服务器功能时)。我不知道该怎么做。我看到了:Get current word in google-apps-script,它解释了如何获取该行的最后一个单词,但这并没有真正起作用,这就是我所拥有的(非常粗略的代码):

function clientize() {///
  var t = Date.now();
  var doc = DocumentApp.getActiveDocument().getCursor();
  var els = doc.getElement();
  var txt = els.asText().getText().split(" ");
  var word = txt[txt.length - 1];
  var offset = doc.getOffset();
  var highlightStyle = {};
  highlightStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FF0000';
  els.setBold(offset - 5,offset , true)
 // els.setAttributes(4,10,highlightStyle);

  return (word)
}

但它只给了我行中的最后一个单词,而不是光标实际所在的单词,而且我不知道一旦找到文本后如何更改文档中单词的颜色。我知道我可以根据某个范围更改颜色,但它只适用于 getElement(),但是如何更改光标所在的整个单词的特定范围?

例如,假设我有行,带有光标:

hello world ho|w are you today

那么当我激活服务器功能时,例如只有“如何”这个词应该变成红色。

这怎么可能????

【问题讨论】:

    标签: javascript google-apps-script google-docs google-apps google-docs-api


    【解决方案1】:

    你可以这样做,诀窍是获取当前用户单词开头和结尾的偏移量。

    function clientize() {
    
      // Get the Position of the user's cursor
      var position = DocumentApp.getActiveDocument().getCursor();
    
      // Get the element that contains this Position
      var ref_element = position.getElement();
    
      // Get the text in the element
      var text = ref_element.asText().getText(); 
    
      // Get the cursor's relative location within ref_element.
      // Verify if cursor is within text elem. or paragraph elem.
      var ref_offset = position.getOffset();
      if (ref_element.getType() == DocumentApp.ElementType.PARAGRAPH){
    
        // Cursor is at beginning or end of line
        if (ref_offset == 1){
    
          // Cursor is at last word. Move to the left into TEXT
          ref_offset = text.length - 1;
    
        }
      }
    
      // Get offset for current word's initial char
      var offset_first = ref_offset - text.substring(0,ref_offset).split(" ")[text.substring(0,ref_offset).split(" ").length - 1].length;
    
      // Get how many chars to the end of current word
      var chars_to_end;
      if (text.substring(ref_offset, ref_offset + text.length - 1).indexOf(" ") > -1) {
    
        // if word has a trailing space exclude it from count
        chars_to_end = text.substring(ref_offset, ref_offset + text.length - 1).indexOf(" ");
    
      } else {
        chars_to_end = text.substring(ref_offset, ref_offset + text.length - 1).length - 1;
      }
    
      // Get offset for current word's last char
      var offset_last = ref_offset + chars_to_end;
    
      // Define the styling attributes
      var style = {};
      style[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FF00FF';
    
      // Apply styling to current word
      ref_element.asText().setAttributes(offset_first, offset_last, style);
    }
    

    注意:如果返回的单词有逗号或其他字符尾随或前导,则必须对其进行过滤。 提示:使用正则表达式。 看看这个测试document with it's bound script 看看它是否有效。

    这里的关键函数也供参考:setAttributes(startOffset, endOffsetInclusive, attributes)

    请注意,有很多方法可以做到这一点,这只是实现它的一种方法。

    【讨论】:

    • 非常好的解决方案,正是我想要的!除了有一件额外的事情我想知道您是否可以帮助解决:当光标位于一行末尾的单词末尾时,然后在激活脚本时出现错误:“找不到方法 setAttributes(number, number,object)",你知道如何解决这个问题吗?我认为它与实际读取下一行而不是光标所在行的 getElement() 有关,您知道解决方法/某种解决方案吗?
    • @bluejayke 很好的观察。如果光标位于行首或行尾,则包含它的元素是DocumentApp.ElementType.PARAGRAPH 类型,因此没有setAttributes(number,number,object) 方法。鉴于此警告,需要调整代码以解决这种可能性。我已经编辑了代码,现在应该可以按预期工作了。 :)
    猜你喜欢
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    相关资源
    最近更新 更多