Created
May 22, 2024 15:47
-
-
Save devmnj/3dbe0b0878b349cf07bf46209f2bbb3e to your computer and use it in GitHub Desktop.
Google Sheet API fetching in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {google} from 'googleapis' | |
export async function getSheetData() { | |
const auth = new google.auth.GoogleAuth({ | |
credentials: { | |
client_email: process.env.SERVICE_EMAIL, | |
private_key: process.env.SERVICE_KEY.replace(/\\n/g, "\n"), | |
}, | |
scopes: ['https://www.googleapis.com/auth/spreadsheets.readonly'] | |
}) | |
const sheets = google.sheets({version: 'v4'}) | |
const range = "nse-eq!a2:h1928" | |
try { | |
const res = await sheets.spreadsheets.values.get({ | |
spreadsheetId: process.env.SHEET_ID, auth: await auth.getClient(), | |
range | |
}) | |
return (await res.data) | |
} catch (e) { | |
console.error("Error fetching sheets", e) | |
return [] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment