Google apps script 如何将GoogleSheets侧边栏输入到服务器
我正在创建一个对话框来显示和编辑谷歌电子表格边栏中的文档属性 我设法在侧边栏中显示属性值,甚至创建文本输入字段,以便通过侧边栏进行交互。但最后,我无法使用serverHandler将值返回到服务器端脚本中Google apps script 如何将GoogleSheets侧边栏输入到服务器,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我正在创建一个对话框来显示和编辑谷歌电子表格边栏中的文档属性 我设法在侧边栏中显示属性值,甚至创建文本输入字段,以便通过侧边栏进行交互。但最后,我无法使用serverHandler将值返回到服务器端脚本中 Google Sheets如何支持带有侧边栏的交互式对话框?你没有说,但听起来你在侧边栏上使用UiApp serverHandler将接收处理程序附加到的元素的值。例如: var button = uiInstance.createSubmitButton("Submit"); var ser
Google Sheets如何支持带有侧边栏的交互式对话框?你没有说,但听起来你在侧边栏上使用UiApp serverHandler将接收处理程序附加到的元素的值。例如:
var button = uiInstance.createSubmitButton("Submit");
var servHandler = uiInstance.createServerHandler("myServerHandler");
button.addClickHandler(servHandler);
当按下按钮而调用myServerHandler()
时,它将在事件对象中接收button
的值
{"parameter":
{"clientY":"13",
"clientX":"40",
"eventType":"click", <<<<<<<<<<<<<<<<<<<<<
"ctrl":"false",
"meta":"false",
"source":"u503258948978",
"button":"1", <<<<<<<<<<<<<<<<<<<<<
"alt":"false",
"screenY":"207",
"screenX":"1020",
"shift":"false",
"y":"13",
"x":"40"}
}
现在,我们还将收到文本框clientInput
的输入,作为名为参数[“客户端输入”]
的属性
谢谢你的帮助。代码运行良好,是进一步探索使用边栏可能实现的功能的良好起点。我还想做的是:-将代码放入一个.lib文件中,这样我就可以在电子表格和文档中使用它-使用HtmlService.createTemplatefromfile顺便提一下:我提出了我最初的问题,因为我试图使用我在中找到的边栏翻译功能,该功能与文档边栏(DocumentApp)一起使用,但与电子表格边栏不一起使用(电子表格应用程序)。
var clientInput = uiInstance.createTextBox().setName("client-input");
...
servHandler.addCallbackElement(clientInput);
{"parameter":
{"clientY":"13",
"clientX":"40",
"eventType":"click",
"ctrl":"false",
"client-input":"hello", <<<<<<<<<<<<<<<
"meta":"false",
"source":"u503258948978",
"button":"1",
"alt":"false",
"screenY":"207",
"screenX":"1020",
"shift":"false",
"y":"13",
"x":"40"}
}
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.createMenu('Custom Menu')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
/**
* Display a sidebar with a button, an input box, and a
* label. The label is initially hidden.
*/
function showSidebar() {
var uiInstance = UiApp.createApplication()
.setTitle('My custom sidebar')
.setWidth(300);
// Create the widgets to be used on sidebar
var clientInput = uiInstance.createTextBox().setName("client-input");
var serverOutput = uiInstance.createLabel().setId("server-response").setVisible(false);
var button = uiInstance.createSubmitButton("Submit");
// Set up server handler, add callbackElement(s), and attach to button
var servHandler = uiInstance.createServerHandler("myServerHandler");
servHandler.addCallbackElement(clientInput);
button.addClickHandler(servHandler);
// Place our widgets on the sidebar
uiInstance.add(button).add(clientInput).add(serverOutput);
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.showSidebar(uiInstance);
}
/**
* Server handler - get the value of "client-input",
* convert it to upper case, then display in the
* Label in the UI.
*/
function myServerHandler(e) {
Logger.log(JSON.stringify(e, undefined, 2)); // Log the event object
var app = UiApp.getActiveApplication();
var text = e.parameter["client-input"];
var upperText = text.toUpperCase();
app.getElementById("server-response").setVisible(true).setText(upperText);
return(app);
}