deno.land / x / sheetjs@v0.18.3 / demos / database / ObjUtils.js

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* xlsx.js (C) 2013-present SheetJS -- http://sheetjs.com *//*global XLSX, module, require */var ObjUtils = (function() {
var X;if(typeof XLSX !== "undefined") X = XLSX;else if(typeof require !== 'undefined') X = require('xlsx');else throw new Error("Could not find XLSX");
function walk(obj, key, arr) { if(Array.isArray(obj)) return; if(typeof obj != "object" || obj instanceof Date) { arr.push({path:key, value:obj}); return; } Object.keys(obj).forEach(function(k) { walk(obj[k], key ? key + "." + k : k, arr); });}
function object_to_workbook(obj) { var wb = X.utils.book_new();
var base = []; walk(obj, "", base); var ws = X.utils.json_to_sheet(base, {header:["path", "value"]}); X.utils.book_append_sheet(wb, ws, "_keys");
Object.keys(obj).forEach(function(k) { if(!Array.isArray(obj[k])) return; X.utils.book_append_sheet(wb, X.utils.json_to_sheet(obj[k]), k); });
return wb;}
function deepset(obj, path, value) { if(path.indexOf(".") == -1) return obj[path] = value; var parts = path.split("."); if(!obj[parts[0]]) obj[parts[0]] = {}; return deepset(obj[parts[0]], parts.slice(1).join("."), value);}function workbook_set_object(obj, wb) { var ws = wb.Sheets["_keys"]; if(ws) { var data = X.utils.sheet_to_json(ws, {raw:true}); data.forEach(function(r) { deepset(obj, r.path, r.value); }); } wb.SheetNames.forEach(function(n) { if(n == "_keys") return; obj[n] = X.utils.sheet_to_json(wb.Sheets[n], {raw:true}); });}
function workbook_to_object(wb) { var obj = {}; workbook_set_object(obj, wb); return obj; }
return { workbook_to_object: workbook_to_object, object_to_workbook: object_to_workbook, workbook_set_object: workbook_set_object};})();
if(typeof module !== 'undefined') module.exports = ObjUtils;
sheetjs

Version Info

Tagged at
2 years ago