简体   繁体   English

复杂对象数据结构到xlsx

[英]Complex object data structure to xlsx

I have a javascript object that contains in itself other objects: 我有一个javascript对象,它本身包含其他对象:

{
    "10": {},
    "12": {
        "20": {
            "value": 1,
            "id": 1,
        },
        "100": {
            "value": 12,
            "id": 1,
        }
    },
    "14": {
        "100": {
            "value": 14,
            "id": 2,
        }
    },
    "16": {},
    "18": {},
    "20": {
        "100": {
            "value": 23,
            "id": 1,
        },
        "150": {
            "value": 56,
            "id": 3,
        }
    },
    "22": {},
    "24": {},
    "26": {
        "50": {
...

I want to export this as an xlsx file, but I have some issues doing so. 我想将此导出为xlsx文件,但是这样做有一些问题。

I have mainly resorted at using js-xlsx that is not very helpful regarding it's documentation, and alasql . 我主要采取的方法是使用js-xlsx ,它对文档和alasql的帮助不是很大。

Creating such a file for a simpler datastructure is quite easy. 创建这样的文件以简化数据结构非常容易。 But I have a few issues when trying to create it with my own structure. 但是,当尝试使用自己的结构创建它时,我遇到了一些问题。

Firstly trying to do it like this: 首先尝试这样做:

alasql('SELECT * INTO XLSX("test.xlsx",{headers:true}) FROM ?',[$scope.TestData]);

fails printing [Object] instead of the actual values. 无法打印[Object]而不是实际值。

Trying to create the cells myself I end up with an empty file. 我自己尝试创建单元格,但最终得到一个空文件。

function sheet_from_array_of_arrays(data, opts) {
    var ws = {};
    var range = {s: {c:10000000, r:10000000}, e: {c:0, r:0 }};
    var row1 = 0;
    for (var R in data) {
        if (data.hasOwnProperty(R)) {
            var row2 = 0;
            for (var C in data[R]) {
                if (data[R].hasOwnProperty(C)) {
                var col = 0;
                    for (var K in data[R][C]) {
                        if (data[R][C].hasOwnProperty(C)) {
                            var RR = row1 + row2;
                            var CC = col;
                            if(range.s.r > RR) range.s.r = RR;
                            if(range.s.c > CC) range.s.c = C;
                            if(range.e.r < RR) range.e.r = RR;
                            if(range.e.c < CC) range.e.c = CC;
                            var cell = {v: data[R][C][K] };
                            if(cell.v == null) continue;
                            var cell_ref = XLSX.utils.encode_cell({c:CC,r:RR});

                            if(typeof cell.v === 'number') cell.t = 'n';
                            else if(typeof cell.v === 'boolean') cell.t = 'b';
                            else if(cell.v instanceof Date) {
                                cell.t = 'n'; cell.z = XLSX.SSF._table[14];
                                cell.v = datenum(cell.v);
                            }
                            else cell.t = 's';

                            ws[cell_ref] = cell;

                            col = col + 1;
                        }
                    }
                    row2 = row2 + 1;
                }
            }
            row1 = row1 + 1;
        }
    }
    if(range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
    return ws;
}

function Workbook() {
    if(!(this instanceof Workbook)) return new Workbook();
    this.SheetNames = [];
    this.Sheets = {};
}

var wb = new Workbook(), ws = sheet_from_array_of_arrays($scope.TestData);

/* add worksheet to workbook */
wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] = ws;
var wbout = XLSX.write(wb, {bookType:'xlsx', bookSST:true, type: 'binary'});

function s2ab(s) {
    var buf = new ArrayBuffer(s.length);
    var view = new Uint8Array(buf);
    for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
    return buf;
}
saveAs(new Blob([s2ab(wbout)],{type:"application/octet-stream"}), "test.xlsx")

Is there a way for me to create an xlsx file with the object I presented above? 有没有办法用上面介绍的对象创建一个xlsx文件?

You can use SEARCH operator for parsing of nested JSON objects: 您可以使用SEARCH运算符来解析嵌套的JSON对象:

var data = {
"10": {},
"12": {
    "20": {
        "value": 1,
        "id": 1,
    },
    "100": {
        "value": 12,
        "id": 1,
    }
},
"14": {
    "100": {
        "value": 14,
        "id": 2,
    }
},
"16": {},
"18": {},
"20": {
    "100": {
        "value": 23,
        "id": 1,
    },
    "150": {
        "value": 56,
        "id": 3,
    }
}
};


  var res = alasql('SEARCH OF(@a) OF(@c) \
    RETURN(@a AS a,@c AS c, _->[value] AS [value], _->id AS id) \
    INTO XLSX("test406.xlsx",{headers:true}) \
    FROM ?',[data]);

Here: 这里:

  • SEARCH ... INTO XLSX FROM ? 从...搜索XLSX吗? - special search operator -特殊搜索运算符
  • OF(@v) - list of keys of the object (key value will be stored into variable OF(@v)-对象的键列表(键值将存储到变量中
  • RETURN(...) - result object RETURN(...)-结果对象
  • _ - current search value _-当前搜索值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM