How to Create an Online Data Entry Form that can Perform CRUD Operations on Google Sheets

Online data entry forms are a powerful tool that you can use to collect many types of data more easily and accurately. Google Sheets is the most powerful cloud-based spreadsheet application that you can use absolutely free. Google is also offering another free tool named Google Apps Script (GAS) to increase the power of your favorite Google apps. In this post, I will explain to you how you can create a free online form (or Web App) that can perform Create, Read, Update and Delete (CRUD) Operations on Google Sheets using Google Apps Script and Google Sheets API v4.

Why I Should Create online Data Entry Forms

You can improve the productivity of your work by creating an HTML form to submit data to Google Sheets than directly typing data on the Sheet.

  • Easy to share the form and ask all your staff or team members to submit data to the same Sheet and at the same time.
  • You need not give Google Sheet edit access to your team, so your data is secure
  • Reduce human errors
  • Increase the reliability of your data -form validation
  • Fast data entry (if you are doing mass data entry work) – Save both time and money
  • Monitor data entry work real-time and let you make proactive decisions
  • And so many…

All the above-mentioned app and services are packed with your Google Account completely free. So, you can build an absolutely free data entry form or any type of online form to perform CRUD Operations on Google Sheets. So, let’s get started.

In a previous post also, I explained, “How to Create Data Entry Form with Google HTML Service and Submit Data to Google Sheets“. However, using that code you can only write data to Google Sheets. If you only need to submit data to Google Sheets, you may use that method because that code is less complex than this.

What you need

A Google Account

Basic knowledge on HTML, CSS, JavaScript

Basic Knowledge on Google Apps Script

You need to have basic programming knowledge on HTML, CSS, and JavaScript to modify the code in this post. However, I will try my best to make the code readable and editable by non-programmers too.

Overview of creating an Online Data Entry Form that can Perform CRUD Operations on Google Sheets

In this tutorial, we are going to create a web form that can Create, Read, Update, and Delete (CRUD) records in Google Sheets.

For creating this online form I am using Google Apps Scripts. There are two ways that you can use Google Apps Scripts namely Container-bound Scripts and Standalone Scripts. Here I am using the Standalone Scripts. So you have to create Google Sheet and the App Script files separately in your Google Drive.

We are going to perform CRUD operations on Google Sheets using Google Sheets API v4. (in the previous post that I mentioned above create the HTML form without using APIs)

The Concept

To perform CRUD operations, you need to create a unique ID for your records. For this purpose, this script uses the current time in milliseconds (since 1970) as explains here.

I briefly explain the logic behind the CRUD operations used in this method below.

Create/ Insert Data

Fill the form and hit the submit button. If it passes the browser validation data is sent to the server-side script. If it is not an update request (does not contain an ID) server-side script adds a new id (it is the timestamp) and append data to the specified range.

Read Data

The server-side script request data for a given range using APIs. after you perform, create, update, or delete action the web app updates the data table in the frontend with changed data.

Update Data

When you click the update button, it passes the record ID to the server-side script. The server-side script validates the ID, and if it exists, it reads the relevant row. The data passes to the form for editing.

Once you hit the submit button, it passes data to the server-side script. It validates the ID and if it exists, updates the replace the relevant row with new data.

Delete Data

When you click the delete button, it passes the record ID to the server-side script. The server-side script validates the ID. If the ID exists, delete the relevant row and shift the cells up.

Live Demo

Use the following links to access the live form and the Data Sheet.

How to Create this online form using Google Apps Script and perform CRUD operations on Google Sheets

Here I am not going to explain the code line by line. Instead, I will briefly explain the purpose of each function, file, and present you the relevant code below.

If you are already familiar with Google Apps Script, you can copy the Google Apps Script file and the Google Sheet to your Google Drive from the following links and start modifying. I have added comments at all the locations that you need to change when you are adding new fields.

Google Sheets1

Link to Google Sheet
(Make a copy to your Drive)

You need to change the Google Sheet ID (Spreadsheet ID) in the Apps Script file as explained below in order to send the data to Google Sheet.

You may not be able to view the Apps Script file from the above link when too many users are viewing the file at the same time. In such a case, you can use the following link which contains the same copy of the above.

Apps Script Link 2, Google Sheet Link 2

The following video demonstrates how to copy these files to your Google Drive and run the code. In this video, from 2:15 onwards I am explaining how to add a new field to the form.

YouTube player

You can get a brief idea of the code following the below steps. I assume that you have already logged into your Google Account.

Step 01: Make a Copy of the Google Sheets

