feat(wip): write to sheets
This commit is contained in:
@@ -18,3 +18,41 @@ router.post('/upload-credentials', (req, res) => {
|
|||||||
|
|
||||||
res.status(200).json({ message: 'Service Account credentials saved successfully.' });
|
res.status(200).json({ message: 'Service Account credentials saved successfully.' });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.post('/write-to-sheet', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { spreadsheetId, range, values } = req.body;
|
||||||
|
|
||||||
|
// Load the stored credentials
|
||||||
|
const credentialsPath = path.join(__dirname, 'service_account_credentials.json');
|
||||||
|
if (!fs.existsSync(credentialsPath)) {
|
||||||
|
return res.status(400).json({ message: 'No credentials found. Please provide credentials first.' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const credentials = JSON.parse(fs.readFileSync(credentialsPath, 'utf-8'));
|
||||||
|
|
||||||
|
// Authenticate with Google using the service account credentials
|
||||||
|
const auth = new google.auth.GoogleAuth({
|
||||||
|
credentials,
|
||||||
|
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
|
||||||
|
});
|
||||||
|
|
||||||
|
const sheets = google.sheets({ version: 'v4', auth });
|
||||||
|
|
||||||
|
// Write data to the provided Google Sheet and range
|
||||||
|
await sheets.spreadsheets.values.append({
|
||||||
|
spreadsheetId,
|
||||||
|
range,
|
||||||
|
valueInputOption: 'USER_ENTERED',
|
||||||
|
resource: {
|
||||||
|
values: values, // Expecting an array of arrays, like [['data1', 'data2'], ['data3', 'data4']]
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(200).json({ message: 'Data written to Google Sheet successfully.' });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error writing to sheet:', error);
|
||||||
|
res.status(500).json({ message: 'Failed to write to Google Sheet.', error });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Reference in New Issue
Block a user