我想選擇單詞或行通過鼠標從Google文檔和腳本我得到這個選定的單詞或行。Google App腳本文檔應用程序獲取選定的行或詞嗎?
var doc=DocumentApp.getActiveDocument();
var docText = doc.editAsText();
var text=docText.getSelection();
我試過,但我沒有找到任何選擇的方法,如在VBA
我想選擇單詞或行通過鼠標從Google文檔和腳本我得到這個選定的單詞或行。Google App腳本文檔應用程序獲取選定的行或詞嗎?
var doc=DocumentApp.getActiveDocument();
var docText = doc.editAsText();
var text=docText.getSelection();
我試過,但我沒有找到任何選擇的方法,如在VBA
昨天增加了使用光標位置和選定文本的能力,地址爲Issue 2865: Get current user location & state information in Document。請參閱blog post。
事實證明,有一些技巧來處理選擇。我試圖在這裏展示他們 - 請添加評論,如果你找到其他人,我會很樂意更新。
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);
}
非常感謝你提醒我關於新發布的注意事項,也爲你的例子:) – Amany
你接近。我想你想要findText() method。
var text = docText.findText("some string of text in the document") // for example
我對VBA並不熟悉,但是這會在文檔中選擇文本。
謝謝你的回答,但我想用鼠標選擇一些單詞和腳本得到這個單詞 – Amany
如果你想找回你選中的文本,可以試試...
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在撰寫本文時尚未提供。
UPDATE: 光標選擇已經上市,見docs。
要添加到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);
}
這個問題非常模糊。你有什麼嘗試?你在別處搜索過嗎? – rGil
好吧現在就看清楚了嗎? – Amany