昨天添加了使用光标位置和选定文本的功能,解决了问题 2865:在 Document 中获取当前用户位置和状态信息。另请参阅博客文章。
Google 提供的方法与上述问题中描述的方法不同 - 没有事件,而是分别通过 Document.getCursor() 和 Document.getSelection() 获得有关光标位置和元素选择的信息。
这是问题中的代码,适用于新的 API。通过此示例,您可以选择文档中的任何文本,并使用所选文本添加指向 google 搜索的链接。
/**
* If user has selected text, add a url link there, unless
* the text already has a link.
*/
function addMyLink() {
var doc = DocumentApp.getActiveDocument();
var selection = doc.getSelection();
// get a URL pattern containing %target%.
// For this example, assume we want to build a google query
var baseUrl = "https://www.google.com/search?q=%target%";
if (!selection) {
tryAgain( "No current selection." );
}
else {
var elements = selection.getSelectedElements();
// We'll only put a hyperlink on text within a paragraph - if the selection
// spans paragraphs, reject it.
if (elements.length > 1) { tryAgain( "Selection cannot span paragraphs." ); return;}
var element = elements[0].getElement();
var elementText = element.asText();
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;
// If you have poo, fling it now...
// I mean - if you have other validation steps, put them here.
// Check if there's a link here already.
// Caveat: if there a link that starts INSIDE the selection,
// this won't find it.
if (elementText.getLinkUrl(startOffset) == null) {
// Generate the URL, and set it on the selected text
var url = baseUrl.replace('%target%',selectedText);
elementText.setLinkUrl(startOffset, endOffset, url);
}
else {
tryAgain( "Remove existing link first." );
}
}
}
/*
* Use Dialog Box to deliver a message to user, with
* "try again" message.
*
* @param {String} message The message to display in the dialog box
*
* @return {Button} The button the user clicked
*/
function tryAgain ( message ) {
var ui = DocumentApp.getUi();
return ui.alert("Please try again",message,ui.ButtonSet.OK);
}
function onOpen() {
DocumentApp.getUi().createMenu('Selection')
.addItem("Add Google Link", 'addMyLink' )
.addToUi();
}