【问题标题】:How to send HTML form data to a google sheet?如何将 HTML 表单数据发送到谷歌表格?
【发布时间】:2021-09-14 18:08:25
【问题描述】:

我正在尝试将 HTML 表单数据发送到谷歌表格。我已经遵循了几个教程,但无法将表单数据发送到谷歌表格。我成功部署了 google 脚本,复制了链接并链接到 HTML,但没有结果。实际上我在这里缺少什么? 寻求您的指导。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Form</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

</head>

<body>
    <nav class="navbar navbar-light bg-light">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">
                <img src="https://www.freeiconspng.com/uploads/tumblr-logo-icon-9.png" alt="" width="30" height="24" class="d-inline-block align-text-top"> Test Form
            </a>
        </div>
    </nav>

    <div class="container">

        <form class="row g-3" method="POST" autocomplete="off" name="test_form">

            <select class="form-select form-select-lg mb-3" name="title" aria-label=".form-select-lg example">
                <option selected>Open this select menu</option>
                <option value="1">Mr.</option>
                <option value="2">Miss</option>
                <option value="3">Mrs.</option>
            </select>

            <div class="container">
                <div class="row g-2">
                    <div class="col-6">
                        <div class="col">
                            <input type="text" class="form-control" name="firstname" placeholder="First name" aria-label="First name">
                        </div>
                    </div>
                    <div class="col-6">
                        <div class="col">
                            <input type="text" class="form-control" name="lastname" placeholder="Last name" aria-label="Last name">
                        </div>
                    </div>
                    <div class="col-6">
                        <div class="col">
                            <input type="email" class="form-control" name="email" placeholder="Email" aria-label="Email">
                        </div>
                    </div>
                    <div class="col-6">
                        <div class="col">
                            <input type="text" class="form-control" name="telephone" placeholder="Telephone" aria-label="Telephone">
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-floating">
                <textarea class="form-control" name="comment" placeholder="Leave a comment here" id="floatingTextarea2" style="height: 100px"></textarea>
                <label for="floatingTextarea2">Comments</label>
            </div>

            <div class="col-12">
                <button class="btn btn-primary" type="submit" name="submit" value="send message">Submit form</button>
            </div>
        </form>

        <script type="text/javascript">
            const scriptURL = 'https://script.google.com/macros/s/AKfycbyWrl6cQr8C6PatE2tTZ3kJWi0lInw4rgu4nRBB8AqY3X7GDJ_6387yOtqsBJSjdV96og/exec'
            const form = document.forms['google-sheet']

            form.addEventListener('submit', e => {
                e.preventDefault()
                fetch(scriptURL, {
                        method: 'POST',
                        body: new FormData(form)
                    })
                    .then(response => alert("Thanks for Contacting us..! We Will Contact You Soon..."))
                    .catch(error => console.error('Error!', error.message))
            })
        </script>
        <!-- <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script> -->
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>
 
    </div>
</body>

</html>

【问题讨论】:

标签: html forms google-apps-script google-sheets


【解决方案1】:

您可以从here学习教程。

请务必谨慎添加Add a new project triggerPublish the project as a web appInput your web app URL

【讨论】:

    猜你喜欢
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 2022-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多