Skip to content

Instantly share code, notes, and snippets.

@sagarpanda
Created April 3, 2020 06:20
Show Gist options
  • Select an option

  • Save sagarpanda/1023f6c7a6260e0cb2322e27cba0762d to your computer and use it in GitHub Desktop.

Select an option

Save sagarpanda/1023f6c7a6260e0cb2322e27cba0762d to your computer and use it in GitHub Desktop.
async function fetchGoogleSheetFeed({spreadsheetId, sheetNum}) {
const tab = sheetNum || 1;
const fetchUrl = `https://spreadsheets.google.com/feeds/cells/${spreadsheetId}/${tab}/public/full?alt=json`;
const data = await fetch(fetchUrl).then(response => {
if(response.status === 200) {
return response.json();
} else {
return { feed: { entry: [] }};
}
});
let count = 0;
const rowData = data.feed.entry.reduce((prev, curr) => {
const { row, col } = curr['gs$cell'];
const content = curr.content['$t'];
const rowIndex = row - 1;
const colIndex = col - 1;
if(prev[rowIndex]) {
prev[rowIndex][`col${colIndex}`] = content;
} else {
prev[rowIndex] = { [`col${colIndex}`]: content };
}
return prev;
}, []);
return rowData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment