如何重定向到其他客户端 html 页面并从 code.gs 获取变量到 html 页面?

问题描述 投票:0回答:0

我有一个 html 表单,在提交表单时,它将重定向到我在 Google 应用程序脚本中添加的 (success.html) 页面,并将数据发送到 Google 工作表。 但是,现在我想从 Google 应用程序脚本中删除我的 success.html,并在 index.html(表单页面)的同一层次结构级别上添加为客户端。 这是我的代码: 成功.html

<!DOCTYPE html>
<html>
  <head>
    <title>Thank You!</title>
  </head>
  <body>
    <h1>Thank you for submitting the form!</h1>
    <h2><?= serialNumber ?></h2>
  </body>
</html>

Index.html

 <form id="form">
      <label for="name">Name:</label>
      <input name="name" id="name" type="text"><br><br>
      <label for="email">Email:</label>
      <input name="email" id="email" type="email"><br><br>
      
      <label for="uploadfile">File:</label>
      <input name="file" id="uploadfile" type="file"><br><br>
      <label for="filename">File Name:</label>
      <input name="filename" id="filename" type="text"><br><br>
      
      <input id="submit" type="submit">
    </form>
    <script>
      const form = document.getElementById('form');
      form.addEventListener('submit', e => {
        e.preventDefault();
        const name = form.name.value;
        const email = form.email.value;
        const file = form.file.files[0];
        if (!file) {
          const url = "https://script.google.com/macros/s/###/exec"; // Please replace this with your Web Apps URL.
          const qs = new URLSearchParams({filename: "", mimeType: "", name: name || "", email: email || ""});
          fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([])})
            .then(res => res.json())
            .then(e => {
              window.open(url, '_self');
            })
            .catch(err => console.log(err));
          return;
        }
        const fr = new FileReader();
        fr.readAsArrayBuffer(file);
        fr.onload = f => {
          const url = "https://script.google.com/macros/s/###/exec"; // Please replace this with your Web Apps URL.
          const qs = new URLSearchParams({filename: form.filename.value || file.name, mimeType: file.type, name: name || "", email: email || ""});
          fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([...new Int8Array(f.target.result)])})
            .then(res => res.json())
            .then(e => {
              window.open(url, '_self');
            })
            .catch(err => console.log(err));
        }
      });
    </script>

预期:在提交表单时,它应该重定向到客户端成功页面(例如;path=/success.html)。 并且还应实时捕获序列号。

注意:这个问题和我之前的问题有关。您可以参考here了解更多详情。

提前致谢!

google-apps-script google-sheets
© www.soinside.com 2019 - 2024. All rights reserved.