You can make a copy of the Google Sheets used in this example from the following link.

Link to Google Sheet – Make a copy to your Google Drive

This spreadsheet contains two sheets namely “Data” and “Helpers”. The data you inserted from the web form is saving to the “Data” Sheet. The “Helper” sheet includes a list of countries, that are required for populating the “Country” drop-down list.

Google Sheets CRUD App - The data sheet

Step 02: Make a Copy of the Google Apps Script File

You can also make a copy of the App Script used in this example file from the below link.

Link to Google Apps Script File – Make a copy to your Google Drive

The Apps Script file contains the following files.

  1. Code.gs
  2. Index.html
  3. JavaScript.html
  4. Form.html
  5. DataTable.html
  6. CSS.htm

The Code.gs file contains the server-side scripts, which include the function that calls Google Sheets API. The other files make up the online form.

I have moved the HTML codes for the form and the data table into separate files namely Form.html and DataTable.html files to make the code more readable. Those two files are included in the Index.html file using include() function in the Code.gs file.

The JavaScript and CSS are written in the JavaScript.html and CSS.html files respectively. Those two files are also included in the Index.html file using the same include() function mentioned above.

See the Google HTML Service guides on Separating HTML, CSS, and JavaScript

Code Snippets

The following are the codes included in the Google Apps Script file mentioned above.

1. Code.gs

The Code.gs file includes the server side functions.

Inside the function globalVariables(), you can define all the variables used in the script. So, you can call them later inside the other functions when you need them.

Most of the functions are self-explanatory, and I have added comments to make it easy to understand. The sources of some code sections are also added in the comments.

I will explain how this code works later in this post.

2. Index.html

I am using the Bootstrap framework for styling the page. I have included it in the header section of the Index.html using BootstrapCDN. The JavaScript.html, CSS.html, Form.html and DataTable.html files are included using <?!= include('file_name'); ?> code in 8,9,15 and 20 lines of the following code snippet.

3. JavaScript.html

This file includes the JavaScript codes which run on the visitor’s browser.

4. Form.html

This file includes the HTML codes which create the HTML data entry form.

5. DataTable.html

This file includes the code required to generate the data table. The data table is inserted by a JavaScript function inside the id dataTable.

6. CSS.html

This file contains a CSS class that changes the button size. You can add your custom CSS inside the following file.

Step 03: Change the SpreadsheetID in Code.gs file

Copy the spreadsheetID of the google sheet, you copied to your drive in Step 01. Replace the spreadsheetId in line 15 of Code.gs file.

Learn how to find spreadsheetID.

Step 04: Enable Google Sheets API

You must enable Google Sheets API in Advance Google Services to run this script. To enable Google Sheets API for you Apps Script,

  1. Open the Script Editor (the file you copied to Google Drive in Step 02)
  2. Go to Resources > Advanced Google services….
  3. In the Advanced Google Service dialog box, go down and enable Google Sheets API
  4. Click Ok

Step 05: Deploy as a Web App

To deploy this script as a web app.

  1. In the Script Editor, Go to Publish > Deploy as web app
  2. In the Deploy as web app dialog, select Project Version as New.
  3. Under Execute the app as: select your email address.
  4. under Who has access to the app, select your preference.
  5. Click Deploy.
  6. In the next dialog box, copy the Current web app URL
  7. Paste it in your browser and your online form will be loaded.

Read more about Deploying a script as a web app in Google Guide.

How this Script perform CRUD Operations on Google Sheets

Create New Record

  1. After you correctly fill the form and hit the submit button, the function handleFormSubmit() in the JavaScript file is called.
  2. This function calls the function processForm() in the Code.gs file and passes the form object as the parameter.
  3. The function processForm() check whether form object has an ID (RecID) and if exist validate using checkID() function.
  4. If the form object does not have a valid recId, the function appendData() is called.
  5. The function getFormValues() (the first parameter of the appendData() function) process the form object and returns a value array. It also creates a new ID if recId not exists.
  6. The function appendData() is executed and append the form data ta to the given range.
  7. Then the function processForm() call the function getLastTenRows() and it returns the last 10 rows of your data range. The function processForm() also return the same to the handleFormSubmit() mentioned in no 1 above.
  8. Then the JavaScript function handleFormSubmit() call the function createTable() function with the last 10 rows as a parameter.
  9. Then the function createTable() update the data table next to the Form.

