Skip to content

Commit

Permalink
use xlsx (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjaudiomv authored Feb 18, 2024
1 parent e980703 commit 344cc0e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 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, XLS or KML
- Converts BMLT data from JSON to CSV, XLSX or KML
- Note that export of KML is only supported with GetSearchResults.
- Use your semantic interface to build query url

Expand Down
16 changes: 8 additions & 8 deletions src/lib/DataConverter.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<script lang="ts">
import { onDestroy } from 'svelte';
import { writable } from 'svelte/store';
import { fetchData, exportCSV, exportXLS, exportKML } from './DataUtils';
import { fetchData, exportCSV, exportXLSX, 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 xlsxDownloadUrl: string = '';
let kmlDownloadUrl: string = '';
let interval: number | undefined;
Expand Down Expand Up @@ -39,7 +39,7 @@
errorMessage.set('');
loadingText.set('Processing');
csvDownloadUrl = '';
xlsDownloadUrl = '';
xlsxDownloadUrl = '';
kmlDownloadUrl = '';
startLoadingAnimation();
Expand All @@ -48,8 +48,8 @@
if (data && data.length > 0) {
// Process data for CSV
csvDownloadUrl = exportCSV(data);
// Process data for XLS
xlsDownloadUrl = exportXLS(data);
// Process data for XLSX
xlsxDownloadUrl = exportXLSX(data);
// Process data for KML
kmlDownloadUrl = query.includes('GetSearchResults') ? exportKML(data) : '';
} else {
Expand Down Expand Up @@ -91,13 +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 xlsxDownloadUrl}
<a href={xlsxDownloadUrl} class="download-links" download="BMLT_data.xlsx">Download XLSX</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, XLS or KML</div>
<div id="description">Converts BMLT data from JSON to CSV, XLSX 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
6 changes: 3 additions & 3 deletions src/lib/DataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ export function exportCSV(data: any[]): string {
return URL.createObjectURL(blob);
}

export function exportXLS(data: any[]): string {
export function exportXLSX(data: any[]): string {
if (!Array.isArray(data) || data.length === 0) {
throw new Error('No data found');
}
const processedData = processExportData(data);
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' });
const wbout = XLSX.write(wb, { bookType: 'xlsx', 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' });
const blob = new Blob([s2ab(wbout)], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
return URL.createObjectURL(blob);
}

Expand Down

0 comments on commit 344cc0e

Please sign in to comment.