Google 应用程序脚本无法设置表单中的值 table

Google apps script canot set values from form table

我有一个 html 形式,例如

<script>
function formSubmit() {
    google.script.run.getValuesFromForm(document.forms[0]);
}
</script>
    <form>
        <table border="1" cellpadding="1" cellspacing="1" style="width: 500px;">
            <tbody>
                <tr>
                    <td>
                        <input id="date" name="date" style="width:125px;" type="date" />
                    </td>
                    <td>
                        <input id="type" name="type" style="width:125px;" type="text" />
                    </td>
                    <td>
                        <input id="status" name="status" style="width:125px;" type="text" />
                    </td>
                    <td>
                        <input id="name" name="name" style="width:125px;" type="text" />
                    </td>
                    <td>
                        <input id="comment" name="comment" style="width:125px;" type="text" />
                    </td>
                    <td>
                        <input id="where" name="where" style="width:125px;" type="text" />
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
 <input onclick="formSubmit()" type="button" value="Submit" />

并尝试将所有数据放入 sheet 但我得到空单元格,如果我在变量

的末尾添加 .value

form.date.value

然后行根本不插入

function getValuesFromForm(form) {
    var s1 = form.date,
        s2 = form.type,
        s3 = form.status,
        s4 = form.name,
        s5 = form.comment,
        s6 = form.where,
        index = 2,
        sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();         

    sheet.insertRowBefore(index).getRange(index, 1).setValues([s1,s2,s3,s4,s5,s6]); 
}

但是这段代码也可以工作

sheet.appendRow([s1,s2,s3,s4,s5,s6]);

我做错了什么?

您需要读取 value 属性 个输入元素:

var s1 = form.date.value,
    s2 = form.type.value,
    s3 = form.status.value,
    s4 = form.name.value,
    s5 = form.comment.value,
    s6 = form.where.value,

您的 HTML 中必须有一个 google.script.run 调用服务器端代码。

https://developers.google.com/apps-script/guides/html/reference/run

google.script.run 是一个 API,它使您能够从用户计算机与 Google 的服务器进行通信。

我在您的代码中没有看到 submit 按钮,没有看到 <script> 标签,也没有看到 onClick 事件。有多种配置表单提交的方法,但需要调用 google.script.run.myServerFunctionName() 来触发 .gs Apps 脚本文件中的函数。

您可以直接从“提交”按钮使用 google.script.run

<input type="button" value="Not Clicked"
    onclick="google.script.run
      .withSuccessHandler(updateButton)
      .withUserObject(this)
      .getEmail()" />

,或具有 HTML onclick 事件属性的按钮:

<button onclick="myFunction()">Click me</button>

或 HTML onsubmit 事件属性:

<form onsubmit="myFunction()">
  Enter name: <input type="text">
  <input type="submit">
</form>

来自 SCRIPT 标签:

<script>
  function myFunction() {
    google.script.run.doSomething();
  };
</script>

您可以通过多种方式传递数据。有很多方法可以配置 HTML 和客户端代码。

已解决

var rowData=[[s1,s2,s3,s4,s5,s6]];
sheet.insertRowBefore(index).getRange(index, 1,1,6).setValues(rowData);