M-IT Support Computer Network

ส่งข้อมูลไปบันทึกยัง Google sheets ด้วย doPost

 https://dexlaos-it.blogspot.com/

ສົ່ງຂໍ້ມູນໄປ Google sheets ດ້ວຍ doPost


ສົ່ງຂໍ້ມູນ HTML ໄປບັນທຶກທີ່ Google sheet ໂດຍໃຊ້ doPost


1. ສ້າງ index.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>HTML form to Google Sheet</title>
</head>
 
<body>
    <h1 id="msg"></h1>
    <form>
        <input type="text" name="name" placeholder='Name'><br><br>
        <input type="email" name="email" placeholder='Email'><br><br>
        <input type="tel" name="phone" placeholder='Phone'><br><br>
        <input type="submit" id="sub">
    </form>
    <script>
        let form = document.querySelector("form");
        form.addEventListener('submit', (e) => {
            e.preventDefault();
            document.querySelector("#sub").value = "Submiting..";
            let data = new FormData(form);
            
            //Your_Api_EndPoint_Url
            fetch('Paste_Your_Api_EndPoint_Url', {
                    method: "POST",
                    body: data
                })
                .then(res => res.text())
                .then(data => {
                    document.querySelector("#msg").innerHTML = data;
                    document.querySelector("#sub").value = "Submit"
                });
        })
    </script>
</body>
 
</html>


2. ໄປທີ່ https://google.com/sheets ແລ້ວສ້າງຊີດຂື້ນມາ


3. ກົດທີ່ສ່ວນຂະຫຍາຍ ຈາກນັ້ນກົດໄປທີ່ Apps Script


4. ໂປຣເຈັກໃຫມ່ຈະເປີດຂື້ນມາ ວ່າງ code ດ້ານລາງ ແລ້ວກັບໄປຫນ້າຊີດ ຈາກນັ້ນຄັດລອກ url ຂອງຊີດ ນຳເອົາ URL ໄປວາງໃນໂຄດ SpreadsheetApp.openByUrl("xxx")

//Your spreadsheet url
const sheets = SpreadsheetApp.openByUrl("Paste your spreadsheet url here");

//if you have changed your sheet name then replace the below Sheet1 with your sheet name
const sheet = sheets.getSheetByName("Sheet1");

function doPost(e){
  let data = e.parameter;
  sheet.appendRow([data.name,data.email,data.phone]);
  return ContentService.createTextOutput("Success");
}


5. ກົດທີ່ การทำให้ใช้งานได้ ຈາກນັ້ນກົດທີ່ การทำให้ใช้งานได้รายการใหม่ 


6. ຫລັງຈາກ ดำเนินการเสร็จสิ้น ເຮົາຈະໄດ້ຮັບ URL ຂອງ API ຄັດລອກ URL ຈາກນັ້ນໄປທີ່ໂປຣແກຣມແກ້ໄຂ code ຂອງເຮົາ ແລະ ນຳ URL ວາງໃນ code fetch('xxx') ຂອງຟາຍ index.html 

แสดงความคิดเห็น (0)
ใหม่กว่า เก่ากว่า