Guest User

Untitled

a guest
Aug 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. const fs = require('fs');
  2. const readline = require('readline');
  3. const {google} = require('googleapis');
  4.  
  5. // If modifying these scopes, delete token.json.
  6. const TOKEN_PATH = 'token.json';
  7.  
  8. // Load client secrets from a local file.
  9. fs.readFile('./GoogleFinanceApi/credentials.json', (err, content) => {
  10. if (err) return console.log('Error loading client secret file:', err);
  11. // Authorize a client with credentials, then call the Google Sheets API.
  12. authorize(JSON.parse(content), appendData);
  13. });
  14.  
  15. function authorize(credentials, callback) {
  16. const {client_secret, client_id, redirect_uris} = credentials.installed;
  17. const oAuth2Client = new google.auth.OAuth2(
  18. client_id, client_secret, redirect_uris[0]);
  19.  
  20. // Check if we have previously stored a token.
  21. fs.readFile(TOKEN_PATH, (err, token) => {
  22. if (err) return getNewToken(oAuth2Client, callback);
  23. oAuth2Client.setCredentials(JSON.parse(token));
  24. callback(oAuth2Client);
  25. });
  26. }
  27.  
  28. function listMajors(auth) {
  29. const sheets = google.sheets({version: 'v4', auth});
  30. sheets.spreadsheets.values.get({
  31. spreadsheetId: '1ckHZsL2fnWVATmXljlewm-6qBo62B0qmu0w_2QdSpGA',
  32. range: 'Sheet1!A2:E',
  33. }, (err, res) => {
  34. if (err) return console.log('The API returned an error: ' + err);
  35. const rows = res.data.values;
  36. if (rows.length) {
  37. console.log('Name, Major:');
  38. // Print columns A and E, which correspond to indices 0 and 4.
  39. rows.map((row) => {
  40. console.log(`${row[0]}, ${row[4]}`);
  41. });
  42. } else {
  43. console.log('No data found.');
  44. }
  45. });
  46. }
  47. function appendData(auth) {
  48. var sheets = google.sheets('v4');
  49. sheets.spreadsheets.values.append({
  50. auth: auth,
  51. spreadsheetId: '1ckHZsL2fnWVATmXljlewm-6qBo62B0qmu0w_2QdSpGA',
  52. range: 'Sheet1!A2:B', //Change Sheet1 if your worksheet's name is something else
  53. valueInputOption: "USER_ENTERED",
  54. resource: {
  55. values: [ ["Void", "Canvas", "Website"], ["Paul", "Shan", "Human"] ]
  56. }
  57. }, (err, response) => {
  58. if (err) {
  59. console.log('The API returned an error: ' + err);
  60. return;
  61. } else {
  62. console.log("Appended");
  63. }
  64. });
  65. }
Add Comment
Please, Sign In to add comment