-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d5c8ca
commit 0e7b09c
Showing
1 changed file
with
45 additions
and
44 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,61 @@ | ||
const fs = require('fs').promises; | ||
const path = require('path'); | ||
const process = require('process'); | ||
const {authenticate} = require('@google-cloud/local-auth'); | ||
const {google} = require('googleapis'); | ||
const { authenticate } = require('@google-cloud/local-auth'); | ||
const { google } = require('googleapis'); | ||
const SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']; | ||
const TOKEN_PATH = path.join(process.cwd(), 'token.json'); | ||
const CREDENTIALS_PATH = path.join(process.cwd(), 'credentials.json'); | ||
async function loadSavedCredentialsIfExist() { | ||
try { | ||
const content = await fs.readFile(TOKEN_PATH); | ||
const credentials = JSON.parse(content); | ||
return google.auth.fromJSON(credentials); | ||
} catch (err) { | ||
return null; | ||
} | ||
try { | ||
const content = await fs.readFile(TOKEN_PATH); | ||
const credentials = JSON.parse(content); | ||
return google.auth.fromJSON(credentials); | ||
} catch (err) { | ||
return null; | ||
} | ||
} | ||
async function saveCredentials(client) { | ||
const content = await fs.readFile(CREDENTIALS_PATH); | ||
const keys = JSON.parse(content); | ||
const key = keys.installed || keys.web; | ||
const payload = JSON.stringify({ | ||
type: 'authorized_user', | ||
client_id: key.client_id, | ||
client_secret: key.client_secret, | ||
refresh_token: client.credentials.refresh_token, | ||
}); | ||
await fs.writeFile(TOKEN_PATH, payload); | ||
const content = await fs.readFile(CREDENTIALS_PATH); | ||
const keys = JSON.parse(content); | ||
const key = keys.installed || keys.web; | ||
const payload = JSON.stringify({ | ||
type: 'authorized_user', | ||
client_id: key.client_id, | ||
client_secret: key.client_secret, | ||
refresh_token: client.credentials.refresh_token, | ||
}); | ||
await fs.writeFile(TOKEN_PATH, payload); | ||
} | ||
|
||
async function authorize() { | ||
let client = await loadSavedCredentialsIfExist(); | ||
if (client) { | ||
let client = await loadSavedCredentialsIfExist(); | ||
if (client) { | ||
return client; | ||
} | ||
client = await authenticate({ | ||
scopes: SCOPES, | ||
keyfilePath: CREDENTIALS_PATH, | ||
}); | ||
if (client.credentials) { | ||
await saveCredentials(client); | ||
} | ||
return client; | ||
} | ||
client = await authenticate({ | ||
scopes: SCOPES, | ||
keyfilePath: CREDENTIALS_PATH, | ||
}); | ||
if (client.credentials) { | ||
await saveCredentials(client); | ||
} | ||
return client; | ||
} | ||
async function listMajors(auth) { | ||
const sheets = google.sheets({version: 'v4', auth}); | ||
const res = await sheets.spreadsheets.values.get({ | ||
//Redirecting you to https://docs.google.com/spreadsheets/d/e/2PACX-1vRBXBdqpurvBmR--bzj9RJmgr7HxAoWVZmlwmhaBK-LYf_BbXn8iAPdH-ogBtXiAwxlTkQgn45PkeRW/pubhtml?gid=406946200&single=true | ||
spreadsheetId: '2PACX-1vRBXBdqpurvBmR--bzj9RJmgr7HxAoWVZmlwmhaBK-LYf_BbXn8iAPdH-ogBtXiAwxlTkQgn45PkeRW', | ||
range: 'Class Data!A2:E', | ||
}); | ||
const rows = res.data.values; | ||
if (!rows || rows.length === 0) { | ||
console.log('No data found.'); | ||
return; | ||
} | ||
console.log(rows); | ||
async function getTime(auth) { | ||
const sheets = google.sheets({ version: 'v4', auth }); | ||
const res = await sheets.spreadsheets.values.get({ | ||
spreadsheetId: '1N-qbsQtT4G-2kMjQRQIKoRrh0Tw4424evuK9xnSSDpo', | ||
range: '150cc (Meta)!F7:G7', | ||
}); | ||
const rows = res.data.values; | ||
console.log(rows); | ||
} | ||
|
||
authorize().then(listMajors).catch(console.error); | ||
authorize().then(listMajors).catch(console.error); | ||
|
||
//7, 25,43 | ||
//1, 19, 36 | ||
function getTrackNumberOnSheet(int) { | ||
const row = Math.ceil(int / 4); | ||
} |