发送 "this" 单选按钮元素参数以在 Google Apps 脚本中运行

Sending "this" radio button element parameter to function in Google Apps script

我在 Google Sheet.

上有一个通过 Apps 脚本生成的自定义边栏

自定义侧边栏 html 包含带有 5 个单选输入元素的表单和字段集,全部带有 onchange 事件:

google.script.run.viewOptionsFormRadioChange(this);

这是完整的表格 HTML:

<form id="viewOptionsForm">
      <fieldset class="viewOptionsFieldset">
        <input type="radio" id="all" name="viewOptionsRadio" value="All"  checked="checked" onchange='google.script.run.viewOptionsFormRadioChange(this);' >
        <label for="all">All</label><br>
        <input type="radio" id="firstImport" name="viewOptionsRadio" value="First Import" onchange='google.script.run.viewOptionsFormRadioChange(this);'>
        <label for="firstImport">First Import</label><br>
        <input type="radio" id="secondImport" name="viewOptionsRadio" value="Second Import" onchange='google.script.run.viewOptionsFormRadioChange(this);'>
        <label for="secondImport">Second Import</label><br>
        <input type="radio" id="compositionLinking" name="viewOptionsRadio" value="Composition Linking" onchange='google.script.run.viewOptionsFormRadioChange(this);'>
        <label for="compositionLinking">Composition Linking</label><br>
        <input type="radio" id="underTheHood" name="viewOptionsRadio" value="Under the Hood" onchange='google.script.run.viewOptionsFormRadioChange(this);'>
        <label for="underTheHood">Under the Hood</label>
      </fieldset>
    </form>

然后我有一个函数“viewOptionsFormRadioChange”:

function viewOptionsFormRadioChange(checkbox) {

    if(checkbox.checked == true){
      if(checkbox.attr('id') == "underTheHood") {
        SpreadsheetApp.getActive().toast("underTheHood Selected", "Title", 15);
      }
    }
}

我正在尝试获取它,以便在我 select Under the Hood 单选按钮时出现 toast 通知,但是“this”元素参数似乎没有作为参数变量出现:复选框,因为上面的方法不起作用。

有什么地方出错了吗?

谢谢大家

我认为在您的脚本中,将 this 修改为 this.valuethis.id 怎么样?看来在这种情况下,this是无法解析的。那么,当你的脚本修改后,下面的修改怎么样?

修改后的脚本:

这次修改,你的HTML被修改了

<form id="viewOptionsForm">
  <fieldset class="viewOptionsFieldset">
    <input type="radio" id="all" name="viewOptionsRadio" value="All"  checked="checked" onchange='google.script.run.viewOptionsFormRadioChange(this.value);' >
    <label for="all">All</label><br>
    <input type="radio" id="firstImport" name="viewOptionsRadio" value="First Import" onchange='google.script.run.viewOptionsFormRadioChange(this.value);'>
    <label for="firstImport">First Import</label><br>
    <input type="radio" id="secondImport" name="viewOptionsRadio" value="Second Import" onchange='google.script.run.viewOptionsFormRadioChange(this.value);'>
    <label for="secondImport">Second Import</label><br>
    <input type="radio" id="compositionLinking" name="viewOptionsRadio" value="Composition Linking" onchange='google.script.run.viewOptionsFormRadioChange(this.value);'>
    <label for="compositionLinking">Composition Linking</label><br>
    <input type="radio" id="underTheHood" name="viewOptionsRadio" value="Under the Hood" onchange='google.script.run.viewOptionsFormRadioChange(this.value);'>
    <label for="underTheHood">Under the Hood</label>
  </fieldset>
</form>

但是,在这种情况下,返回值是像First ImportSecond Import、、、这样的字符串值。因此,您的 Google Apps 脚本可能需要进行如下修改。

function viewOptionsFormRadioChange(checkbox) {
  if(checkbox == "Under the Hood") {
    SpreadsheetApp.getActive().toast("underTheHood Selected", "Title", 15);
  }
}

通过此修改,当您选中“Under the Hood”时,toast() 为 运行。

注:

  • 如果要使用id的值,请将this.value修改为this.id。这样,您就可以在 HTML 标签中使用 ID 的值。就像 if(checkbox == "underTheHood") {,,,} 而不是 if(checkbox == "Under the Hood") {,,,}.