Skip to content

Instantly share code, notes, and snippets.

@kamrankhan54
Last active September 10, 2023 12:45
Show Gist options
  • Save kamrankhan54/883238abbab5ad1425bdecf4e9c91d89 to your computer and use it in GitHub Desktop.
Save kamrankhan54/883238abbab5ad1425bdecf4e9c91d89 to your computer and use it in GitHub Desktop.
Google Spreadsheets Export to JSON
import gs from 'fast-gs';
import axios from 'axios';
// Needed to throttle data coming in from Google spreadsheets
const RateLimiter = require('limiter').RateLimiter;
const limiter = new RateLimiter(50, 'second');
// Access details for worksheet
const googleKey = '';
const googleSheetId = ;
// Gets data from Google spreadsheets in JSON format
function getDataFromGoogleSpreadsheet(cb) {
gs.toObject(googleKey, googleSheetId, (err, data) => {
if (err) { cb(err); return; }
cb(null, data);
});
}
function importData(data) {
axios({
url: '', // endpoint for posting data
method: 'post',
data: { testA: data.key, testB: data.key }, // Example post data
})
.then((res) => {
// Validate if successful
})
.catch((er) => {
// Check for error
});
}
// Running script
getDataFromGoogleSpreadsheet((err, result) => {
if (err) { console.error('Unable to retrieve data from google spreadsheet: ' + err)); return; }
result.forEach((data) => {
limiter.removeTokens(1, (errd, throttling) => {
importData(data);
})
.catch((er) => {
// Error logs
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment