Ram*_* ZK 5 google-sheets google-apps-script
在 Google 表格中,我有一个由 backendcode.gs和 frontend两部分组成的附加组件index.html,其中 index.html 是单击菜单时显示的侧边栏。这是code.gs:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('My add-on')
.addItem('Test', 'openSideBar')
.addToUi();
}
function onSelectionChange(e) {
Logger.log("Selection changed ");
}
function openSideBar() {
var html = HtmlService.createHtmlOutputFromFile('Index');
SpreadsheetApp.getUi().showSidebar(html);
}
function getCurrentSelection() {
var currentCell = SpreadsheetApp.getCurrentCell();
return { column: currentCell.getColumn(), row: currentCell.getRow() };
}
Run Code Online (Sandbox Code Playgroud)
这是onSelectionChange的文档。在Logger.log("Selection changed")当选择的是变化的确叫。但是,当用户单击单元格并更改选择时,我需要在前端通知或进行更改。
这是index.html:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Test
<input type="text" id="formula" />
<script>
function onSelectionChangeJs(e) {
document.getElementById("formula").value = e ;
}
function loadCell() {
google.script.run.withSuccessHandler(onSelectionChangeJs).getCurrentSelection();
}
</script>
<button onclick="loadCell()">Get cell</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
虽然目前我在单击按钮时检查当前选择,但我需要onSelectionChangeJs在工作表中的选择更改时自动调用。
我相信这是对附加开发的普遍需求。因此,没有人知道如何让onSelectionChange的code.gs通知onSelectionChangeJs的index.html?
小智 -1
听起来好像当用户更改表格中的单元格时,您希望该更改反映在 HTML 页面中,对吧?你不将该值发送到 HTML 吗?我认为这需要刷新 HTML 页面,但您可以使用该新参数进行重建。我对这个附加组件了解不够,无法多说。但是,如果您有 onSelectionChange() 仅触发 Logger.log。它可以触发一个函数,通过将 e 插入到 html id“formula”中来刷新 HTML。你不能直接用代码来改变js,但是你可以编写code.gs,与html js做同样的事情。这是否符合您需要的方向?