Skip to content

Commit

Permalink
Add xls
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaudiomv committed Feb 18, 2024
1 parent 321a576 commit ce0e70e
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 65 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BMLT Data Converter

- Converts BMLT data from JSON to CSV or KML
- Converts BMLT data from JSON to CSV, XLS or KML
- Note that export of KML is only supported with GetSearchResults.
- Use your semantic interface to build query url

Expand Down
132 changes: 73 additions & 59 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-svelte": "^2.35.1",
"fetch-jsonp": "^1.3.0",
"gh-pages": "^6.1.1",
"prettier": "^3.1.1",
"prettier-plugin-svelte": "^3.1.2",
"svelte": "^4.2.7",
"svelte-check": "^3.6.0",
"tslib": "^2.4.1",
"typescript": "^5.0.0",
"vite": "^5.0.3"
"vite": "^5.0.3",
"gh-pages": "^6.1.1",
"fetch-jsonp": "^1.3.0",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz"
},
"type": "module"
}
11 changes: 9 additions & 2 deletions src/lib/DataConverter.svelte
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<script lang="ts">
import { onDestroy } from 'svelte';
import { writable } from 'svelte/store';
import { fetchData, exportCSV, exportKML } from './DataUtils';
import { fetchData, exportCSV, exportXLS, exportKML } from './DataUtils';
const processing = writable<boolean>(false);
const errorMessage = writable<string>('');
const loadingText = writable<string>('');
let query: string = '';
let csvDownloadUrl: string = '';
let xlsDownloadUrl: string = '';
let kmlDownloadUrl: string = '';
let interval: number | undefined;
Expand Down Expand Up @@ -38,6 +39,7 @@
errorMessage.set('');
loadingText.set('Processing');
csvDownloadUrl = '';
xlsDownloadUrl = '';
kmlDownloadUrl = '';
startLoadingAnimation();
Expand All @@ -46,6 +48,8 @@
if (data && data.length > 0) {
// Process data for CSV
csvDownloadUrl = exportCSV(data);
// Process data for XLS
xlsDownloadUrl = exportXLS(data);
// Process data for KML
kmlDownloadUrl = query.includes('GetSearchResults') ? exportKML(data) : '';
} else {
Expand Down Expand Up @@ -87,10 +91,13 @@
{#if csvDownloadUrl}
<a href={csvDownloadUrl} class="download-links" download="BMLT_data.csv">Download CSV</a><br />
{/if}
{#if xlsDownloadUrl}
<a href={xlsDownloadUrl} class="download-links" download="BMLT_data.xls">Download XLS</a><br />
{/if}
{#if kmlDownloadUrl}
<a href={kmlDownloadUrl} class="download-links" download="BMLT_data.kml">Download KML</a>
{/if}
<div id="description">Converts BMLT data from JSON to CSV or KML</div>
<div id="description">Converts BMLT data from JSON to CSV, XLS or KML</div>
</div>
<div id="footer">
<a href="https://github.com/bmlt-enabled/bmlt-data-converter/issues" class="footer-link" target="_blank">Issues?</a>
Expand Down
33 changes: 33 additions & 0 deletions src/lib/DataUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fetchJsonp from 'fetch-jsonp';
import * as XLSX from 'xlsx';

interface Meeting {
meeting_name: string;
Expand Down Expand Up @@ -70,6 +71,38 @@ export function exportCSV(data: any[]): string {
return URL.createObjectURL(blob);
}

export function exportXLS(data: any[]): string {
if (!Array.isArray(data) || data.length === 0) {
throw new Error('No data found');
}

const processedData = data.map((row) =>
Object.keys(row).reduce((acc, key) => {
let value: string | number = row[key];
if (typeof value === 'string' && value.includes('#@-@#')) {
[, value] = value.split('#@-@#');
}
acc[key] = value;
return acc;
}, {} as any)
);

const wb = XLSX.utils.book_new();
const ws = XLSX.utils.json_to_sheet(processedData);
XLSX.utils.book_append_sheet(wb, ws, 'Data');
const wbout = XLSX.write(wb, { bookType: 'xls', type: 'binary' });

function s2ab(s: string): ArrayBuffer {
const buf = new ArrayBuffer(s.length);
const view = new Uint8Array(buf);
for (let i = 0; i < s.length; i++) view[i] = s.charCodeAt(i) & 0xff;
return buf;
}

const blob = new Blob([s2ab(wbout)], { type: 'application/vnd.ms-excel' });
return URL.createObjectURL(blob);
}

export function exportKML(data: Meeting[]): string {
const placemarks = data.map(createPlacemark).filter(Boolean).join('\n');

Expand Down

0 comments on commit ce0e70e

Please sign in to comment.