-
Notifications
You must be signed in to change notification settings - Fork 14
/
WordApp.js
93 lines (82 loc) · 2.52 KB
/
WordApp.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
// --- WordApp ---
(function(r) {
var WordApp;
WordApp = (function() {
var disassembleWord, getXmlObj, parsDOCX, putError, putInternalError;
class WordApp {
constructor(blob_) {
this.name = "WordApp";
if (!blob_ || blob_.getContentType() !== MimeType.MICROSOFT_WORD) {
throw new Error("Please set the blob of data of DOCX format.");
}
this.obj = {
excel: blob_
};
this.contentTypes = "[Content_Types].xml";
this.document = "word/document.xml";
this.mainObj = {};
parsDOCX.call(this);
}
// --- begin methods
getTableColumnWidth() {
var body, n1, obj, root, xmlObj;
if (this.mainObj.fileObj.hasOwnProperty(this.document)) {
xmlObj = getXmlObj.call(this, this.document);
root = xmlObj.getRootElement();
n1 = root.getNamespace("w");
body = root.getChild("body", n1).getChildren("tbl", n1);
obj = body.map((e, i) => {
var tblGrid, tblPr, tblW, temp, w;
temp = {
tableIndex: i,
unit: "pt"
};
tblPr = e.getChild("tblPr", n1);
if (tblPr) {
tblW = tblPr.getChild("tblW", n1);
if (tblW) {
w = tblW.getAttribute("w", n1);
if (w) {
temp.tableWidth = Number(w.getValue()) / 20;
}
}
}
tblGrid = e.getChild("tblGrid", n1);
if (tblGrid) {
temp.tebleColumnWidth = tblGrid.getChildren("gridCol", n1).map((f) => {
return Number(f.getAttribute("w", n1).getValue()) / 20;
});
}
return temp;
});
return obj;
}
}
};
WordApp.name = "WordApp";
// --- end methods
parsDOCX = function() {
disassembleWord.call(this);
};
disassembleWord = function() {
var blobs;
blobs = Utilities.unzip(this.obj.excel.setContentType(MimeType.ZIP));
this.mainObj.fileObj = blobs.reduce((o, b) => {
return Object.assign(o, {
[b.getName()]: b
});
}, {});
};
getXmlObj = function(k_) {
return XmlService.parse(this.mainObj.fileObj[k_].getDataAsString());
};
putError = function(m) {
throw new Error(`${m}`);
};
putInternalError = function(m) {
throw new Error(`Internal error: ${m}`);
};
return WordApp;
}).call(this);
return r.WordApp = WordApp;
})(this);