Update Record

  1. The user clicks the update button in the row which data need to be updated.
  2. It calls the editData() JavaScript function which you can see in the JavaScript file.
  3. Then, the function editData() call the serverside function getRecordById() (it is in the Code.gs file) with the recId as the parameter.
  4. The getRecordById() returns the relevant row of data as an array to the editData() function.
  5. Then, the editData() function calls the JavaScript function populateForm() with the data array (it is in the JavaScript file).
  6. The function populateForm() populates the form fields with the data.
  7. Then the user can make the changes and hit the submit button.
  8. Hereafter, almost the same process in the above “Create New Record” is followed. Since here form passes a recId, in step 4 it calls the updateData() function. It replaces the row which matches the recId.

Delete Data

  1. The user clicks the Delete button in the row which data need to be deleted.
  2. Browser requests to confirm the action.
  3. It calls the JavaScript function deleteData() which you can see in the JavaScript file.
  4. Then, the function deleteData() calls the serverside function deleteData() (the second is in the Code.gs file) with the recId as the parameter.
  5. The serverside function deleteData() validate and delete the row that matches the recId. After deleting the record function returns the last 10 records of the data range.
  6. Then the JavaScript function deleteData() (the function in the JavaScript file) calls the function createTable() with the last 10 rows as a parameter.
  7. Then the function createTable() updates the data table next to the Form.

Form Validation

Form validation is very important when submitting data to Google Sheets. For form validation, I have used browser defaults. However, depending on the requirements you may need more customization. You can learn more about Bootstrap form validation from this link.

What Next

This form covers most of the common form elements that you need to build a data entry form or any type of online form to perform CRUD operations on Google Sheets. So, you can customize this form to match with your requirements using those elements. You can learn more about this using the links provided in this article as well as from the URLs commented in the code.

When you have data on your hand, you must use data visualization techniques to spot the signals in your data. If you can build an online dashboard for your data, you can make decisions even while your data collection project is ongoing. You can learn more about building a free online dashboard from my previous post, “How to Create an Online Dashboard for free to Share and Visualize Your Data“.

Updated

Some of our visitors requested to add a search field in the form. The following updated script contains a search field to search data in the Google Sheet.

Wrapping Up

Google Apps Scripts has been introduced to increase the power of your favorite Google apps. So, by using Google Apps Script with Google Apps you can build various types of custom solutions to boost your collaboration and productivity.

In this example, I explained to you how to build an Online Data Entry Form that can Perform CRUD Operations on Google Sheets. Where I used Google Sheets APIs with Google Apps Script HTML service. I have added most of the form elements that are required to build most of the forms used in your day-to-day works. So, you can expand this form to meet your requirements even if you do not have much knowledge of coding.

137 thoughts on “How to Create an Online Data Entry Form that can Perform CRUD Operations on Google Sheets”

  1. Hi, first of all thanks for everything!

    I have added many fields to the form and everything works apart from one small issue. When I edit the entries, all the fields are not populated. would you know why? I have a total of 25 fields. Please let me know if possible. Thanks.

    Reply
  2. first of all thanks for the sharing.
    i got error when i try clik edit button
    it says “unchaught TypeError : Cannot read property ‘value’ of null’

    Reply
  3. I’m trying to combine this and the Sidebar/Modal functions. Yet for some reason the HTML and CSS between these two are written vastly differently. When I try to use the Sidebar code here, it ends up not processing the includes correctly.

    Is there a combined sample I could start with? I have some pretty advanced apps scripts but really need to add HTML dialogs and was hoping this would help me do that. I need the CRUD features combined with the dialogs. Currently I’m using onEdit on a spreadsheet dashboard but it doesn’t allow for multiple users simultaneously as well.

    Reply
  4. Hello,
    Nice work! It worked fine once I added the Sheets Service.
    I tried to Deploy so other users could add their contacts but it doesn’t display the Country of the GetData. It was Deployed as any user with the link.
    Its almost as if the Sheets Service is not connected.
    Thoughts?

    Thanks,
    Mike

    Reply
  5. Hi, nice work here. I am loving it and I see a lot of use case scenarios for this.

    A quick question: How possible is it to create filters for the data?

    In a way that multiple drop-down boxes are presented and the data gets filtered based on the data in the drop-down boxes.

    Thanks once again.

    Reply
  6. Hi! Great instruction and tool. I’ve got some additional question. Would that need a many adjustments go get only data only reported by current user using the web app?

    Reply
  7. How i can check duplicate key a specific COLUMN, I don’t want check uniqeID from ID column, but i want to check and return data depend value on specific column

    Reply

Leave a Comment

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

%d bloggers like this: