[英]Get current word in google-apps-script
Just started working with Google-apps-script and for the most part it is pretty straight forward. 刚开始使用Google-apps-script,在大多数情况下,这非常简单。 I am having trouble though being able to get the current word the user is typing / where the cursor is.
我无法获得用户正在键入的当前单词/光标所在的位置。
ie, if |
即,如果
|
represents my cursor and I was typing the word "hello" and was in this state hello|
代表我的光标,我是键入单词“你好”,并在此状态下
hello|
I want to return hello as a string. 我想以字符串形式返回hello。
I've tried many variations to get this, but can't seem to find how in the api. 我已经尝试了多种变体来实现这一点,但似乎无法在api中找到方法。
var doc = DocumentApp.getActiveDocument();
var word = doc.getCursor().getElement().asText().getText();
If I had hello all|
如果我
hello all|
(cursor at the end of all) I'd get the entire string 'hello all' back. (所有光标的末尾)我会得到整个字符串“ hello all”。 I am hoping there is a way to just pick off the last element (after the last space).
我希望有一种方法可以选择最后一个元素(在最后一个空格之后)。
Thanks in advance! 提前致谢!
This gives you the last complete word: 这给您最后一个完整的单词:
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var words = doc.getCursor().getElement().asText().getText().split(' ');
Logger.log(words[parseInt(words.length)-2]);
}
This gives you the last word you where typing on: 这将为您提供键入时的最后一个字:
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var words = doc.getCursor().getElement().asText().getText().split(' ');
Logger.log(words[parseInt(words.length)-1]);
}
You should change the split method with the correct regex to enable puntuation and enters. 您应该使用正确的正则表达式更改split方法,以启用标点和进入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.