Skip to content

Instantly share code, notes, and snippets.

@uranusjr
Last active January 19, 2022 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uranusjr/b5f8e51c86bfbf63996683b3546f5687 to your computer and use it in GitHub Desktop.
Save uranusjr/b5f8e51c86bfbf63996683b3546f5687 to your computer and use it in GitHub Desktop.
{
"ambient": { // Ambient sensor.
"ambient": [{x: timestamp, y: temperature}, /* more... */]
},
"1-1": { // Silo 1, cable 1.
"1": [{x: timestamp, y: temperature}, /* more... */], // Node 1.
"2": [{x: timestamp, y: temperature}, /* more... */] // Node 2.
}
}
import {DateTime} from 'luxon'
import xlsx from 'xlsx'
import i18n from '@/i18n'
function formatSheetName(key) {
// TODO: Silo alias.
return key === 'ambient' ? i18n.t('ambientLabel') : key
}
function formatNodeName(key) {
return key === 'ambient' ? i18n.t('ambientLabel') : key
}
const baseDate = new Date(1899, 11, 30, 0, 0, 0)
function timestampToExcelDate(timestamp, timezone) {
const v = DateTime
.fromMillis(timestamp, {zone: 'utc'})
.setZone(timezone)
.toJSDate()
const dnthresh = baseDate.getTime() +
(v.getTimezoneOffset() - baseDate.getTimezoneOffset()) * 60000
return (v.getTime() - dnthresh) / (24 * 60 * 60 * 1000)
}
export function exportTemperatures(tabularData, timezone, filename) {
const book = xlsx.utils.book_new()
for (const [key, entry] of Object.entries(tabularData)) {
const aoa = [['', ...entry.nodes.map(formatNodeName)]]
for (const [ts, data] of Object.entries(entry.data)) {
const timestamp = Number(ts)
if (Number.isNaN(timestamp)) {
continue
}
const date = timestampToExcelDate(timestamp)
const dateCell = {v: date, t: 'n', z: 'yyyy-mm-dd" "hh:MM:ss'}
const temperatures = entry.nodes.map((nid) => {
const value = data[nid]
const temperature = Number(value)
return Number.isNaN(temperature) ? (value || '') : temperature
})
aoa.push([dateCell, ...temperatures])
}
const sheet = xlsx.utils.aoa_to_sheet(aoa)
sheet['!cols'] = [{wch: 16}, {wch: 8}]
xlsx.utils.book_append_sheet(book, sheet, formatSheetName(key))
}
xlsx.writeFile(book, filename)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment