Skip to content

Commit

Permalink
Refactor code & add comments for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoBrodas committed Jul 9, 2021
1 parent 4acd9f8 commit ea5be4a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 43 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ A boiler plate code for getting started with Google Sheets API.

Contributions are always welcome!

### Improvements?
### What's new in v1.5?

- The code needs a bit of refactoring
- Anyone who has handled filter and update operation.
- Refactored code🤘
- Added few comments for readability.

## Tech Stack

Expand All @@ -35,19 +35,19 @@ Install dependencies
npm install
```

Create a new `.env` file.
- Create a new `.env` file.

Make a new project in Google Cloud console.
- Make a new project in Google Cloud console.

Integrate Google sheets API with the project.
- Integrate Google sheets API with the project.

Make a new service account, and a new Key.
- Make a new service account, and a new Key.

Copy the downloaded file to your remote workspace and rename it as `credentials.json`.
- Copy the downloaded file to your remote workspace and rename it as `credentials.json`.

Add the service email to the newly created Google sheets file at https://docs.google.com/spreadsheets
- Add the service email to the newly created Google sheets file at https://docs.google.com/spreadsheets

Start the server
- Start the server

```bash
npm run dev
Expand Down
67 changes: 34 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,41 @@ const express = require('express');
const { google } = require('googleapis');
const app = express();

//fetches data from the spreadsheet
app.get('/', async (req, res) => {
const spreadsheetId = process.env.DATABASE_ID;

// --- helper functions ---
// get meta-data
async function getMetaData(auth, spreadsheetId) {
const getMetaData = await googleSheet.spreadsheets.get({
auth,
spreadsheetId,
});
//use this method to get values like sheetID.
return getMetaData;
}

// get auth token
function getAuth() {
const auth = new google.auth.GoogleAuth({
keyFile: 'credentials.json',
scopes: 'https://www.googleapis.com/auth/spreadsheets',
});
return auth;
}

async function getGoogleSheet(auth) {
const client = await auth.getClient();
const googleSheet = google.sheets({ version: 'v4', auth: client });
const spreadsheetId = process.env.DATABASE_ID;
return googleSheet;
}
// --- helper functions ---

//fetches data from the spreadsheet
app.get('/', async (req, res) => {
const auth = getAuth();
const googleSheet = await getGoogleSheet(auth);

// const getMetaData = await googleSheet.spreadsheets.get({
// auth,
// spreadsheetId,
// });
// use this method to get values like sheetID.
//const metadata = getMetaData(auth, spreadsheetId, googleSheet);

const getSheetData = await googleSheet.spreadsheets.values.get({
auth,
Expand All @@ -31,22 +50,16 @@ app.get('/', async (req, res) => {

//posts data to cell
app.post('/post', async (req, res) => {
const auth = new google.auth.GoogleAuth({
keyFile: 'credentials.json',
scopes: 'https://www.googleapis.com/auth/spreadsheets',
});

const client = await auth.getClient();
const googleSheet = google.sheets({ version: 'v4', auth: client });
const spreadsheetId = process.env.DATABASE_ID;
const auth = getAuth();
const googleSheet = await getGoogleSheet(auth);

await googleSheet.spreadsheets.values.append({
auth,
spreadsheetId,
range: 'Sheet1!A2:B',
valueInputOption: 'USER_ENTERED',
resource: {
values: [['Kayne', 'Create a makeup app']],
values: [['NextJS', 'The framework of the future']],
},
});

Expand All @@ -55,14 +68,8 @@ app.post('/post', async (req, res) => {

// deletes cell data
app.post('/delete', async (req, res) => {
const auth = new google.auth.GoogleAuth({
keyFile: 'credentials.json',
scopes: 'https://www.googleapis.com/auth/spreadsheets',
});

const client = await auth.getClient();
const googleSheet = google.sheets({ version: 'v4', auth: client });
const spreadsheetId = process.env.DATABASE_ID;
const auth = getAuth();
const googleSheet = await getGoogleSheet(auth);

await googleSheet.spreadsheets.values.clear({
auth,
Expand All @@ -75,14 +82,8 @@ app.post('/delete', async (req, res) => {

// update cell data
app.put('/update', async (req, res) => {
const auth = new google.auth.GoogleAuth({
keyFile: 'credentials.json',
scopes: 'https://www.googleapis.com/auth/spreadsheets',
});

const client = await auth.getClient();
const googleSheet = google.sheets({ version: 'v4', auth: client });
const spreadsheetId = process.env.DATABASE_ID;
const auth = getAuth();
const googleSheet = await getGoogleSheet(auth);

await googleSheet.spreadsheets.values.update({
auth,
Expand Down

0 comments on commit ea5be4a

Please sign in to comment.