【发布时间】:2022-08-15 05:28:50
【问题描述】:
我正在为谷歌表格制作侧栏。我有一个 html 页面。我发送的数据分为两部分。我有帐户,每个帐户都有属性。
{
\"name\": \"accountSummaries/223471136\",
\"account\": \"accounts/223471136\",
\"displayName\": \"G4MeasurmentTesting\",
\"propertySummaries\": [
{
\"property\": \"properties/307799514\",
\"displayName\": \"Ga4CsharpTesting\",
\"propertyType\": \"PROPERTY_TYPE_ORDINARY\",
\"parent\": \"accounts/223471136\"
}
]
},
我想要做的是在我的菜单中显示两个下拉菜单,第一个是帐户,第二个是帐户下的属性。
我的问题是当用户选择第一个选择时如何重新加载第二个选择?
我给第一个选择了一个名称 <select id=\"account\" name=\"account\"> 然后尝试使用 accounts[account.selected].propertySummaries.length; 为第二个加载数据
它不起作用,因为我看到ReferenceError: account is not defined 这对我来说意味着它没有看到选择的名称。
必须有一种方法来检测在第一个选择中选择了哪个项目对吗?
<!DOCTYPE html>
<html>
<head>
<base target=\"_top\">
</head>
<body>
<? var accounts = listAccounts(); ?>
<? const item = accounts[1];?>
<label>Account: </label>
<select id=\"account\" name=\"account\">
<option></option>
<? for (var i = 0; i < accounts.length; i++) { ?>
<option value=\"<?= i ?>\"><?= accounts[i].displayName ?></option>
<? } ?>
</select>
<!-- Not working -->
<label>property: </label>
<select id=\"view\" name=\"view\">
<option></option>
<? for (var i = 0; i < accounts[account.selected].propertySummaries.length; i++) { ?>
<option><?= accounts[account.selected].propertySummaries[i].displayName ?></option>
<? } ?>
</select>
<!-- Not working -->
<?= accounts ?> <input type=\"button\" value=\"Close\" onclick=\"google.script.host.close()\" />
</body>
</html>
上面的代码给出了一个错误ReferenceError: account is not defined
代码.gs
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu(\'GA Menu\')
.addItem(\'Alert Test\', \'ShowMessage\')
.addSeparator()
.addSubMenu(ui.createMenu(\'Sub-menu\')
.addItem(\'Second item\', \'ShowPrompt\'))
.addItem(\'Show sidebar\', \'showSidebar\')
.addToUi();
}
// https://developers.google.com/apps-script/guides/dialogs?hl=en#code.gs_1
function showSidebar() {
var html = HtmlService
.createTemplateFromFile(\'Page\')
.evaluate();
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showSidebar(html);
}
如果我通过指定说 0 来强制它,它确实有效,所以问题不在于它能够读取我的数据。因此,问题中的数据表单不是问题,而是链接两个选择。
<label>property: </label>
<select id=\"view\" name=\"view\">
<? for (var i = 0; i < accounts[0].propertySummaries.length; i++) { ?>
<option><?= accounts[0].propertySummaries[i].displayName ?></option>
<? } ?>
</select>
更新尝试 2
另一个想法是尝试使用文档来抓取它
<? const account = document.getElementById(\'account\'); ?>
哪个也没用,它只是给了我
ReferenceError: document is not defined详情
-
您的代码引用了
account.selected,但我没有看到任何地方定义了account,就像错误所说的那样。 -
<select id=\"account\" name=\"account\"> <--- 它是我试图从中检测到的选择下拉列表的名称。我已经编辑了我的问题,以便更清楚我为解决这个问题所做的努力。
-
您只显示最初加载页面时运行的模板化脚本。你的
<script>在哪里显示事件处理程序document.getElementById(\"account\").addEventListener(\"change\", myFunction)?并显示您的服务器功能,该功能显示模板填写的侧边栏。 -
@TheWizEd 看到现在我们正在取得一些进展。我没有那个。我确实尝试了 document.getElementById,您可以在第二次更新中看到它,但也显示错误。添加code.gs检查编辑请
-
accounts在您的模板中是如何定义的?而<?= accounts[i].displayName ?>在evaluate之前需要在showSidebar中使用html.accounts = accounts;。给我一点时间,我会做一个模型,我会做什么。
标签: javascript google-apps-script