使用复选框和按钮向文档添加文本?
Adding text to document using checkboxes and buttons?
我正在为 google 文档(类似文字)创建应用程序脚本插件,我创建了一个带有复选框和按钮的侧边栏,并希望在单击后将特定文本打印到文档中按钮并根据我突出显示的复选框,到目前为止,我设法使用 Html 组件创建了侧边栏,但我无法打印文档中的文本。
<link href="https://ssl.gstatic.com/docs/script/css/add-ons.css" rel="stylesheet">
<div class="sidebar">
<div class="elements">
<input type="checkbox" id="myCheck1">Check Box 1<br>
<input type="checkbox" id="myCheck2">Check Box 2<br><br>
<button class="blue" id="process" onclick="myFunction()">Print</button><br><br>
<p id="text1" style="display:none">Checkbox1 is CHECKED!</p>
<p id="text2" style="display:none">Checkbox2 is CHECKED!</p>
</div>
</div>
<script>
function myFunction() {
var checkBox1 = document.getElementById("myCheck1");
var checkBox2 = document.getElementById("myCheck2");
var text = document.getElementById("text1");
var text = document.getElementById("text2");
if (checkBox1.checked == true){
text1.style.display = "block";
} else {
text1.style.display = "none";
}
if (checkBox2.checked == true){
text2.style.display = "block";
} else {
text2.style.display = "none";
}
}
</script>
这是 Html 我为侧边栏准备的并尝试了一个我在网上找到的示例来打印文本,但这将在按钮下方的侧边栏上打印文本,而不是在文档上。
function onInstall() {
onOpen();
}
function onOpen() {
DocumentApp.getUi()
.createAddonMenu() // Add a new option in the Google Docs Add-ons Menu
.addItem("KO Addon", "showSidebar")
.addToUi(); // Run the showSidebar function when someone clicks the menu
}
function showSidebar() {
var html = HtmlService.createTemplateFromFile("KO")
.evaluate()
.setTitle("KO Addon Options"); // The title shows in the sidebar
DocumentApp.getUi().showSidebar(html);
}
这是将侧边栏添加到文档的主要代码,我只能使用主要代码在文档上打印内容,但我不知道如何让它们一起工作,任何帮助将不胜感激,
谢谢。
- 您想将从 HTML 端检索到的值放入 Google 文档。
如果我对你的问题的理解是正确的,这个样本怎么样?在您的情况下,当 HTML 端的值发送到 Google Apps 脚本时,您可以使用 google.script.run
.
来实现它
示例脚本:
HTML 方:
请在HTML端的myFunction()
末尾添加以下脚本。
google.script.run.putValues({checkBox1: checkBox1.checked, checkBox2: checkBox2.checked});
气体侧:
请将以下功能添加到 Google Apps 脚本。
function putValues(obj) {
var body = DocumentApp.getActiveDocument().getBody();
body.appendParagraph(JSON.stringify(obj));
}
注:
- 在此示例中,它假设您的脚本在 Google 文档上显示侧边栏并且它工作正常。
- 修改脚本后,请打开侧边栏。单击按钮时,复选框的值将发送到 Google Apps 脚本并将值放入活动文档。
- 这是一个简单的示例脚本。所以请根据自己的情况修改。
参考文献:
如果我误解了你的问题,请告诉我。我想修改一下。
我正在为 google 文档(类似文字)创建应用程序脚本插件,我创建了一个带有复选框和按钮的侧边栏,并希望在单击后将特定文本打印到文档中按钮并根据我突出显示的复选框,到目前为止,我设法使用 Html 组件创建了侧边栏,但我无法打印文档中的文本。
<link href="https://ssl.gstatic.com/docs/script/css/add-ons.css" rel="stylesheet">
<div class="sidebar">
<div class="elements">
<input type="checkbox" id="myCheck1">Check Box 1<br>
<input type="checkbox" id="myCheck2">Check Box 2<br><br>
<button class="blue" id="process" onclick="myFunction()">Print</button><br><br>
<p id="text1" style="display:none">Checkbox1 is CHECKED!</p>
<p id="text2" style="display:none">Checkbox2 is CHECKED!</p>
</div>
</div>
<script>
function myFunction() {
var checkBox1 = document.getElementById("myCheck1");
var checkBox2 = document.getElementById("myCheck2");
var text = document.getElementById("text1");
var text = document.getElementById("text2");
if (checkBox1.checked == true){
text1.style.display = "block";
} else {
text1.style.display = "none";
}
if (checkBox2.checked == true){
text2.style.display = "block";
} else {
text2.style.display = "none";
}
}
</script>
这是 Html 我为侧边栏准备的并尝试了一个我在网上找到的示例来打印文本,但这将在按钮下方的侧边栏上打印文本,而不是在文档上。
function onInstall() {
onOpen();
}
function onOpen() {
DocumentApp.getUi()
.createAddonMenu() // Add a new option in the Google Docs Add-ons Menu
.addItem("KO Addon", "showSidebar")
.addToUi(); // Run the showSidebar function when someone clicks the menu
}
function showSidebar() {
var html = HtmlService.createTemplateFromFile("KO")
.evaluate()
.setTitle("KO Addon Options"); // The title shows in the sidebar
DocumentApp.getUi().showSidebar(html);
}
这是将侧边栏添加到文档的主要代码,我只能使用主要代码在文档上打印内容,但我不知道如何让它们一起工作,任何帮助将不胜感激,
谢谢。
- 您想将从 HTML 端检索到的值放入 Google 文档。
如果我对你的问题的理解是正确的,这个样本怎么样?在您的情况下,当 HTML 端的值发送到 Google Apps 脚本时,您可以使用 google.script.run
.
示例脚本:
HTML 方:请在HTML端的myFunction()
末尾添加以下脚本。
google.script.run.putValues({checkBox1: checkBox1.checked, checkBox2: checkBox2.checked});
气体侧:
请将以下功能添加到 Google Apps 脚本。
function putValues(obj) {
var body = DocumentApp.getActiveDocument().getBody();
body.appendParagraph(JSON.stringify(obj));
}
注:
- 在此示例中,它假设您的脚本在 Google 文档上显示侧边栏并且它工作正常。
- 修改脚本后,请打开侧边栏。单击按钮时,复选框的值将发送到 Google Apps 脚本并将值放入活动文档。
- 这是一个简单的示例脚本。所以请根据自己的情况修改。
参考文献:
如果我误解了你的问题,请告诉我。我想修改一下。