Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use xlsx #26

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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