Google Apps 脚本 - 移动光标 onclick
Google Apps Script - Move Cursor onclick
我想在 Google Docs 文档的侧边栏中实现 Table 的目录,点击后会转到相应的部分。我正在逐个元素为侧边栏生成 HTML,我看到 Document
class 中有一个 moveCursor(position)
函数,但我看不出如何实际使用 onclick 调用它。不是完整的代码,但显示了问题:
function generateHtml() {
var html = HtmlService.createHtmlOutput('<html><body>');
var document = DocumentApp.getActiveDocument();
var body = document.getBody();
//Iterate each document element
var totalElements = body.getNumChildren();
for(var i = 0; i < totalElements; ++i) {
var element = body.getChild(i);
if(element.getType() == DocumentApp.ElementType.PARAGRAPH) {
var text = paragraph.getText();
if(text.trim()) { //Not blank paragraph
var position = document.newPosition(element, 0);
/**Would like to have <a onclick=document.moveCursor(position)> here**/
//Show first 20 chars as preview in table of contents
html.append('Detected paragraph ')
.append(text.substring(0, 20))
.append('<br />');
}
}
}
html.append('</body></html>');
return html;
}
如何在 Apps 脚本中完成此操作?代码可以根据需要完全重构。
这一行:
/**Would like to have <a onclick=document.moveCursor(position)> here**/
更改为:
<div onmouseup="myClientFunction()">Text Here</div>
将 <script>
标签添加到您的 HTML:
<script>
var valueToSend = code to get value;
window.myClientFunction = function() {
google.script.run
.myGsFunctionToMoveCursor(valueToSend);
};
</script>
那么你需要在脚本文件(.gs 扩展名)中有一个 myGsFunctionToMoveCursor()
函数
function myGsFunctionToMoveCursor(valueReceived) {
//To Do - Write code to move cursor in Google Doc
. . . Code to move cursor
};
我想在 Google Docs 文档的侧边栏中实现 Table 的目录,点击后会转到相应的部分。我正在逐个元素为侧边栏生成 HTML,我看到 Document
class 中有一个 moveCursor(position)
函数,但我看不出如何实际使用 onclick 调用它。不是完整的代码,但显示了问题:
function generateHtml() {
var html = HtmlService.createHtmlOutput('<html><body>');
var document = DocumentApp.getActiveDocument();
var body = document.getBody();
//Iterate each document element
var totalElements = body.getNumChildren();
for(var i = 0; i < totalElements; ++i) {
var element = body.getChild(i);
if(element.getType() == DocumentApp.ElementType.PARAGRAPH) {
var text = paragraph.getText();
if(text.trim()) { //Not blank paragraph
var position = document.newPosition(element, 0);
/**Would like to have <a onclick=document.moveCursor(position)> here**/
//Show first 20 chars as preview in table of contents
html.append('Detected paragraph ')
.append(text.substring(0, 20))
.append('<br />');
}
}
}
html.append('</body></html>');
return html;
}
如何在 Apps 脚本中完成此操作?代码可以根据需要完全重构。
这一行:
/**Would like to have <a onclick=document.moveCursor(position)> here**/
更改为:
<div onmouseup="myClientFunction()">Text Here</div>
将 <script>
标签添加到您的 HTML:
<script>
var valueToSend = code to get value;
window.myClientFunction = function() {
google.script.run
.myGsFunctionToMoveCursor(valueToSend);
};
</script>
那么你需要在脚本文件(.gs 扩展名)中有一个 myGsFunctionToMoveCursor()
函数
function myGsFunctionToMoveCursor(valueReceived) {
//To Do - Write code to move cursor in Google Doc
. . . Code to move cursor
};