{
    "componentChunkName": "component---src-templates-blog-post-js",
    "path": "/saving-data-to-google-spreadheet-node/",
    "result": {"data":{"site":{"siteMetadata":{"title":"CrewCode Solutions"}},"markdownRemark":{"id":"d2c3f700-da09-536c-bca4-5bb8c0cd346e","excerpt":"In this article i will explain you step by step how you can save data to google spreadsheet in your node js application Step 1:Create google spreadsheet…","html":"<p>In this article i will explain you step by step how you can save data to google spreadsheet in your node js application</p>\n<h4>Step 1:Create google spreadsheet credentials</h4>\n<p>In order to read and write data to google spreadheet you need to authenticate with google for that we need to create Google service account key file</p>\n<ul>\n<li>\n<p>Go to <a href=\"https://console.developers.google.com/\">https://console.developers.google.com/</a></p>\n</li>\n<li>\n<p><strong><em>Select a project or create a new project</em></strong></p>\n</li>\n</ul>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Saving-data-to-google-spreadsheet-in-node-1.png\" alt=\"new-project\"></p>\n<ul>\n<li><strong><em>Go to credentials and click on manage service account</em></strong></li>\n</ul>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Saving-data-to-google-spreadsheet-in-node-2.png\" alt=\"service-account\"></p>\n<ul>\n<li><strong><em>Click on create service account</em></strong></li>\n</ul>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Saving-data-to-google-spreadsheet-in-node-3.png\" alt=\"create-service-account\"></p>\n<ul>\n<li><strong><em>Fill the detail and click on done</em></strong></li>\n</ul>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Saving-data-to-google-spreadsheet-in-node-4.png\" alt=\"create-account\"></p>\n<ul>\n<li><strong><em>Click on manage keys from dropdown</em></strong></li>\n</ul>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Saving-data-to-google-spreadsheet-in-node-5.png\" alt=\"manage-keys\"></p>\n<ul>\n<li><strong><em>Click on Add Key and from Popup select JSON and click on create</em></strong></li>\n</ul>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Saving-data-to-google-spreadsheet-in-node-6.png\" alt=\"create-keys\"></p>\n<ul>\n<li><strong><em>Copy the private key id and client email from your downloaded json file add to your env file or use it in next step</em></strong></li>\n</ul>\n<h4>Step 2: Enable Google spread sheet api</h4>\n<ul>\n<li>\n<p>Click on Enable APIs &#x26; Service from left panel</p>\n</li>\n<li>\n<p>Click on ENABLE APIS AND SERVICE from the top header</p>\n</li>\n<li>\n<p>Search Google Sheet API</p>\n</li>\n<li>\n<p>Click on Google Sheet API from the search result and click on Enable</p>\n</li>\n</ul>\n<h4>Step 3: Create Google Spreadsheet</h4>\n<p>Create google spreadhsheet and copy spreadhsheet id and sheet id ans save it somewhere safe or paste it in your env file because we are going to use this</p>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Saving-data-to-google-spreadsheet-in-node-7.png\" alt=\"create-keys\"></p>\n<h4>Step 4: Share the google spreadsheet with the private email from downloaded json file</h4>\n<p><img src=\"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/Saving-data-to-google-spreadsheet-in-node-8.png\" alt=\"share-spreadhsheet\"></p>\n<h4>Step 5: Add data to google spread sheet</h4>\n<ul>\n<li>Install google-spreadsheet to your nodejs application</li>\n</ul>\n<pre><code class=\"language-js\">npm install --save google-spreadsheet\n</code></pre>\n<ul>\n<li>Copy paste following code under route app.post(\"/savedata\") in your node.js project</li>\n</ul>\n<pre><code class=\"language-js\">import express from \"express\";\nimport path from \"path\";\nimport \"dotenv/config\";\nimport { GoogleSpreadsheet } from \"google-spreadsheet\";\n\nconst app = express();\nconst port = 3000;\nconst __dirname = path.resolve();\nconst doc = new GoogleSpreadsheet(process.env.SPREADSHEET_ID);\n\napp.use(express.json());\n\napp.post(\"/savedata\", async (req, res, next) => {\n  await doc.useServiceAccountAuth({\n    client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,\n    private_key: process.env.GOOGLE_PRIVATE_KEY,\n  });\n\n  await doc.loadInfo();\n  const sheet = doc.sheetsById[process.env.SHEET_ID];\n  const result = await sheet.addRow({\n    \"First Name\": req.body.first_name,\n    \"Last Name\": req.body.last_name,\n    \"Email Address\": req.body.email,\n    Password: req.body.password,\n  });\n  res.json(result);\n});\n\napp.get(\"/readdata\", (req, res, next) => {});\n\napp.listen(port, () => {\n  console.log(`Demo app listening on port ${port}`);\n});\n</code></pre>","fields":{"slug":"/saving-data-to-google-spreadheet-node/"},"frontmatter":{"title":"Saving data to google spreadsheet in node js application","date":"May 01, 2023","description":"In this article i will explain you step by step how you can save data to google spreadheet in your nodejs application","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/google-spreadheet-node.jpeg"}},"previous":{"fields":{"slug":"/restapi-using-deno-oak-mongodb/"},"frontmatter":{"title":"Developing REST Api using Deno, Oak and MongoDB","date":"April 30, 2023","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/RestAPI.jpg"}},"next":{"fields":{"slug":"/serverless-api-using-nodejs-lambda/"},"frontmatter":{"title":"Serverless Rest API using nodejs and AWS Lambda","date":"May 15, 2023","bannerimage":"https://crew-code-images.s3.us-east-1.amazonaws.com/blog_images/serverless-node.jpeg"}}},"pageContext":{"id":"d2c3f700-da09-536c-bca4-5bb8c0cd346e","previousPostId":"6d904096-d929-5403-a8d8-150a4d45b491","nextPostId":"58348641-da6b-517b-8c01-a88b84257a9e"}},
    "staticQueryHashes": ["3860684146"]}