Google Apps脚本:查找文本并将光标设置为找到的文本 [英] Google Apps Script: Finding text and setting cursor to found text

查看:101
本文介绍了Google Apps脚本:查找文本并将光标设置为找到的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是我正在尝试的内容:

  var position = doc.newPosition(foundElement.getElement(),foundElement.getStartOffset()); 
doc.setCursor(position);

但是,游标不会移动。即使只是一些简单的例子,例如:

$ p $ //在doc开头设置光标
var paragraph = doc.getBody ().getChild(0);
var position = doc.newPosition(paragraph.getChild(0),0);
doc.setCursor(position);

findText返回一个范围元素,而document.setCursor需要一个位置。我如何从rangeelement到位置? :)



这让我获得了解决方案的一半查找文本(多次)并高亮显示

解决方案

试过这段代码,它完全设置光标在找到的文本的开头。

  function myFunction(){
var doc = DocumentApp.getActiveDocument() ;
var paragraph = doc.getBody()。getChild(0);
var foundElement = doc.getBody()。findText(text);
var position = doc.newPosition(foundElement.getElement(),foundElement.getStartOffset());
doc.setCursor(position);
}

希望有帮助!


I would like to setCursor to the location that is returned by findText.

Here is what I am trying:

    var position = doc.newPosition(foundElement.getElement(), foundElement.getStartOffset());
    doc.setCursor(position);

But, the cursor does not move. Even with the simple examples like

 //setting cursor at the beginning of the doc
 var paragraph = doc.getBody().getChild(0);
 var position = doc.newPosition(paragraph.getChild(0), 0);
 doc.setCursor(position);

findText returns a rangelement, while document.setCursor expects a position. How do I go from rangeelement to position? :)

This got me halfway to the solution Finding text (multiple times) and highlighting

解决方案

Tried this code and it is perfectly setting the cursor in the beginning of the found text.

function myFunction() {
  var doc = DocumentApp.getActiveDocument();
  var paragraph = doc.getBody().getChild(0);
  var foundElement = doc.getBody().findText("text");
  var position = doc.newPosition(foundElement.getElement(), foundElement.getStartOffset());
    doc.setCursor(position);
}

Hope that helps!

这篇关于Google Apps脚本:查找文本并将光标设置为找到的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