New logic to sync Google Sheets locally and then operate on them

This commit is contained in:
Vivek Teega 2023-06-08 14:45:07 +05:30
parent 645686e64e
commit 914bdc9a53
3 changed files with 73 additions and 94 deletions

View File

@ -14,7 +14,7 @@ router.get('/data', async (req, res) => {
// Fetch consumption data // Fetch consumption data
await tokenRoom_EconomicSystem.fetchConsumptionData(); await tokenRoom_EconomicSystem.fetchConsumptionData();
// Fetch consumption number // // Fetch consumption number
await tokenRoom_EconomicSystem.fetchConsumptionNumber(); await tokenRoom_EconomicSystem.fetchConsumptionNumber();
const productionCost = tokenRoom_EconomicSystem.productionCost; const productionCost = tokenRoom_EconomicSystem.productionCost;
@ -22,7 +22,7 @@ router.get('/data', async (req, res) => {
const consumptionCost = tokenRoom_EconomicSystem.consumptionCost; const consumptionCost = tokenRoom_EconomicSystem.consumptionCost;
const productionValuation = tokenRoom_EconomicSystem.calculateProductionValuation(); const productionValuation = tokenRoom_EconomicSystem.calculateProductionValuation();
const consumptionValuation = tokenRoom_EconomicSystem.calculateConsumptionValuation(); const consumptionValuation = tokenRoom_EconomicSystem.calculateConsumptionValuation();
const systemValuation = tokenRoom_EconomicSystem.calculateSystemValuation(); //const systemValuation = tokenRoom_EconomicSystem.calculateSystemValuation();
res.json({ res.json({
productionCost, productionCost,
@ -30,7 +30,7 @@ router.get('/data', async (req, res) => {
consumptionCost, consumptionCost,
productionValuation, productionValuation,
consumptionValuation, consumptionValuation,
systemValuation, //systemValuation,
}); });
} catch (error) { } catch (error) {
console.error('Error fetching data:', error); console.error('Error fetching data:', error);

View File

@ -2,12 +2,13 @@ const { google } = require('googleapis');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const axios = require('axios'); const axios = require('axios');
const sheet_data = require('./config/sheet_data.json');
// Set the path to the Google service account key file // 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 // 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 // Set the local directory path to sync the folder to
const localDirectory = path.join(__dirname, 'data'); const localDirectory = path.join(__dirname, 'data');
@ -35,6 +36,7 @@ async function downloadFile(drive, fileId, filePath) {
res.data res.data
.on('end', () => { .on('end', () => {
console.log(`Downloaded file: ${filePath}`); console.log(`Downloaded file: ${filePath}`);
console.log(`${filePath} : filePath`)
resolve(); resolve();
}) })
.on('error', (err) => { .on('error', (err) => {
@ -47,10 +49,10 @@ async function downloadFile(drive, fileId, filePath) {
async function syncFolder(drive, folderId, localPath) { async function syncFolder(drive, folderId, localPath) {
// List all files and subfolders in the folder // List all files and subfolders in the folder
const res = await drive.files.list({ const res = await drive.files.list({
q: `'${folderId}' in parents and trashed = false`, q: `'${folderId}' in parents and trashed = false`,
fields: 'files(id, name, mimeType)', fields: 'files(id, name, mimeType)',
}); });
const items = res.data.files; const items = res.data.files;
@ -92,7 +94,6 @@ async function runSync() {
// Create a Google Drive API instance // Create a Google Drive API instance
const drive = google.drive({ version: 'v3', auth: authClient }); const drive = google.drive({ version: 'v3', auth: authClient });
await syncFolder(drive, folderId, localDirectory); // Update with the path to your local folder await syncFolder(drive, folderId, localDirectory); // Update with the path to your local folder
} catch (error) { } catch (error) {

View File

@ -1,6 +1,7 @@
const { google } = require('googleapis'); const { google } = require('googleapis');
const privatekey = require('../config/access_token.json');
const sheet_data = require('../config/sheet_data.json'); const sheet_data = require('../config/sheet_data.json');
const xlsx = require('xlsx');
const path = require('path');
class EconomicSystem { class EconomicSystem {
constructor() { constructor() {
@ -13,104 +14,81 @@ class EconomicSystem {
} }
async fetchProductionData() { 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 { try {
const response = await sheets.spreadsheets.values.get({ const workbook = xlsx.readFile(path.resolve(__dirname, `../data/${sheet_data.revenue_spreadsheetName}`));
spreadsheetId: sheet_data.revenue_spreadsheetId, // Assuming the "TR-Expenses" sheet is the third sheet (index 2) of the workbook
range: sheet_data.TR_Expenses_range, const worksheet = workbook.Sheets[sheet_data.TR_Expenses_sheet];
valueRenderOption: 'FORMULA' const { s: startCell, e: endCell } = xlsx.utils.decode_range(sheet_data.TR_Expenses_range);
});
let productionSum = 0;
const consumptionData = response.data.values; for (let row = startCell.r; row <= endCell.r; row++) {
if (consumptionData) { for (let col = startCell.c; col <= endCell.c; col++) {
let sum = 0; const cellAddress = xlsx.utils.encode_cell({ r: row, c: col });
for (const row of consumptionData) { const cellValue = worksheet[cellAddress]?.v;
const value = parseFloat(row[0]); if (typeof cellValue === 'number'){
if (!isNaN(value)) { productionSum += cellValue;
sum += value; }
} }
}
this.productionCost = sum;
} }
this.productionCost = productionSum;
} catch (error) { } catch (error) {
console.error('Error fetching production data:', error); console.error('Error fetching production data:', error);
} return null;
}
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);
} }
} }
async fetchConsumptionData() { 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 { try {
const response = await sheets.spreadsheets.values.get({ const workbook = xlsx.readFile(path.resolve(__dirname, `../data/${sheet_data.revenue_spreadsheetName}`));
spreadsheetId: sheet_data.revenue_spreadsheetId, const worksheet = workbook.Sheets[sheet_data.TR_Consumption_sheet];
range: sheet_data.TR_Consumption_range, const { s: startCell, e: endCell } = xlsx.utils.decode_range(sheet_data.TR_Consumption_range);
valueRenderOption: 'FORMULA'
}); let consumptionSum = 0;
for (let row = startCell.r; row <= endCell.r; row++) {
const consumptionData = response.data.values; for (let col = startCell.c; col <= endCell.c; col++) {
if (consumptionData) { const cellAddress = xlsx.utils.encode_cell({ r: row, c: col });
let sum = 0; const cellValue = worksheet[cellAddress]?.v;
for (const row of consumptionData) { if (typeof cellValue === 'number'){
const value = parseFloat(row[0]); consumptionSum += cellValue;
if (!isNaN(value)) {
sum += value;
} }
} }
this.consumptionCost = sum
} }
this.consumptionCost = consumptionSum;
} catch (error) { } 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() { calculateConsumptionValuation() {
this.consumptionValuation = this.consumptionNumber * sheet_data.TR_Consumption_Valuation_Price_USD; this.consumptionValuation = this.consumptionNumber * sheet_data.TR_Consumption_Valuation_Price_USD;
return this.consumptionValuation; return this.consumptionValuation;
@ -120,10 +98,10 @@ class EconomicSystem {
this.productionValuation = this.productionCost; this.productionValuation = this.productionCost;
return this.productionValuation; return this.productionValuation;
} }
calculateSystemValuation() { calculateSystemValuation() {
return Math.max(this.productionValuation, this.consumptionValuation); return Math.max(this.productionValuation, this.consumptionValuation);
} }
} }
module.exports = EconomicSystem; module.exports = EconomicSystem;