How to Create Data Entry Form with Google HTML Service and Submit Data to Google Sheets

Data entry is one of the important steps in the data analysis process. It is important that the data collected is correctly entered into the electronic format. One of the ways that you can maintain the data quality is creating an efficient form which can accurately enter your data. A correct data entry form is a great way to accelerate data entry and boost productivity. This can reduce entering inconsistent and inaccurate data which affects analysis negatively.

The data entry forms are even helpful when they are available online. With online forms, you can allow your data entry operators to work from home. You can implement tracking methods to monitor their performance. So, you can do the data entry work more easily and efficiently with online forms.

The Google Forms is the most popular online survey tools which are available free of charge. However, when it comes to mass data entry works, it is not that supportive. Instead of using Google Forms, you can build your own data entry form with Google HTML Service. With HTML service, you can have your own design, input fields, validations and save your data to Google Sheet. And also you can embed your form in Google Sites and build your own web app.

In this post, I will show you how you can create the following simple data entry form with Google Apps Scripts and submit the data into Google Sheets.

Step 01: Create a new Google Sheet on your Google Drive

Create a new Google Sheet and add column labels as shown in the below image (these column names are not used in the programme, it is only to identify the data stored underneath)

Step 02. Creating the HTML form

Open the script editor from Tools → Script editor in your Google Sheet.

Then, replace code in the Code.gs file with the following code.

Next, create a new HTML file in the script editor from, File → New → HTML file. name it as “Index“. Then replace the auto-generated code with the following.

Now the HTML form is ready and you can deploy it as a web app by going to Publish → Deploy as a web app… Click the deploy button on the dialog box and copy the link in the next dialog box. Paste the link in the browser, then you can see the form we created.

In order to add styles to the form, I have loaded Bootstrap CSS via CDN (You can see it in line 5 of the above code).

Step 03. Sending Form Data to Google Sheet

Using google.script.run asynchronous client-side JavaScript API, we can call server-side Apps Script functions from HTML-service pages.

The following JavaScript, calls the server side function processForm once you click the submit button. To add this JavaScript , create a new HTML file and name it ass JavaScript. Then replace the auto-generated code with the following.

Then add the following code just before the </head> tag of the Index.html file. This code calls the server-side function include() and includes the above JavaScript in the head section of the Index.html file.

The function preventFormSubmit() in the above JavaScript code change the default form behavior and it prevents the form redirecting to an inaccurate URL. Once you click the submit button, the above javascript calls the server-side function handleFormSubmit() with the form data.

Then add the following two server-side functions to the Code.gs file we created at the beginning and replace the URL with your Google Sheet URL.

** Important! Replace the URL (line 9) with the URL of your spreadsheet.

Your final code arrangement should look like bellow.

You can see the live form from this link.

You can make a copy of the above Google Sheet (with the code) from this link.

Step 04. Deploying a script as a web app

Now you can deploy the above code as a web app by going to Publish → Deploy as a web app… Then select New for the project version and click update. Copy the Current web app URL in the next dialog box and click OK. Paste the copied URL in the browser to get the form. You can read more about Deploying a script as a web app from this Google Apps Script Guide.

What Next?

In the above code I have not added form validations. In order to improve the data quality you need to add data validation for your critical fields. You can add form validation either from the server side or client side.

You can also embed this form in a Google Site. Read more about Embedding your web app in Google Sites from this Google Apps Script guide.

References

  1. HTML Service: Communicate with Server Functions
  2. HTML Service: Best Practices

4 thoughts on “How to Create Data Entry Form with Google HTML Service and Submit Data to Google Sheets”

  1. This is a wonderful tutorial and has helped me immensely. I do have a couple of questions. First, my form does not clear after I hit the submit button. How can I make it clear automatically? Second, is there a way to use the same form to perform a search of the spreadsheet that the form sends information to, in order to update/modify information that already exists?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: