Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Get spreadsheet npm package
- const { GoogleSpreadsheet } = require('google-spreadsheet');
- // Ensure you've updated this file with your client secret
- const clientSecret = require('./client_secret.json');
- // Add your Google sheet ID here
- const googleSheetID = '1234567891234567898765465431';
- // Instantiates the spreadsheet
- const sheet = new GoogleSpreadsheet(googleSheetID);
- // Asynchronously get the data
- async function getData() {
- try {
- // Authenticate using the JSON file we set up earlier
- await sheet.useServiceAccountAuth(clientSecret);
- await sheet.loadInfo();
- // Get the first tab's data
- const tab = sheet.sheetsByIndex[0];
- // Get row data
- const rows = await tab.getRows();
- // Empty array for our data
- let data = [];
- // If we have data
- if (rows.length > 0) {
- // Iterate through the array of rows
- // and push the clean data from your spreadsheet
- rows.forEach(row => {
- data.push(cleanData(row));
- });
- } else {
- return false;
- }
- console.log(JSON.stringify(data));
- // Return the data JSON encoded
- return JSON.stringify(data);
- } catch (err) {
- console.log(err);
- return false;
- }
- }
- // Add the data we want into an object
- function cleanData(data) {
- return {
- title: data['title'],
- start: data['start'],
- duration: data['totalDuration'],
- end: data['end'],
- organizer: data['organizer'],
- participants: data['participants']
- }
- }
- // Call the above method
- getData();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement