docs.sheetjs.com/docz/static/next/[id].js

44 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2022-08-16 09:29:15 +00:00
import Head from 'next/head';
2023-05-26 22:50:23 +00:00
import { read, utils } from 'xlsx';
import base64 from "@/sheetjs.xlsx";
2022-08-16 09:29:15 +00:00
2023-05-26 22:50:23 +00:00
export default function Index({type, html, name}) { return ( <>
2022-08-16 09:29:15 +00:00
<Head>
<meta httpEquiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{`SheetJS Next.JS ${type} Demo`}</title>
</Head>
2023-05-26 22:50:23 +00:00
<h3>{`SheetJS Next.JS ${type} Demo`}</h3>
<p>
This demo reads from <code>/sheetjs.xlsx</code><br/><br/>
Sheet name: <b>{name}</b><br/>
</p>
<div dangerouslySetInnerHTML={{ __html: html }} />
</> ); }
2022-08-16 09:29:15 +00:00
let cache = [];
export async function getStaticProps(ctx) {
if(!cache || !cache.length) {
2023-05-26 22:50:23 +00:00
const wb = read(base64, {type: "base64"});
2022-08-16 09:29:15 +00:00
cache = wb.SheetNames.map((name) => ({ name, sheet: wb.Sheets[name] }));
}
const entry = cache[ctx.params.id];
return {
props: {
type: "getStaticPaths",
name: entry.name,
id: ctx.params.id.toString(),
html: entry.sheet ? utils.sheet_to_html(entry.sheet) : "",
},
}
}
export async function getStaticPaths() {
2023-05-26 22:50:23 +00:00
const wb = read(base64, {type: "base64"});
2022-08-16 09:29:15 +00:00
cache = wb.SheetNames.map((name) => ({ name, sheet: wb.Sheets[name] }));
return {
2023-05-26 22:50:23 +00:00
paths: wb.SheetNames.map((_, idx) => ({ params: { id: idx.toString() } })),
2022-08-16 09:29:15 +00:00
fallback: false,
};
}