Dmitrij Holkin
Dmitrij Holkin

Reputation: 2055

Google apps script canot set values from form table

I have a html form like

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

and trying to put all data to sheet but i got empty cells, if i add .value on the end of variables

form.date.value

then row do not inserting at all

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]); 
}

But this code works as well

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

What i doing wrong?

Upvotes: 1

Views: 1437

Answers (3)

Dmitrij Holkin
Dmitrij Holkin

Reputation: 2055

Solved

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

Upvotes: 1

Alan Wells
Alan Wells

Reputation: 31300

You must have a google.script.run in your HTML somewhere that calls the server side code.

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

google.script.run is an API that enables you to communicate from the users computer, to Google's servers.

I don't see a submit button in your code, or a <script> tag, or an onClick event. There are various ways to configure the form submission, but there needs to be a google.script.run.myServerFunctionName() call to trigger the function in the .gs Apps Script file.

You can use google.script.run directly from a SUBMIT button:

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

, or an button with an HTML onclick Event Attribute:

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

or a HTML onsubmit Event Attribute:

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

and from a SCRIPT tag:

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

You can pass the data in various ways. There are lots of ways to configure the HTML and client side code.

Upvotes: 1

dfsq
dfsq

Reputation: 193261

You need to read value property of input elements:

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,

Upvotes: 1

Related Questions