1

I've created a html form & used Google apps scripts url to capture html form details to Google sheets. I've got the source code from Here modified accordingly. So, on html from submit it's successfully capturing drive link to Google sheets. Finally in above source code it's return output in JSON format in console log. But instead of JSON on submit i need output to return normal text eg:return ContentService.createTextOutput("formSubmitted Successfully");

note: this is my first question so please excuse if format is wrong.

Expected output: Instead of returning JSON in console, need to redirect user to another html success page(i.e, added in Google apps script).

Thank you in advance

Sandeep
  • 15
  • 6

1 Answers1

0

From your question, if you are using my sample script of my answer, how about the following modification?

From:

fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([...new Int8Array(f.target.result)])})
.then(res => res.json())
.then(e => console.log(e))  // <--- You can retrieve the returned value here.
.catch(err => console.log(err));

To:

fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([...new Int8Array(f.target.result)])})
.then(res => res.json())
.then(e => {
  window.open('https://###', '_self');
})
.catch(err => console.log(err));
  • Please set your redirect URL to https://### of window.open('https://###', '_self');.

Added:

From your following reply,

In Google apps script I've a 2 files one is code.gs and success.html. Now I want return user to success.html (which is in apps script). So, in this scenario how can I achieve this?

I've having hosted the html form. But you can just save it in html format and open locally.

When your provided script is used, please modify as follows.

Google Apps Script side:

Please add the following function to Google Apps Script. In this case, doPost is not modified. From your reply, it supposes that your HTML filename in your Google Apps Script project is success.html. Please confirm the filename again. When it is different, please modify the below script.

function doGet() {
  return HtmlService.createHtmlOutputFromFile("success.html").setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

HTML&Javascript side:

Please modify your HTML and Javascript as follows.

<!DOCTYPE html>
<html>
  <head>
    <title>File Upload to Google Drive with Name and Email Input</title>
  </head>
  <body>
    <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>
  </body>
</html>
  • In this case, when you open HTML form and click the button, after the file is uploaded, success.html is opened by doGet.

Note:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • @Sandeep Thank you for replying. When you modified the Google Apps Script of Web Apps, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful about this. – Tanaike Mar 21 '23 at 07:29
  • Tanaike, In Google apps script I've a 2 files one is code.gs and success.html. Now I want return user to success.html (which is in apps script). So, in this scenario how can I achieve this? – Sandeep Mar 21 '23 at 07:36
  • @Sandeep Thank you for replying. In this case, in order to correctly understand your current situation, can you provide your sample Google Apps Script project? – Tanaike Mar 21 '23 at 07:37
  • Sure! Here is my project code. And thankyou so much for your time. https://drive.google.com/file/d/1CgQpatIKubn8NxW2pDylwmd6QEtnufJp/view?usp=sharing – Sandeep Mar 21 '23 at 08:08
  • @Sandeep Thank you for replying. When I saw your provided text data, I cannot understand the method for opening "HTML FORM:". In your actual situation, "HTML FORM:" is put to outside of Google Apps Script project? – Tanaike Mar 21 '23 at 08:16
  • I've haven't hosted the html form. But you can just save it in html format and open locally. – Sandeep Mar 21 '23 at 08:23
  • @Sandeep Thank you for replying. I thought that I could understand your situation. So, I updated my answer. Please confirm it. – Tanaike Mar 21 '23 at 08:31
  • Thanks @Tanaike, will implement it & let you know. – Sandeep Mar 21 '23 at 08:34
  • It worked !! Thanks a lot @Tanaike. Just want to know that what changes you've done in order to achieve this in some detail please. – Sandeep Mar 21 '23 at 09:05
  • @Sandeep About `want to know that what changes you've done in order to achieve this`, I think that you can see it from the difference between your showing script and my proposed script. – Tanaike Mar 21 '23 at 09:14
  • Got it marked as Solved, Thankyou once again @Tanaike !! – Sandeep Mar 21 '23 at 09:17
  • @Sandeep Thank you for replying. About the contact using email, now, I avoid direct contact using email related to the questions on Stackoverflow. I apologize for this. If you have a new question, I would like to recommend posting it as a new question to Stackoverflow. By this, it will help a lot of users including me think of a solution. And, I would like to support you on Stackoverflow. I apologize for my current situation, again. – Tanaike Mar 21 '23 at 09:23
  • Hi @Tanaike, had one more query related to this, so can you please refer this question: https://stackoverflow.com/questions/75799793/how-to-get-variable-from-code-gs-file-to-html-file-in-google-apps-script – Sandeep Mar 21 '23 at 10:10