gpt4 book ai didi

google-apps-script - 在前端为 Google Sheets 插件捕获 onSelectionChange

转载 作者:行者123 更新时间:2023-12-03 20:39:01 28 4
gpt4 key购买 nike

在 Google 表格中,我有一个由两部分组成的附加组件 code.gs和前端 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() };
}
这是 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>
虽然目前我正在通过单击按钮检查当前选择,但我需要 onSelectionChangeJs在工作表中更改选择时自动调用。
我相信这是对附加开发的普遍需求。那么有谁知道如何让 onSelectionChangecode.gs通知 onSelectionChangeJsindex.html ?

最佳答案

听起来就像当用户更改表格中的单元格时,您希望该更改反射(reflect)在 HTML 页面中,对吗?
什么不将该值发送到 HTML?我认为这需要刷新 HTML 页面,但您可以使用该新参数进行重建。我对附加组件了解得不够多。
但是,如果您只有 onSelectionChange() 触发 Logger.log。它可以触发一个函数,通过将 e 插入到 html id“formula”中来刷新 HTML。
不能直接用代码改变js,但是可以写code.gs,和html js做同样的事情。
这是在你需要的方向吗?

关于google-apps-script - 在前端为 Google Sheets 插件捕获 onSelectionChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67749373/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com