docs.sheetjs.com/docz/static/tauri/App.vue

108 lines
3.4 KiB
Vue
Raw Normal View History

2022-08-14 02:10:41 +00:00
<script setup lang="ts">
import { ref, shallowRef, onMounted } from 'vue'
import { read, write, utils, version, WorkBook } from 'xlsx';
import { DialogFilter, message, open, save } from '@tauri-apps/api/dialog';
import { fetch, ResponseType } from '@tauri-apps/api/http';
import { readBinaryFile, writeBinaryFile } from '@tauri-apps/api/fs';
const ver = ref(version);
const data = shallowRef<any[][]>([[]])
const origin = ref("");
const update = (wb: WorkBook) => {
const ws = wb.Sheets[wb.SheetNames[0]];
data.value = utils.sheet_to_json<any[]>(ws, { header: 1})
};
2024-04-26 04:16:13 +00:00
/* Download from https://docs.sheetjs.com/pres.numbers */
2022-08-14 02:10:41 +00:00
onMounted(async() => {
try {
2024-04-26 04:16:13 +00:00
origin.value = "https://docs.sheetjs.com/pres.numbers";
const response = await fetch<Uint8Array>("https://docs.sheetjs.com/pres.numbers", { method: "GET", responseType: ResponseType.Binary });
2022-08-14 02:10:41 +00:00
const wb = read(new Uint8Array(response.data));
update(wb);
} catch(e) { await message((e as Error).message || (e as string), { title: "Fetch Error", type: "error"}); }
});
const filters: DialogFilter[] = [
{name: "Excel Binary Workbook", extensions: ["xlsb"]},
{name: "Excel Workbook", extensions: ["xlsx"]},
{name: "Excel 97-2004 Workbook", extensions: ["xls"]},
{name: "Excel 2003 XML Spreadsheet", extensions: ["xml"]},
{name: "Symbolic Link", extensions: ["slk"]},
{name: "Flat OpenDocument Spreadsheet", extensions: ["fods"]},
{name: "OpenDocument Spreadsheet", extensions: ["fods"]},
// ...
];
/* Load from File */
const openFile = async() => {
try {
const selected = await open({
title: "Open Spreadsheet",
multiple: false,
directory: false,
filters
}) as string;
const d = await readBinaryFile(selected);
const wb = read(d);
update(wb);
origin.value = selected;
} catch(e) { await message((e as Error).message || (e as string), { title: "Load Error", type: "error"}); }
};
/* Save to File */
const saveFile = async() => {
try {
const selected = await save({
title: "Save to Spreadsheet",
filters
});
2023-01-21 10:50:57 +00:00
if(!selected) throw new Error("No file selected");
2022-08-14 02:10:41 +00:00
const ws = utils.aoa_to_sheet(data.value);
const wb = utils.book_new();
utils.book_append_sheet(wb, ws, "SheetJSTauri");
const d = write(wb, {type: "buffer", bookType: selected.slice(selected.lastIndexOf(".") + 1) as any}) as Uint8Array;
await writeBinaryFile(selected, d);
await message(`File saved to ${selected}`);
} catch(e) { await message((e as Error).message || (e as string), { title: "Save Error", type: "error"}); }
};
</script>
<template>
<div>
<h1><a href="https://sheetjs.com" target="_blank">
<img src="https://sheetjs.com/sketch128.png" class="logo" alt="SheetJS" />
SheetJS × Tauri {{ ver }}</a></h1>
2023-02-12 08:15:17 +00:00
<div class="centre"><button type="button" @click="openFile()">Load Data</button> or
<button type="button" @click="saveFile()">Save Data</button></div>
<p class="centre"><b class="centre">Data from {{ origin }}</b></p>
2022-08-14 02:10:41 +00:00
<table class="center"><tbody>
<tr v-for="(row, index) in data" v-bind:key="index">
<td v-for="(cell, col) in row" v-bind:key="col">
{{ cell }}
</td>
</tr>
</tbody></table>
</div>
</template>
<style scoped>
.logo {
2023-02-12 08:15:17 +00:00
padding: 0px;
2022-08-14 02:10:41 +00:00
height: 64px; width: 64px;
2023-02-12 08:15:17 +00:00
vertical-align: middle;
2022-08-14 02:10:41 +00:00
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {
filter: drop-shadow(0 0 2em #42b883aa);
}
2023-02-12 08:15:17 +00:00
.centre { text-align: center; }
2022-08-14 02:10:41 +00:00
table.center {
margin-left: auto;
margin-right: auto;
}
</style>