Skip to content

Instantly share code, notes, and snippets.

@usefulthink
Created May 4, 2017 10:37
Show Gist options
  • Save usefulthink/ebf60a0df6732fa0a91ab0569fa39a2e to your computer and use it in GitHub Desktop.
Save usefulthink/ebf60a0df6732fa0a91ab0569fa39a2e to your computer and use it in GitHub Desktop.
function makeLessCrappy(crappyData) {
const entries = crappyData.feed.entry;
const data = [];
const tmp = entries.map(entry => ({
address: entry.id.$t.replace(/.*\/R(\d+)C(\d+)$/, '$1,$2').split(',').map(Number),
content: entry.content.$t
}));
tmp.forEach(({address, content}) => {
const [row, col] = address;
if(!data[row - 1]) { data[row - 1] = []; }
data[row - 1][col - 1] = content.trim();
});
return data;
}
const columns = [
'backtrack:startTime', 'backtrack:duration', 'backtrack:number',
'-', 'backtrack:who', 'backtrack:what', '-',
'sidetrack:startTime', 'sidetrack:duration', 'sidetrack:number',
'-', 'sidetrack:who', 'sidetrack:what', '-'
];
function structureData(lessCrappyData) {
let day = 0;
const mergedRecords = {};
for (let row = 2, nRows = lessCrappyData.length; row < nRows; row++) {
if (row === 29) {
day++;
}
if (!lessCrappyData[row]) { continue; }
const tracks = {};
for (let col = 0, nCols = lessCrappyData[row].length; col < nCols; col++) {
if (!columns[col] || columns[col] === '-') { continue; }
const [track, field] = columns[col].split(':');
if (!tracks[track]) {
tracks[track] = {};
}
tracks[track][field] = lessCrappyData[row][col];
}
Object.keys(tracks).forEach(track => {
if (!mergedRecords[track]) {
mergedRecords[track] = [];
}
if (!mergedRecords[track][day]) {
mergedRecords[track][day] = [];
}
mergedRecords[track][day].push(tracks[track]);
});
}
return mergedRecords;
}
function cleanup(data) {
Object.keys(data).forEach(trackId => {
const track = data[trackId];
track.forEach((talks, day) => {
data[trackId][day] = talks.filter(talk => Boolean(talk.what));
})
});
return data;
}
fetch('https://spreadsheets.google.com/feeds/cells/1kjFshBwdJzAz4IT-02ZTPUTtQYYl4zk9IxuwsohOTos/od4/public/basic?alt=json')
.then(res => res.json())
.then(makeLessCrappy)
.then(structureData)
.then(cleanup)
.then(data => console.log(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment