-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathxls.js
238 lines (208 loc) · 9.08 KB
/
xls.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/////////////////////////////////////////////////////////////////////
// Copyright (c) Autodesk, Inc. All rights reserved
// Written by Forge Partner Development
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.
//
// AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
/////////////////////////////////////////////////////////////////////
if (!window.jQuery) alert('jQuery is required for this sample');
if (!window.XLSX) alert('Sheet JS is required for this sample');
var ForgeXLS = {
Utility: {
Constants: {
BASE_URL: 'https://developer.api.autodesk.com',
MODEL_DERIVATIVE_V2: '/modelderivative/v2/designdata/'
},
forgeGetRequest: function(url, token, callback) {
jQuery.ajax({
url: url,
beforeSend: function(request) {
request.setRequestHeader('Authorization', 'Bearer ' + token);
},
success: function(response) {
if (response.result && response.result === 'success') {
setTimeout(function() {
console.log('Data not ready... retry in 1 second');
ForgeXLS.Utility.forgeGetRequest(url, token, callback);
}, 1000);
return;
}
if (callback)
callback(response);
}
});
},
getMetadata: function(urn, token, callback) {
console.log('Downloading metadata...');
this.forgeGetRequest(this.Constants.BASE_URL + this.Constants.MODEL_DERIVATIVE_V2 + urn + '/metadata', token, callback);
},
getHierarchy: function(urn, guid, token, callback) {
console.log('Downloading hierarchy...');
this.forgeGetRequest(this.Constants.BASE_URL + this.Constants.MODEL_DERIVATIVE_V2 + urn + '/metadata/' + guid, token, callback);
},
getProperties: function(urn, guid, token, callback) {
console.log('Downloading properties...');
this.forgeGetRequest(this.Constants.BASE_URL + this.Constants.MODEL_DERIVATIVE_V2 + urn + '/metadata/' + guid + '/properties', token, callback);
}
},
downloadXLSX: function(urn, token, status) {
var fileName = decodeURIComponent(atob(urn).replace(/^.*[\\\/]/, '')) + '.xlsx';
if (fileName.indexOf('.rvt') == -1) {
if (status) status(true, 'Not a Revit file, aborting.');
return;
}
if (status) {
status(false, 'Preparing ' + fileName);
status(false, 'Reading project information....');
}
this.prepareTables(urn, token, function(tables) {
if (status) status(false, 'Building XLSX file...');
var wb = new Workbook();
jQuery.each(tables, function(name, table) {
if (name.indexOf('<') == -1) { // skip tables starting with <
var ws = ForgeXLS.sheetFromTable(table);
wb.SheetNames.push(name);
wb.Sheets[name] = ws;
}
});
var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'binary' });
saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), fileName);
if (status) status(true, 'Downloading...');
})
},
sheetFromTable: function(table) {
var ws = {};
var range = { s: { c: 10000000, r: 10000000 }, e: { c: 0, r: 0 } };
var allProperties = [];
table.forEach(function(object) {
jQuery.each(object, function(propName, propValue) {
if (allProperties.indexOf(propName) == -1)
allProperties.push(propName);
})
});
table.forEach(function(object) {
allProperties.forEach(function(propName) {
if (!object.hasOwnProperty(propName))
object[propName] = '';
});
});
var propsNames = [];
for (var propName in table[0]) {
propsNames.push(propName);
}
//propsNames.sort(); // removed due first 3 ID columns
var R = 0;
var C = 0;
for (; C != propsNames.length; ++C) {
var cell_ref = XLSX.utils.encode_cell({ c: C, r: R });
ws[cell_ref] = { v: propsNames[C], t: 's' };
}
R++;
for (var index = 0; index != table.length; ++index) {
C = 0;
propsNames.forEach(function(propName) {
if (range.s.r > R) range.s.r = 0;
if (range.s.c > C) range.s.c = 0;
if (range.e.r < R) range.e.r = R;
if (range.e.c < C) range.e.c = C;
var cell = { v: table[index][propName] };
if (cell.v == null) return;
var cell_ref = XLSX.utils.encode_cell({ c: C, r: R });
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;
C++;
});
R++;
}
if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range);
return ws;
},
prepareTables: function(urn, token, callback) {
this.Utility.getMetadata(urn, token, function(metadata) {
if (metadata.data.metadata.length == 0) {
alert('Unexpected metadata');
return;
}
var guid = metadata.data.metadata[0].guid;
ForgeXLS.Utility.getHierarchy(urn, guid, token, function(hierarchy) {
ForgeXLS.Utility.getProperties(urn, guid, token, function(properties) {
callback(ForgeXLS.prepareRawData(hierarchy, properties));
});
});
});
},
prepareRawData: function(hierarchy, properties) {
var tables = {};
hierarchy.data.objects[0].objects.forEach(function(category) {
var idsOnCategory = [];
ForgeXLS.getAllElementsOnCategory(idsOnCategory, category.objects);
var rows = [];
idsOnCategory.forEach(function(objectid) {
var columns = ForgeXLS.getProperties(objectid, properties);
rows.push(columns);
});
tables[category.name] = rows;
});
return tables;
},
getAllElementsOnCategory: function(ids, category) {
category.forEach(function(item) {
if (typeof(item.objects) === 'undefined') {
if (!ids.indexOf(item.objectid) >= 0)
ids.push(item.objectid);
} else
ForgeXLS.getAllElementsOnCategory(ids, item.objects);
});
},
getProperties: function(id, objCollection) {
var data = {};
objCollection.data.collection.forEach(function(obj) {
if (obj.objectid != id) return;
data['Viewer ID'] = id;
data['Revit ID'] = obj.name.match(/\d+/g)[0];
data['Name'] = obj.name.replace('[' + data['Revit ID'] + ']', '').trim();
for (var propGroup in obj.properties) {
if (propGroup.indexOf('__') > -1) break;
if (obj.properties.hasOwnProperty(propGroup)) {
for (var propName in obj.properties[propGroup]) {
if (obj.properties[propGroup].hasOwnProperty(propName) && !Array.isArray(obj.properties[propGroup][propName]))
data[propGroup + ':' + propName] = obj.properties[propGroup][propName];
}
}
}
});
return data;
}
};
function Workbook() {
if (!(this instanceof Workbook)) return new Workbook();
this.SheetNames = [];
this.Sheets = {};
}
function datenum(v, date1904) {
if (date1904) v += 1462;
var epoch = Date.parse(v);
return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
}
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;
}