Javascript 当模式对话关闭时,如何在google插件的侧栏中加载特定部分?
我正在尝试在谷歌文档中创建一个附加组件。附加组件的一部分允许用户提供一些我在模态对话框中得到的输入。当用户保存更改时,我应该关闭对话框并根据用户输入更新侧边栏中的一个部分 我试图在PropertiesService中保存详细信息,然后在侧栏中加载这些详细信息Javascript 当模式对话关闭时,如何在google插件的侧栏中加载特定部分?,javascript,google-apps-script,google-docs,Javascript,Google Apps Script,Google Docs,我正在尝试在谷歌文档中创建一个附加组件。附加组件的一部分允许用户提供一些我在模态对话框中得到的输入。当用户保存更改时,我应该关闭对话框并根据用户输入更新侧边栏中的一个部分 我试图在PropertiesService中保存详细信息,然后在侧栏中加载这些详细信息 google.script.run.withSuccessHandler(function () { google.script.host.close(); }).sidebar_UI(); function sidebar_UI()
google.script.run.withSuccessHandler(function ()
{
google.script.host.close();
}).sidebar_UI();
function sidebar_UI()
{
var sidebar_UI = HtmlService.createTemplateFromFile("sidebar_UI").evaluate();
DocumentApp.getUi().showSidebar(sidebar_UI);
}
我很想知道,在用户详细信息存储在PropertiesService中之后,是否有办法不加载整个侧边栏,而是加载一个特定的部分。您必须在HTML文件中编写一些Javascript,以查询服务器代码,获取您想要显示的任何信息 基本上,您是在模态窗口中保存属性,然后从侧边栏检索该数据。我认为没有更干净的方法将数据从模式对话框传递到侧边栏 以下是一个例子: 代码.gs
function onOpen() {
SpreadsheetApp.getUi()
.createAddonMenu()
.addItem('Open Sidebar', 'openSidebar')
.addToUi();
}
function onInstall() {
onOpen();
}
function openSidebar() {
var html = HtmlService.createTemplateFromFile('Sidebar')
.evaluate();
SpreadsheetApp.getUi().showSidebar(html);
}
function showDialog() {
var html = HtmlService.createHtmlOutputFromFile('Modal')
.setWidth(400)
.setHeight(300);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showModalDialog(html, 'Modal');
}
function saveName(name) {
PropertiesService.getDocumentProperties().setProperty('STUDENT_NAME', name);
}
function getName() {
return PropertiesService.getDocumentProperties().getProperty('STUDENT_NAME');
}
Modal.html
<form onsubmit="submitName(event)">
<input type="text" name="studentName" />
<button type="submit">Submit</button>
</form>
<script>
function submitName(e) {
e.preventDefault();
var studentName = e.target.studentName.value;
google.script.run
.withSuccessHandler(google.script.host.close)
.saveName(studentName);
}
</script>
提交
函数submitName(e){
e、 预防默认值();
var studentName=e.target.studentName.value;
google.script.run
.withSuccessHandler(google.script.host.close)
.saveName(studentName);
}
Sidebar.html
<section>
<button onclick="showDialog()">Open Modal</button>
<button onclick="getName()">Update Name</button>
<h1>Name:</h1>
<div id="response">
<!-- the thing you are changing in the modal goes here -->
</div>
</section>
<script>
function showDialog() {
google.script.run
.withSuccessHandler(getName)
.showDialog();
}
function getName() {
google.script.run
.withSuccessHandler(function(res) {
console.log(res);
document.getElementById('response').innerHTML = res;
})
.getName();
}
</script>
开放模态
更新名称
姓名:
函数showDialog(){
google.script.run
.withSuccessHandler(getName)
.showDialog();
}
函数getName(){
google.script.run
.withSuccessHandler(函数(res){
控制台日志(res);
document.getElementById('response').innerHTML=res;
})
.getName();
}
如果我使用“DocumentApp.getUi().showSidebar(sidebar_UI.evaluate());”,不管怎样,如果我正确加载了整个侧边栏UI,我想加载一个特定的部分@jordanWell,你需要用模板化的HTML设置你的侧栏,这样它只显示你想要的部分。我想,你甚至可以创建几个不同的HTML文件,然后根据用户输入打开你需要的一个。我有一个侧栏,从中我打开一个模式对话框。一旦用户输入详细信息并点击保存按钮,模式对话框关闭,用户详细信息被存储。现在我不想重新加载整个侧边栏,而是只在旧侧边栏中重新加载一个特定部分,其中包含模态对话中保存的详细信息box@jordanTo在不重新加载的情况下更新侧栏,您只需在模式窗口的withSuccessHandler中调用另一个google.script.run.getSectionDetails()。