有没有办法在 Google 电子表格中满足特定条件时自动选中 html 中的复选框。
我有一个与单元格匹配的 JavaScript,如果其值为 1 或任何定义的条件,则应自动选中“name1”复选框。
这是我名为 code.gs 的文件:-
function openInputDialog() {
var html = HtmlService.createHtmlOutputFromFile('Index').setSandboxMode(HtmlService.SandboxMode.IFRAME);
SpreadsheetApp.getUi().showModalDialog(html, 'Add Item');
}
function itemAdd(form) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var match1 = sheet.getRange("B2").getValue();
if(match1 === "1")
{
autocheck the checkbox "name1"; //cant figure out what to use here
}
Logger.log(form);
}
我的 Index.html 文件如下所示:-
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<form id='myForm'>
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td></td>
<td><input type="checkbox" name="name1" id="name1" value="1"/>xyz</td>
<td><input type="checkbox" name="name2" id="name2"/>abc</td>
<td><input type="checkbox" name="name3" id="name3"/>qwe</td>
<td><input type="checkbox" name="name4" id="name4"/>zxc</td>
</tr>
</tbody>
</table>
<br><br>
<input type="button" value="Submit" onclick="google.script.run
.withSuccessHandler(google.script.host.close)
.itemAdd(this.parentNode)" />
</form>
</html>
最佳答案
您无法单独从 code.gs 中选中该复选框。请引用下面的代码。
代码.gs
function getValues(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var match1 = sheet.getRange("B2").getValue();
return match1;
}
index.html --- 在html页面中添加以下脚本
<script>
google.script.run.withSuccessHandler(checkDefault).getValues();
function checkDefault(val){
var checkBoxName = "name"+val;
document.getElementById(checkBoxName).checked = true;
}
</script>
关于javascript - 满足特定条件时自动选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45551594/