--- id: 39278362-dbfc-11ee-8a2b-139ebfa11748 title: | How to Build a Basic CMS with Google Sheets and React status: ARCHIVED tags: - read-later - RSS date_added: 2024-03-06 14:55:48 url_omnivore: | https://omnivore.app/me/how-to-build-a-basic-cms-with-google-sheets-and-react-18e158f0d8c url_original: | https://www.freecodecamp.org/news/how-to-build-a-basic-cms-with-google-sheets-and-reactjs/ --- # How to Build a Basic CMS with Google Sheets and React ## Highlights ### Step 1: Set Up Your Google Sheets 1. Go to your Google Sheets 2. Open the sheet you want to use or create a new one 3. Click on `Extensions` in the menu 4. Then click on `Apps Script` In the Apps Script editor, you can write a script to serve as your endpoint. Here's a script that returns the contents of a Google Sheet in JSON format: ```kotlin function convertRangeToJson(data) { var jsonArray = []; // Check if data is empty or doesn't contain enough rows for headers and at least one data row if (!data || data.length < 2) { // Return an empty array or a meaningful message as needed return jsonArray; // or return 'No data available'; } var headers = data[0]; for (var i = 1, length = data.length; i < length; i++) { var row = data[i]; var record = {}; for (var j = 0; j < row.length; j++) { record[headers[j]] = row[j]; } jsonArray.push(record); } return jsonArray; } ``` Then: 1. Click `File` \> `Save`, and give your project a name 2. Click on `Deploy` \> `New deployment`. 3. Click on `Select type` and choose `Web app`. 4. Fill in the details for your deployment. Under `Execute as`, choose whether the script should run as your account or as the user accessing the web app. Under `Who has access`, choose who can access your web app. 5. Click `Deploy`. [source](https://omnivore.app/me/how-to-build-a-basic-cms-with-google-sheets-and-react-18e158f0d8c#d5dee3e3-c455-4623-952d-2b11f076c3fa) --- ## Original  In today's digital landscape, creating a content management system (CMS) that is both cost-effective and easy to maintain can be difficult, especially if you're operating on a tight budget. This tutorial will show you a solution that leverages Google Sheets as a makeshift database and React to build the frontend. This will let you effectively bypass the need for a dedicated server or traditional database system. This approach not only reduces the overhead costs associated with web development, but also simplifies content updates and management. It's an ideal solution if you're looking to launch your own simple CMS without substantial investment. This solution is suitable for freelancers at the beginning of their career and for clients who cannot invest much in their website. ## Why Google Sheets? Opting for Google Sheets as the backbone of your CMS comes down to its simplicity, flexibility, and cost-effectiveness. Traditional web development requires a backend server to process data, a database to store information, and a frontend to display content. But each layer adds complexity and cost. Google Sheets, on the other hand, acts as a highly accessible and intuitive interface that eliminates the need for a server and a database. It lets your users update content in real-time, much like any CMS, but without the usual setup and maintenance costs. This makes it an excellent choice for individuals, small businesses, or anyone looking to deploy a web application quickly and with minimal expense. ## Getting Started Before diving into the code, ensure you have Node.js and npm installed on your system. These tools will allow us to create a React application and manage its dependencies. Let's start with Google Sheets now. ### ==Step 1: Set Up Your Google Sheets== 1. ==Go to your Google Sheets== 2. ==Open the sheet you want to use or create a new one== 3. ==Click on== `==Extensions==` ==in the menu== 4. ==Then click on== `==Apps== ==Script==` ==In the Apps Script editor, you can write a script to serve as your endpoint. Here's a script that returns the contents of a Google Sheet in JSON format:== ```kotlin function convertRangeToJson(data) { var jsonArray = []; // Check if data is empty or doesn't contain enough rows for headers and at least one data row if (!data || data.length < 2) { // Return an empty array or a meaningful message as needed return jsonArray; // or return 'No data available'; } var headers = data[0]; for (var i = 1, length = data.length; i < length; i++) { var row = data[i]; var record = {}; for (var j = 0; j < row.length; j++) { record[headers[j]] = row[j]; } jsonArray.push(record); } return jsonArray; } ``` ==Then:== 1. ==Click== `==File==` ==>== `==Save==`==, and give your project a name== 2. ==Click on== `==Deploy==` ==>== `==New== ==deployment==`==.== 3. ==Click on== `==Select== ==type==` ==and choose== `==Web app==`==.== 4. ==Fill in the details for your deployment. Under== `==Execute== ==as==`==, choose whether the script should run as your account or as the user accessing the web app. Under== `==Who has== ==access==`==, choose who can access your web app.== 5. ==Click== `==Deploy==`==.== You may be asked to authorize the script to access your Google Sheets. Follow the prompts to do so. After deploying, you'll be given a URL for your web app. This is your API endpoint. To give you an idea of what you have done so far, this is your sheet structure:  How your sheet should currently look And this is the JSON you get when you call the endpoint:  JSON ### Step 2: Create Your React App With your Google Sheets API ready, it's time to create the React app that will fetch and display this data. First, go ahead and create a React app. Run the following command in your terminal to create a new React application: ```dsconfig npx create-react-app google-sheets-cards cd google-sheets-cards npm start ``` You can also [use modern build tools like Vite](https://www.freecodecamp.org/news/get-started-with-vite/) for this purpose, as CRA is no longer the recommended way of building a React app. Next, create the card component. Inside the `src` directory, create a file named `Card.js`. This component will be responsible for displaying each data record: ```javascript // src/Card.js function Card({ title, content }) { return (
{content}