Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: get accurate sheet count for uploaded documents

Marc Seitzfaa5cd03e474
diff --git a/lib/files/put-file.ts b/lib/files/put-file.ts
index 6ddc4e08..ed38eaa3 100644
--- a/lib/files/put-file.ts
+++ b/lib/files/put-file.ts
@@ -3,7 +3,10 @@ import { upload } from "@vercel/blob/client";
import { match } from "ts-pattern";
import { newId } from "@/lib/id-helper";
-import { getPagesCount } from "@/lib/utils/get-page-number-count";
+import {
+ getPagesCount,
+ getSheetsCount,
+} from "@/lib/utils/get-page-number-count";
import { SUPPORTED_DOCUMENT_TYPES } from "../constants";
@@ -116,10 +119,19 @@ const putFileInS3 = async ({
}
let numPages: number = 1;
+ // get page count for pdf files
if (file.type === "application/pdf") {
const body = await file.arrayBuffer();
numPages = await getPagesCount(body);
}
+ // get sheet count for excel files
+ else if (
+ SUPPORTED_DOCUMENT_TYPES.includes(file.type) &&
+ file.type !== "application/pdf"
+ ) {
+ const body = await file.arrayBuffer();
+ numPages = getSheetsCount(body);
+ }
return {
type: DocumentStorageType.S3_PATH,
diff --git a/lib/sheet/index.ts b/lib/sheet/index.ts
index 134c6351..a03d378b 100644
--- a/lib/sheet/index.ts
+++ b/lib/sheet/index.ts
@@ -16,9 +16,6 @@ const customSort = (a: string, b: string) => {
};
export const parseSheet = async ({ fileUrl }: { fileUrl: string }) => {
- let columnData: string[] | null = null;
- let rowData: RowData[] | null = null;
-
const response = await fetch(fileUrl);
const arrayBuffer = await response.arrayBuffer();
const data = new Uint8Array(arrayBuffer);
diff --git a/lib/utils/get-page-number-count.ts b/lib/utils/get-page-number-count.ts
index 1922bb9e..0387f56c 100644
--- a/lib/utils/get-page-number-count.ts
+++ b/lib/utils/get-page-number-count.ts
@@ -1,4 +1,5 @@
import { pdfjs } from "react-pdf";
+import * as XLSX from "xlsx";
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
@@ -6,3 +7,9 @@ export const getPagesCount = async (arrayBuffer: ArrayBuffer) => {
const pdf = await pdfjs.getDocument(arrayBuffer).promise;
return pdf.numPages;
};
+
+export const getSheetsCount = (arrayBuffer: ArrayBuffer) => {
+ const data = new Uint8Array(arrayBuffer);
+ const workbook = XLSX.read(data, { type: "array" });
+ return workbook.SheetNames.length ?? 1;
+};