Naveen J
Naveen J

Reputation: 63

Create Spreadsheet for read and write using Node js

I Need to create a spreadsheet like a google sheet with user authentication. I have stuck with how to start this project because I couldn't find which NPM module needs to use.

so Kindly help anyone push me in the right direction?

Upvotes: 0

Views: 1467

Answers (1)

Jeff Rush
Jeff Rush

Reputation: 912

I've been working on similar task few weeks ago.

So here is the mini help for you.

1. Read and follow Node.js Quickstart guide.

Keep an eye on the next concepts like:

  • Scopes

For example to get a value from a cell we can use spreadsheets.values.get

Scopes for this method are (use 1 of these):

https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.readonly
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/spreadsheets
https://www.googleapis.com/auth/spreadsheets.readonly

in node.js it can be an array

const SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly'];
  • Token

Once you run for the first time your index.js file the Terminal will ask you for authorization, on finish you will find a token.json in your work directory.

If you change Scopes -> delete token.json from your directory

  • SpreadsheetId

When you create/edit your spreadsheet you get similar url https://docs.google.com/spreadsheets/d/1eqRe0ksSwgOoPO0C_mZE6opjS1TlsCU5g1HtkiiILuw/edit#gid=0 1eqRe0ksSwgOoPO0C_mZE6opjS1TlsCU5g1HtkiiILuw is the ID


2. Create custom functions using Google Sheets API Reference

Example how to get cell values:

const sheets  = google.sheets({ version: 'v4', auth });
function getCellsValue(cells, callback){
  sheets.spreadsheets.values.get({
  spreadsheetId: 'spreadsheetId',
  range: cells
  }, (err, res) => {
    if (err) return console.log('Error #1001 : getCellsValue: ' + err);
    const output = res.data.values;
    if (output.length) {
      callback(output);
      return output;
    } else {
      console.log('No data found.');
    }
  });
}

// here is the example use of this function which output values from D2:D13
getCellsValue("D2:D13", (e) => {
  for(let i = 0; i < e.length; i++){
    console.log(e[i].toString());
  }
}, (err, res) => {
  if (err) return console.error('The API returned an error: ', err.message);
})

3. Make a use of Try this API

This is very useful tool.

Try this API

In case you need more help just use Stackoverflow for more transparency. Stackoverflow helps you when you make a very detailed question.

I hope this helped you and you can start your Google Sheets API journey.

Upvotes: 2

Related Questions