From 914bdc9a53b5809aabadfac98bdf09c839d88295 Mon Sep 17 00:00:00 2001 From: Vivek Teega Date: Thu, 8 Jun 2023 14:45:07 +0530 Subject: [PATCH] New logic to sync Google Sheets locally and then operate on them --- routes/economicSystem.js | 6 +- test-sync-sheets.js | 15 ++-- utils/EconomicSystem.js | 146 +++++++++++++++++---------------------- 3 files changed, 73 insertions(+), 94 deletions(-) diff --git a/routes/economicSystem.js b/routes/economicSystem.js index fbd072a..f42b09e 100644 --- a/routes/economicSystem.js +++ b/routes/economicSystem.js @@ -14,7 +14,7 @@ router.get('/data', async (req, res) => { // Fetch consumption data await tokenRoom_EconomicSystem.fetchConsumptionData(); - // Fetch consumption number + // // Fetch consumption number await tokenRoom_EconomicSystem.fetchConsumptionNumber(); const productionCost = tokenRoom_EconomicSystem.productionCost; @@ -22,7 +22,7 @@ router.get('/data', async (req, res) => { const consumptionCost = tokenRoom_EconomicSystem.consumptionCost; const productionValuation = tokenRoom_EconomicSystem.calculateProductionValuation(); const consumptionValuation = tokenRoom_EconomicSystem.calculateConsumptionValuation(); - const systemValuation = tokenRoom_EconomicSystem.calculateSystemValuation(); + //const systemValuation = tokenRoom_EconomicSystem.calculateSystemValuation(); res.json({ productionCost, @@ -30,7 +30,7 @@ router.get('/data', async (req, res) => { consumptionCost, productionValuation, consumptionValuation, - systemValuation, + //systemValuation, }); } catch (error) { console.error('Error fetching data:', error); diff --git a/test-sync-sheets.js b/test-sync-sheets.js index b25fc0c..5f38a45 100644 --- a/test-sync-sheets.js +++ b/test-sync-sheets.js @@ -2,12 +2,13 @@ const { google } = require('googleapis'); const fs = require('fs'); const path = require('path'); const axios = require('axios'); +const sheet_data = require('./config/sheet_data.json'); // Set the path to the Google service account key file -const keyFilePath = path.join(__dirname, 'config' ,'*.json'); +const keyFilePath = path.join(__dirname, 'config' ,'access_token.json'); // Set the ID of the Google Drive folder to sync -const folderId = ''; +const folderId = sheet_data.sync_folderId; // Set the local directory path to sync the folder to const localDirectory = path.join(__dirname, 'data'); @@ -35,6 +36,7 @@ async function downloadFile(drive, fileId, filePath) { res.data .on('end', () => { console.log(`Downloaded file: ${filePath}`); + console.log(`${filePath} : filePath`) resolve(); }) .on('error', (err) => { @@ -47,10 +49,10 @@ async function downloadFile(drive, fileId, filePath) { async function syncFolder(drive, folderId, localPath) { // List all files and subfolders in the folder - const res = await drive.files.list({ - q: `'${folderId}' in parents and trashed = false`, - fields: 'files(id, name, mimeType)', - }); + const res = await drive.files.list({ + q: `'${folderId}' in parents and trashed = false`, + fields: 'files(id, name, mimeType)', + }); const items = res.data.files; @@ -92,7 +94,6 @@ async function runSync() { // Create a Google Drive API instance const drive = google.drive({ version: 'v3', auth: authClient }); - await syncFolder(drive, folderId, localDirectory); // Update with the path to your local folder } catch (error) { diff --git a/utils/EconomicSystem.js b/utils/EconomicSystem.js index 26a94e2..bf5d7ad 100644 --- a/utils/EconomicSystem.js +++ b/utils/EconomicSystem.js @@ -1,6 +1,7 @@ const { google } = require('googleapis'); -const privatekey = require('../config/access_token.json'); const sheet_data = require('../config/sheet_data.json'); +const xlsx = require('xlsx'); +const path = require('path'); class EconomicSystem { constructor() { @@ -13,104 +14,81 @@ class EconomicSystem { } async fetchProductionData() { - const auth = new google.auth.JWT( - privatekey.client_email, - null, - privatekey.private_key, - ['https://www.googleapis.com/auth/spreadsheets'] - ); - - const sheets = google.sheets({ version: 'v4', auth }); - try { - const response = await sheets.spreadsheets.values.get({ - spreadsheetId: sheet_data.revenue_spreadsheetId, - range: sheet_data.TR_Expenses_range, - valueRenderOption: 'FORMULA' - }); - - const consumptionData = response.data.values; - if (consumptionData) { - let sum = 0; - for (const row of consumptionData) { - const value = parseFloat(row[0]); - if (!isNaN(value)) { - sum += value; + const workbook = xlsx.readFile(path.resolve(__dirname, `../data/${sheet_data.revenue_spreadsheetName}`)); + // Assuming the "TR-Expenses" sheet is the third sheet (index 2) of the workbook + const worksheet = workbook.Sheets[sheet_data.TR_Expenses_sheet]; + const { s: startCell, e: endCell } = xlsx.utils.decode_range(sheet_data.TR_Expenses_range); + + let productionSum = 0; + for (let row = startCell.r; row <= endCell.r; row++) { + for (let col = startCell.c; col <= endCell.c; col++) { + const cellAddress = xlsx.utils.encode_cell({ r: row, c: col }); + const cellValue = worksheet[cellAddress]?.v; + if (typeof cellValue === 'number'){ + productionSum += cellValue; + } } - } - this.productionCost = sum; } + + this.productionCost = productionSum; } catch (error) { console.error('Error fetching production data:', error); - } - } - - async fetchConsumptionNumber(){ - const auth = new google.auth.JWT( - privatekey.client_email, - null, - privatekey.private_key, - ['https://www.googleapis.com/auth/spreadsheets'] - ); - - const sheets = google.sheets({ version: 'v4', auth }); - - try { - const response = await sheets.spreadsheets.values.get({ - spreadsheetId: sheet_data.booking_spreadsheetId, - range: sheet_data.Booking_range, - valueRenderOption: 'UNFORMATTED_VALUE' - }); - - const consumptionData = response.data.values; - if (consumptionData) { - let sum = 0; - for (const row of consumptionData) { - const value = parseFloat(row[0]); - if (!isNaN(value)) { - sum += value; - } - } - this.consumptionNumber = sum - } - } catch (error) { - console.error('Error fetching consumption data:', error); + return null; } } async fetchConsumptionData() { - const auth = new google.auth.JWT( - privatekey.client_email, - null, - privatekey.private_key, - ['https://www.googleapis.com/auth/spreadsheets'] - ); - - const sheets = google.sheets({ version: 'v4', auth }); - try { - const response = await sheets.spreadsheets.values.get({ - spreadsheetId: sheet_data.revenue_spreadsheetId, - range: sheet_data.TR_Consumption_range, - valueRenderOption: 'FORMULA' - }); - - const consumptionData = response.data.values; - if (consumptionData) { - let sum = 0; - for (const row of consumptionData) { - const value = parseFloat(row[0]); - if (!isNaN(value)) { - sum += value; + const workbook = xlsx.readFile(path.resolve(__dirname, `../data/${sheet_data.revenue_spreadsheetName}`)); + const worksheet = workbook.Sheets[sheet_data.TR_Consumption_sheet]; + const { s: startCell, e: endCell } = xlsx.utils.decode_range(sheet_data.TR_Consumption_range); + + let consumptionSum = 0; + for (let row = startCell.r; row <= endCell.r; row++) { + for (let col = startCell.c; col <= endCell.c; col++) { + const cellAddress = xlsx.utils.encode_cell({ r: row, c: col }); + const cellValue = worksheet[cellAddress]?.v; + if (typeof cellValue === 'number'){ + consumptionSum += cellValue; } } - this.consumptionCost = sum } + + this.consumptionCost = consumptionSum; } catch (error) { - console.error('Error fetching consumption data:', error); + console.error('Error fetching conumption data:', error); + return null; + } + + } + + async fetchConsumptionNumber() { + try { + const workbook = xlsx.readFile(path.resolve(__dirname, `../data/${sheet_data.booking_spreadsheetName}`)); + // Assuming the "TR-Expenses" sheet is the third sheet (index 2) of the workbook + const worksheet = workbook.Sheets[sheet_data.Booking_sheet]; + const { s: startCell, e: endCell } = xlsx.utils.decode_range(sheet_data.Booking_range); + + let consumptionNumber = 0; + for (let row = startCell.r; row <= endCell.r; row++) { + for (let col = startCell.c; col <= endCell.c; col++) { + const cellAddress = xlsx.utils.encode_cell({ r: row, c: col }); + const cellValue = worksheet[cellAddress]?.v; + if (typeof cellValue === 'number'){ + consumptionNumber += cellValue; + } + } + } + + this.consumptionNumber = consumptionNumber; + } catch (error) { + console.error('Error fetching conumption data:', error); + return null; } } + calculateConsumptionValuation() { this.consumptionValuation = this.consumptionNumber * sheet_data.TR_Consumption_Valuation_Price_USD; return this.consumptionValuation; @@ -120,10 +98,10 @@ class EconomicSystem { this.productionValuation = this.productionCost; return this.productionValuation; } - + calculateSystemValuation() { return Math.max(this.productionValuation, this.consumptionValuation); } } -module.exports = EconomicSystem; +module.exports = EconomicSystem; \ No newline at end of file