Complete explanation of how to make a Google Sheets backend
On this occasion I want to share a complete tutorial on how to make a backend using Google Sheets. As a first step, of course, you have to create a Spreadsheet file on your Google Drive. So that you can understand properly the header should be able to follow the example I provide in the image below.
The next step is to create a Script by looking for the Tools menu on your spreadsheet page, then looking for the Script Editor option and clicking on that option which will open the Script Editor page.
We start typing the script code, first we create a GET function with 5 choices of actions.
- We make an insert action which is for the function of adding data,
- The read action is to read a single data,
- The update action is for the data update function,
- The delete action is for the function to delete data based on the data id, and
- The readAll action is a function that reads all of the data in the spreadsheet.
For example code, you can copy below;
After we have finished creating the GET function and in it we create the C.R.U.D (Create Read Update Delete) function, then we create a script for each of these functions.
- INSERT or CREATE function
The explanation that needs to be paid attention is that lines of code 19 to 24 are parameters for making data in accordance with the header. So make sure you understand this code well, if the additional data is header data in the column then you also have to add the parameter code.
- READ function (for single data)
The explanation is on code line 50, the READ function on this single data needs to identify the ID of the data you want to read. After the ID is found, then you pay attention to the line code 64 to 68, where the data in line with the ID will be taken and displayed in JSON format with line code details 70 to 77.
- UPDATE function
The explanation is almost the same as the INSERT function, but here the difference is that ID is only a parameter to find data and is not changed.
- DELETE function
The explanation is for the DELETE function such as update, while the code lines 121 to 125 are actually only optional because only the ID parameter is used.
- READALL function
The explanation for the READALL function is the simplest, because in essence it reads all the data in the Sheet based on the Header.