]> git.ardis.lu - google-forms-frontend/commitdiff
Initial commit
authorArdis Lu <ardisglu@gmail.com>
Sun, 19 Sep 2021 09:46:22 +0000 (02:46 -0700)
committerArdis Lu <ardisglu@gmail.com>
Sun, 19 Sep 2021 09:46:22 +0000 (02:46 -0700)
README.md [new file with mode: 0644]
favicon.ico [new file with mode: 0644]
github.svg [new file with mode: 0644]
index.html [new file with mode: 0644]

diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..cf5cc80
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+# google-forms-frontend
+
+Single page HTML frontend that posts form submissions directly to a Google Form, which in turn feeds into a Google Sheet. Intended to demonstrate the minimal amount of code necessary to create a frontend on top of a Google Form.
diff --git a/favicon.ico b/favicon.ico
new file mode 100644 (file)
index 0000000..a34cb40
Binary files /dev/null and b/favicon.ico differ
diff --git a/github.svg b/github.svg
new file mode 100644 (file)
index 0000000..5bd3a0e
--- /dev/null
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24" height="24" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" style="-ms-transform: rotate(360deg); -webkit-transform: rotate(360deg); transform: rotate(360deg);"><path d="M12 2A10 10 0 0 0 2 12c0 4.42 2.87 8.17 6.84 9.5c.5.08.66-.23.66-.5v-1.69c-2.77.6-3.36-1.34-3.36-1.34c-.46-1.16-1.11-1.47-1.11-1.47c-.91-.62.07-.6.07-.6c1 .07 1.53 1.03 1.53 1.03c.87 1.52 2.34 1.07 2.91.83c.09-.65.35-1.09.63-1.34c-2.22-.25-4.55-1.11-4.55-4.92c0-1.11.38-2 1.03-2.71c-.1-.25-.45-1.29.1-2.64c0 0 .84-.27 2.75 1.02c.79-.22 1.65-.33 2.5-.33c.85 0 1.71.11 2.5.33c1.91-1.29 2.75-1.02 2.75-1.02c.55 1.35.2 2.39.1 2.64c.65.71 1.03 1.6 1.03 2.71c0 3.82-2.34 4.66-4.57 4.91c.36.31.69.92.69 1.85V21c0 .27.16.59.67.5C19.14 20.16 22 16.42 22 12A10 10 0 0 0 12 2z" fill="currentColor"/><rect x="0" y="0" width="24" height="24" fill="rgba(0, 0, 0, 0)" /></svg>
\ No newline at end of file
diff --git a/index.html b/index.html
new file mode 100644 (file)
index 0000000..aae44da
--- /dev/null
@@ -0,0 +1,125 @@
+<!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">
+  <style type="text/css">
+    body {
+      font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Roboto,
+        "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", Helvetica, Arial,
+        sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
+      color: hsl(0deg 0% 15%);
+      background: hsl(0deg 0% 90%);
+    }
+
+    header {
+      display: flex;
+      align-items: center;
+      justify-content: center;
+    }
+
+    main {
+      max-width: 720px;
+      margin: 0 auto;
+    }
+
+    form {
+      display: flex;
+      flex-wrap: wrap;
+    }
+
+    label,
+    input,
+    textarea {
+      flex: 100%;
+    }
+
+    input,
+    textarea {
+      margin-block-end: 1rem;
+    }
+
+    button {
+      margin-inline-end: 1rem;
+    }
+  </style>
+  <title>google-forms-frontend</title>
+</head>
+
+<body>
+  <header>
+    <h1>google-forms-frontend</h1>
+    <a href="https://github.com/ardislu/google-forms-frontend">
+      <img src="/github.svg" width="24" height="24" alt="GitHub" />
+    </a>
+  </header>
+
+  <main>
+    <form action="https://docs.google.com/forms/u/0/d/e/1FAIpQLSclZ7yXg5kNXTlJUKDNGMCGR9vvgKqzyUB-HfLiwc_sveTxEA/formResponse">
+      <label for="entry.185031848">Example short answer:</label>
+      <input type="text" name="entry.185031848">
+
+      <label for="entry.1096192054">Example long answer:</label>
+      <textarea name="entry.1096192054"></textarea>
+
+      <label for="entry.552287623">Multiple choice option:</label>
+      <input list="multiple-choice" name="entry.552287623">
+      <datalist id="multiple-choice">
+        <option value="Multiple choice option 1">
+        <option value="Multiple choice option 2">
+        <option value="Multiple choice option 3">
+        <option value="Multiple choice option 4">
+        <option value="Multiple choice option 5">
+      </datalist>
+
+      <label for="entry.2010116811">Date:</label>
+      <input type="date" name="entry.2010116811">
+
+      <label for="entry.1500214518">Time:</label>
+      <input type="time" name="entry.1500214518">
+
+      <button type="submit">Submit</button>
+      <output></output>
+    </form>
+  </main>
+
+  <script>
+    const form = document.querySelector('form');
+    const button = document.querySelector('button');
+    const output = document.querySelector('output');
+
+    form.addEventListener('submit', async event => {
+      event.preventDefault();
+      
+      const short = form['entry.185031848'];
+      const long = form['entry.1096192054'];
+      const multiple = form['entry.552287623'];
+      const date = form['entry.2010116811'];
+      const dateValue = new Date(`${date.value} UTC`); // UTC prevents later functions from shifting values to match local timezone
+      const time = form['entry.1500214518'];
+      const timeValue = new Date(`January 1, 1970 ${form['entry.1500214518'].value} UTC`); // Date value can be any arbitrary date, but it's required. UTC prevents later functions from shifting values to match local timezone
+
+      const headers = {
+        'Content-Type': 'application/x-www-form-urlencoded'
+      }
+
+      let body = '';
+      body += `${short.name}=${encodeURIComponent(short.value)}&`;
+      body += `${long.name}=${encodeURIComponent(long.value)}&`;
+      body += `${multiple.name}=${encodeURIComponent(multiple.value)}&`;
+      body += `${date.name}_year=${dateValue.getUTCFullYear()}&`;
+      body += `${date.name}_month=${dateValue.getUTCMonth() + 1}&`; // Google Forms requires 1-12, but getMonth is 0-11
+      body += `${date.name}_day=${dateValue.getUTCDate()}&`;
+      body += `${time.name}_hour=${timeValue.getUTCHours().toString().padStart(2, '0')}&`; // Google Forms requires 2 digits, but getHours does not include leading 0
+      body += `${time.name}_minute=${timeValue.getUTCMinutes().toString().padStart(2, '0')}`; // Google Forms requires 2 digits, but getMinutes does not include leading 0. Last argument must not have trailing &
+
+      await fetch(form.action, { method: 'POST', headers: headers, body: body, mode: 'no-cors' }); // Can't set CORS headers for Google Forms, so can't access the Response. But Request will still be sent.
+      button.disabled = true;
+      output.textContent = '✔ Form submitted!' // Assume the response was successful
+    })
+  </script>
+</body>
+
+</html>
\ No newline at end of file
This page took 0.035606 seconds and 4 git commands to generate.