简体   繁体   中英

Submit form data into google spreadsheet

I am trying to add some data in form of html form table into a spreadsheet by matching some values.

But somehow Submit button does nothing.

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 lastRow = sheet.getLastRow();
    var match1 = sheet.getRange("B2").getValue();


    for (var i = 0; i = 300; i++)

    {
        if (lastRow - i == match1) {
            sheet.getRange(lastRow - i, 2, 1, 1).setValues(form.details);
            break;
        }

    }
}

index.Html ----->

<!DOCTYPE html>
<html>

<head>
    <base target="_top">
</head>
<br>
<form>
    Details:<br>
    <input type="text" name="details">
    <br> Quantity:
    <br>
    <input type="text" name="quantity">
    <br><br>
    <input type="button" value="Submit" onclick="google.script.run
            .withSuccessHandler(google.script.host.close)
            .itemAdd(this.parentNode)" />
</form>

</html>

I would really appreciate your help.

I think this will work for you.

<!DOCTYPE html>
<html>

<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
    function localItemAdd()
    {
       var i1=$('#d').val();//it is helpful to use intermediate values when debugging new code
       var i2=$('#q').val();
       var A={};
       A['details']=i1;
       A['quantity']=i2;
       google.script.run.itemAdd(A);
    }
    console.log('My Code'); //this make it easier to find your code in the debugger console
    </script>
</head>
<body>

    Details:<br><input id="d" type="text" name="details" />
    <br> Quantity:<br><input id="q" type="text" name="quantity" />
    <br><br><input type="button" value="Submit" onclick="localItemAdd();google.script.host.close();" />
</body>
</html>

Try this....Replace the itemAdd function

function itemAdd(form) {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName("Sheet1");
    var lastRow = sheet.getLastRow();
    var match1 = sheet.getRange("B2").getValue();

    var dataRange = sheet.getRange(1, 1,lastRow).getValues();
    for(var i=0;i<dataRange.length;i++){
        if(dataRange[i][0] == match1){
            sheet.getRange(i+1, 2).setValue(form.details)
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM