Google应用脚本无法从表单表中设置值

时间:2015-02-03 15:26:58

标签: javascript google-apps-script google-sheets

我有一个类似

的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" />

并尝试将所有数据放到工作表但我得到空单元格,如果我在变量末尾添加.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]);

我做错了什么?

3 个答案:

答案 0 :(得分:1)

您需要阅读输入元素的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,

答案 1 :(得分:1)

您的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脚本文件中触发该功能。

您可以直接从SUBMIT按钮使用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和客户端代码。

答案 2 :(得分:1)

解决

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