javascript - 满足特定条件时自动选中复选框

标签 javascript html google-apps-script

有没有办法在 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/

相关文章:

javascript - c++中的future是否对应于javascript中的promise?

javascript - 从数据表行中获取每条记录的属性值

php - 使用 <a> 元素使整个 block 可点击

html - css transform flip 在转换时禁用点击事件

javascript - 响应式容器,但图像不占满宽度

java - 在 javascript 中处理 POJO

html - 布局问题( float /填充)

javascript - 自动将 Google 表格中单元格内容大写的脚本?

google-apps-script - 谷歌应用程序脚本 : Unexpected error while getting the method or property openById on object SpreadsheetApp

authentication - 以 Google Apps super 管理员身份通过 API 为用户关闭两步验证