Skip to content

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

feat: add sheet parsing

Marc Seitz6291dfae6102
diff --git a/lib/sheet/index.ts b/lib/sheet/index.ts
new file mode 100644
index 00000000..8048210a
--- /dev/null
+++ b/lib/sheet/index.ts
@@ -0,0 +1,47 @@
+import * as XLSX from "xlsx";
+
+type RowData = { [key: string]: any };
+
+// Custom sort function to sort keys A, B, .. Z, AA, AB, ..
+const customSort = (a: string, b: string) => {
+ if (a.length === b.length) {
+ return a.localeCompare(b);
+ }
+ return a.length - b.length;
+};
+
+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);
+ const workbook = XLSX.read(data, { type: "array" });
+ const firstSheetName = workbook.SheetNames[0];
+ const worksheet = workbook.Sheets[firstSheetName];
+
+ const json: RowData[] = XLSX.utils.sheet_to_json(worksheet, {
+ header: "A",
+ });
+
+ // Collect all unique keys from the JSON data
+ const allKeys = Array.from(new Set(json.flatMap(Object.keys)));
+
+ // Sort the keys alphabetically
+ allKeys.sort(customSort);
+
+ // Ensure each row has the same set of keys
+ const normalizedData = json.map((row) => {
+ const normalizedRow: RowData = {};
+ allKeys.forEach((key) => {
+ normalizedRow[key] = row[key] || "";
+ });
+ return normalizedRow;
+ });
+
+ columnData = allKeys;
+ rowData = normalizedData;
+
+ return { columnData, rowData };
+};
diff --git a/package-lock.json b/package-lock.json
index e1cd0e2f..e8a90012 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -111,6 +111,7 @@
"ts-pattern": "^5.1.1",
"typescript": "^5.3.3",
"ua-parser-js": "^1.0.37",
+ "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz",
"zod": "^3.23.8"
},
"devDependencies": {
@@ -18149,6 +18150,18 @@
}
}
},
+ "node_modules/xlsx": {
+ "version": "0.20.2",
+ "resolved": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz",
+ "integrity": "sha512-+nKZ39+nvK7Qq6i0PvWWRA4j/EkfWOtkP/YhMtupm+lJIiHxUrgTr1CcKv1nBk1rHtkRRQ3O2+Ih/q/sA+FXZA==",
+ "license": "Apache-2.0",
+ "bin": {
+ "xlsx": "bin/xlsx.njs"
+ },
+ "engines": {
+ "node": ">=0.8"
+ }
+ },
"node_modules/xmlhttprequest-ssl": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-2.0.0.tgz",
diff --git a/package.json b/package.json
index 15deddd4..ed84316e 100644
--- a/package.json
+++ b/package.json
@@ -120,6 +120,7 @@
"ts-pattern": "^5.1.1",
"typescript": "^5.3.3",
"ua-parser-js": "^1.0.37",
+ "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz",
"zod": "^3.23.8"
},
"devDependencies": {