feat: write data to sheet

This commit is contained in:
karishmas6
2024-09-18 19:54:31 +05:30
parent 0f66f36a48
commit 5de67998b3

View File

@@ -0,0 +1,35 @@
import { google } from "googleapis";
import fs from 'fs';
import path from 'path';
export async function writeDataToSheet(spreadsheetId: string, range: string, data: any[]) {
try {
const credentialsPath = path.join(__dirname, 'service_account_credentials.json');
const credentials = JSON.parse(fs.readFileSync(credentialsPath, 'utf-8'));
const auth = new google.auth.GoogleAuth({
credentials: {
client_email: credentials.client_email,
private_key: credentials.private_key,
},
scopes: ['https://www.googleapis.com/auth/spreadsheets'],
});
const authToken = await auth.getClient();
const sheets = google.sheets({ version: 'v4', auth: authToken as any });
const resource = { values: data };
await sheets.spreadsheets.values.append({
spreadsheetId,
range,
valueInputOption: 'USER_ENTERED',
requestBody: resource,
});
console.log(`Data written to Google Sheet: ${spreadsheetId}, Range: ${range}`);
} catch (error) {
console.error(`Error writing data to Google Sheet: ${error.message}`);
throw error;
}
}