Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const readline = require('readline');
- const {google} = require('googleapis');
- // If modifying these scopes, delete token.json.
- const TOKEN_PATH = 'token.json';
- // Load client secrets from a local file.
- fs.readFile('./GoogleFinanceApi/credentials.json', (err, content) => {
- if (err) return console.log('Error loading client secret file:', err);
- // Authorize a client with credentials, then call the Google Sheets API.
- authorize(JSON.parse(content), appendData);
- });
- function authorize(credentials, callback) {
- const {client_secret, client_id, redirect_uris} = credentials.installed;
- const oAuth2Client = new google.auth.OAuth2(
- client_id, client_secret, redirect_uris[0]);
- // Check if we have previously stored a token.
- fs.readFile(TOKEN_PATH, (err, token) => {
- if (err) return getNewToken(oAuth2Client, callback);
- oAuth2Client.setCredentials(JSON.parse(token));
- callback(oAuth2Client);
- });
- }
- function listMajors(auth) {
- const sheets = google.sheets({version: 'v4', auth});
- sheets.spreadsheets.values.get({
- spreadsheetId: '1ckHZsL2fnWVATmXljlewm-6qBo62B0qmu0w_2QdSpGA',
- range: 'Sheet1!A2:E',
- }, (err, res) => {
- if (err) return console.log('The API returned an error: ' + err);
- const rows = res.data.values;
- if (rows.length) {
- console.log('Name, Major:');
- // Print columns A and E, which correspond to indices 0 and 4.
- rows.map((row) => {
- console.log(`${row[0]}, ${row[4]}`);
- });
- } else {
- console.log('No data found.');
- }
- });
- }
- function appendData(auth) {
- var sheets = google.sheets('v4');
- sheets.spreadsheets.values.append({
- auth: auth,
- spreadsheetId: '1ckHZsL2fnWVATmXljlewm-6qBo62B0qmu0w_2QdSpGA',
- range: 'Sheet1!A2:B', //Change Sheet1 if your worksheet's name is something else
- valueInputOption: "USER_ENTERED",
- resource: {
- values: [ ["Void", "Canvas", "Website"], ["Paul", "Shan", "Human"] ]
- }
- }, (err, response) => {
- if (err) {
- console.log('The API returned an error: ' + err);
- return;
- } else {
- console.log("Appended");
- }
- });
- }
Add Comment
Please, Sign In to add comment