Skip to content

Instantly share code, notes, and snippets.

@devmnj
Created May 22, 2024 15:47
Show Gist options
  • Save devmnj/3dbe0b0878b349cf07bf46209f2bbb3e to your computer and use it in GitHub Desktop.
Save devmnj/3dbe0b0878b349cf07bf46209f2bbb3e to your computer and use it in GitHub Desktop.
Google Sheet API fetching in Javascript
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