@microsoft/connected-workbooks 1.0.0 → 2.1.16-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +87 -57
- package/dist/GridParser.d.ts +6 -0
- package/dist/GridParser.js +55 -0
- package/dist/TableDataParserFactory.d.ts +4 -0
- package/dist/TableDataParserFactory.js +15 -0
- package/dist/constants.d.ts +121 -0
- package/dist/constants.js +131 -0
- package/dist/generators.d.ts +3 -0
- package/dist/generators.js +14 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +7 -4
- package/dist/mashupDocumentParser.d.ts +7 -4
- package/dist/mashupDocumentParser.js +169 -72
- package/dist/types.d.ts +55 -0
- package/dist/types.js +29 -0
- package/dist/utils/arrayUtils.d.ts +16 -0
- package/dist/{arrayUtils.js → utils/arrayUtils.js} +12 -10
- package/dist/utils/constants.d.ts +121 -0
- package/dist/utils/constants.js +131 -0
- package/dist/utils/documentUtils.d.ts +15 -0
- package/dist/utils/documentUtils.js +101 -0
- package/dist/utils/htmlUtils.d.ts +1 -0
- package/dist/utils/htmlUtils.js +25 -0
- package/dist/utils/index.d.ts +6 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/mashupDocumentParser.d.ts +7 -0
- package/dist/utils/mashupDocumentParser.js +166 -0
- package/dist/utils/pqUtils.d.ts +15 -0
- package/dist/utils/pqUtils.js +101 -0
- package/dist/utils/tableUtils.d.ts +10 -0
- package/dist/utils/tableUtils.js +146 -0
- package/dist/utils/xmlInnerPartsUtils.d.ts +24 -0
- package/dist/utils/xmlInnerPartsUtils.js +198 -0
- package/dist/utils/xmlPartsUtils.d.ts +8 -0
- package/dist/utils/xmlPartsUtils.js +62 -0
- package/dist/workbookManager.d.ts +8 -13
- package/dist/workbookManager.js +78 -133
- package/dist/workbookTemplate.d.ts +2 -1
- package/dist/workbookTemplate.js +2 -2
- package/package.json +21 -8
- package/dist/arrayUtils.d.ts +0 -10
- package/dist/arrayUtils.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/mashupDocumentParser.js.map +0 -1
- package/dist/workbookManager.js.map +0 -1
- package/dist/workbookTemplate.js.map +0 -1
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const types_1 = require("../types");
|
|
16
|
+
const constants_1 = require("./constants");
|
|
17
|
+
const documentUtils_1 = __importDefault(require("./documentUtils"));
|
|
18
|
+
const updateDocProps = (zip, docProps = {}) => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
+
const { doc, properties } = yield documentUtils_1.default.getDocPropsProperties(zip);
|
|
20
|
+
//set auto updated elements
|
|
21
|
+
const docPropsAutoUpdatedElementsArr = Object.keys(types_1.DocPropsAutoUpdatedElements);
|
|
22
|
+
const nowTime = new Date().toISOString();
|
|
23
|
+
docPropsAutoUpdatedElementsArr.forEach((tag) => {
|
|
24
|
+
documentUtils_1.default.createOrUpdateProperty(doc, properties, types_1.DocPropsAutoUpdatedElements[tag], nowTime);
|
|
25
|
+
});
|
|
26
|
+
//set modifiable elements
|
|
27
|
+
const docPropsModifiableElementsArr = Object.keys(types_1.DocPropsModifiableElements);
|
|
28
|
+
docPropsModifiableElementsArr
|
|
29
|
+
.map((key) => ({
|
|
30
|
+
name: types_1.DocPropsModifiableElements[key],
|
|
31
|
+
value: docProps[key],
|
|
32
|
+
}))
|
|
33
|
+
.forEach((kvp) => {
|
|
34
|
+
documentUtils_1.default.createOrUpdateProperty(doc, properties, kvp.name, kvp.value);
|
|
35
|
+
});
|
|
36
|
+
const serializer = new XMLSerializer();
|
|
37
|
+
const newDoc = serializer.serializeToString(doc);
|
|
38
|
+
zip.file(constants_1.docPropsCoreXmlPath, newDoc);
|
|
39
|
+
});
|
|
40
|
+
const updateConnections = (connectionsXmlString, queryName, refreshOnOpen) => __awaiter(void 0, void 0, void 0, function* () {
|
|
41
|
+
var _a, _b, _c;
|
|
42
|
+
const parser = new DOMParser();
|
|
43
|
+
const serializer = new XMLSerializer();
|
|
44
|
+
const refreshOnLoadValue = refreshOnOpen ? constants_1.trueValue : constants_1.falseValue;
|
|
45
|
+
const connectionsDoc = parser.parseFromString(connectionsXmlString, constants_1.xmlTextResultType);
|
|
46
|
+
const connectionsProperties = connectionsDoc.getElementsByTagName(constants_1.element.databaseProperties);
|
|
47
|
+
const dbPr = connectionsProperties[0];
|
|
48
|
+
dbPr.setAttribute(constants_1.elementAttributes.refreshOnLoad, refreshOnLoadValue);
|
|
49
|
+
// Update query details to match queryName
|
|
50
|
+
(_a = dbPr.parentElement) === null || _a === void 0 ? void 0 : _a.setAttribute(constants_1.elementAttributes.name, constants_1.elementAttributesValues.connectionName(queryName));
|
|
51
|
+
(_b = dbPr.parentElement) === null || _b === void 0 ? void 0 : _b.setAttribute(constants_1.elementAttributes.description, constants_1.elementAttributesValues.connectionDescription(queryName));
|
|
52
|
+
dbPr.setAttribute(constants_1.elementAttributes.connection, constants_1.elementAttributesValues.connection(queryName));
|
|
53
|
+
dbPr.setAttribute(constants_1.elementAttributes.command, constants_1.elementAttributesValues.connectionCommand(queryName));
|
|
54
|
+
const connectionId = (_c = dbPr.parentElement) === null || _c === void 0 ? void 0 : _c.getAttribute(constants_1.elementAttributes.id);
|
|
55
|
+
const connectionXmlFileString = serializer.serializeToString(connectionsDoc);
|
|
56
|
+
if (connectionId === null) {
|
|
57
|
+
throw new Error(constants_1.connectionsNotFoundErr);
|
|
58
|
+
}
|
|
59
|
+
return { connectionId, connectionXmlFileString };
|
|
60
|
+
});
|
|
61
|
+
const updateSharedStrings = (sharedStringsXmlString, queryName) => __awaiter(void 0, void 0, void 0, function* () {
|
|
62
|
+
const parser = new DOMParser();
|
|
63
|
+
const serializer = new XMLSerializer();
|
|
64
|
+
const sharedStringsDoc = parser.parseFromString(sharedStringsXmlString, constants_1.xmlTextResultType);
|
|
65
|
+
const sharedStringsTable = sharedStringsDoc.getElementsByTagName(constants_1.element.sharedStringTable)[0];
|
|
66
|
+
if (!sharedStringsTable) {
|
|
67
|
+
throw new Error(constants_1.sharedStringsNotFoundErr);
|
|
68
|
+
}
|
|
69
|
+
const textElementCollection = sharedStringsDoc.getElementsByTagName(constants_1.element.text);
|
|
70
|
+
let textElement = null;
|
|
71
|
+
let sharedStringIndex = textElementCollection.length;
|
|
72
|
+
if (textElementCollection && textElementCollection.length) {
|
|
73
|
+
for (let i = 0; i < textElementCollection.length; i++) {
|
|
74
|
+
if (textElementCollection[i].innerHTML === queryName) {
|
|
75
|
+
textElement = textElementCollection[i];
|
|
76
|
+
sharedStringIndex = i + 1;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (textElement === null) {
|
|
82
|
+
if (sharedStringsDoc.documentElement.namespaceURI) {
|
|
83
|
+
textElement = sharedStringsDoc.createElementNS(sharedStringsDoc.documentElement.namespaceURI, constants_1.element.text);
|
|
84
|
+
textElement.textContent = queryName;
|
|
85
|
+
const siElement = sharedStringsDoc.createElementNS(sharedStringsDoc.documentElement.namespaceURI, constants_1.element.sharedStringItem);
|
|
86
|
+
siElement.appendChild(textElement);
|
|
87
|
+
sharedStringsDoc.getElementsByTagName(constants_1.element.sharedStringTable)[0].appendChild(siElement);
|
|
88
|
+
}
|
|
89
|
+
const value = sharedStringsTable.getAttribute(constants_1.elementAttributes.count);
|
|
90
|
+
if (value) {
|
|
91
|
+
sharedStringsTable.setAttribute(constants_1.elementAttributes.count, (parseInt(value) + 1).toString());
|
|
92
|
+
}
|
|
93
|
+
const uniqueValue = sharedStringsTable.getAttribute(constants_1.elementAttributes.uniqueCount);
|
|
94
|
+
if (uniqueValue) {
|
|
95
|
+
sharedStringsTable.setAttribute(constants_1.elementAttributes.uniqueCount, (parseInt(uniqueValue) + 1).toString());
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const newSharedStrings = serializer.serializeToString(sharedStringsDoc);
|
|
99
|
+
return { sharedStringIndex, newSharedStrings };
|
|
100
|
+
});
|
|
101
|
+
const updateWorksheet = (sheetsXmlString, sharedStringIndex) => __awaiter(void 0, void 0, void 0, function* () {
|
|
102
|
+
const parser = new DOMParser();
|
|
103
|
+
const serializer = new XMLSerializer();
|
|
104
|
+
const sheetsDoc = parser.parseFromString(sheetsXmlString, constants_1.xmlTextResultType);
|
|
105
|
+
sheetsDoc.getElementsByTagName(constants_1.element.cellValue)[0].innerHTML = sharedStringIndex.toString();
|
|
106
|
+
const newSheet = serializer.serializeToString(sheetsDoc);
|
|
107
|
+
return newSheet;
|
|
108
|
+
});
|
|
109
|
+
const updatePivotTablesandQueryTables = (zip, queryName, refreshOnOpen, connectionId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
110
|
+
var _d, _e;
|
|
111
|
+
// Find Query Table
|
|
112
|
+
let found = false;
|
|
113
|
+
const queryTablePromises = [];
|
|
114
|
+
(_d = zip.folder(constants_1.queryTablesPath)) === null || _d === void 0 ? void 0 : _d.forEach((relativePath, queryTableFile) => __awaiter(void 0, void 0, void 0, function* () {
|
|
115
|
+
queryTablePromises.push((() => {
|
|
116
|
+
return queryTableFile.async(constants_1.textResultType).then((queryTableString) => {
|
|
117
|
+
return {
|
|
118
|
+
path: relativePath,
|
|
119
|
+
queryTableXmlString: queryTableString,
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
})());
|
|
123
|
+
}));
|
|
124
|
+
(yield Promise.all(queryTablePromises)).forEach(({ path, queryTableXmlString }) => {
|
|
125
|
+
const { isQueryTableUpdated, newQueryTable } = updateQueryTable(queryTableXmlString, connectionId, refreshOnOpen);
|
|
126
|
+
zip.file(constants_1.queryTablesPath + path, newQueryTable);
|
|
127
|
+
if (isQueryTableUpdated) {
|
|
128
|
+
found = true;
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
if (found) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
// Find Pivot Table
|
|
135
|
+
const pivotCachePromises = [];
|
|
136
|
+
(_e = zip.folder(constants_1.pivotCachesPath)) === null || _e === void 0 ? void 0 : _e.forEach((relativePath, pivotCacheFile) => __awaiter(void 0, void 0, void 0, function* () {
|
|
137
|
+
if (relativePath.startsWith(constants_1.pivotCachesPathPrefix)) {
|
|
138
|
+
pivotCachePromises.push((() => {
|
|
139
|
+
return pivotCacheFile.async(constants_1.textResultType).then((pivotCacheString) => {
|
|
140
|
+
return {
|
|
141
|
+
path: relativePath,
|
|
142
|
+
pivotCacheXmlString: pivotCacheString,
|
|
143
|
+
};
|
|
144
|
+
});
|
|
145
|
+
})());
|
|
146
|
+
}
|
|
147
|
+
}));
|
|
148
|
+
(yield Promise.all(pivotCachePromises)).forEach(({ path, pivotCacheXmlString }) => {
|
|
149
|
+
const { isPivotTableUpdated, newPivotTable } = updatePivotTable(pivotCacheXmlString, connectionId, refreshOnOpen);
|
|
150
|
+
zip.file(constants_1.pivotCachesPath + path, newPivotTable);
|
|
151
|
+
if (isPivotTableUpdated) {
|
|
152
|
+
found = true;
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
if (!found) {
|
|
156
|
+
throw new Error(constants_1.queryAndPivotTableNotFoundErr);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
const updateQueryTable = (tableXmlString, connectionId, refreshOnOpen) => {
|
|
160
|
+
const refreshOnLoadValue = refreshOnOpen ? constants_1.trueValue : constants_1.falseValue;
|
|
161
|
+
let isQueryTableUpdated = false;
|
|
162
|
+
const parser = new DOMParser();
|
|
163
|
+
const serializer = new XMLSerializer();
|
|
164
|
+
const queryTableDoc = parser.parseFromString(tableXmlString, constants_1.xmlTextResultType);
|
|
165
|
+
const queryTable = queryTableDoc.getElementsByTagName(constants_1.element.queryTable)[0];
|
|
166
|
+
let newQueryTable = constants_1.emptyValue;
|
|
167
|
+
if (queryTable.getAttribute(constants_1.elementAttributes.connectionId) == connectionId) {
|
|
168
|
+
queryTable.setAttribute(constants_1.elementAttributes.refreshOnLoad, refreshOnLoadValue);
|
|
169
|
+
newQueryTable = serializer.serializeToString(queryTableDoc);
|
|
170
|
+
isQueryTableUpdated = true;
|
|
171
|
+
}
|
|
172
|
+
return { isQueryTableUpdated, newQueryTable };
|
|
173
|
+
};
|
|
174
|
+
const updatePivotTable = (tableXmlString, connectionId, refreshOnOpen) => {
|
|
175
|
+
const refreshOnLoadValue = refreshOnOpen ? constants_1.trueValue : constants_1.falseValue;
|
|
176
|
+
let isPivotTableUpdated = false;
|
|
177
|
+
const parser = new DOMParser();
|
|
178
|
+
const serializer = new XMLSerializer();
|
|
179
|
+
const pivotCacheDoc = parser.parseFromString(tableXmlString, constants_1.xmlTextResultType);
|
|
180
|
+
let cacheSource = pivotCacheDoc.getElementsByTagName(constants_1.element.cacheSource)[0];
|
|
181
|
+
let newPivotTable = constants_1.emptyValue;
|
|
182
|
+
if (cacheSource.getAttribute(constants_1.elementAttributes.connectionId) == connectionId) {
|
|
183
|
+
cacheSource = cacheSource.parentElement;
|
|
184
|
+
cacheSource.setAttribute(constants_1.elementAttributes.refreshOnLoad, refreshOnLoadValue);
|
|
185
|
+
newPivotTable = serializer.serializeToString(pivotCacheDoc);
|
|
186
|
+
isPivotTableUpdated = true;
|
|
187
|
+
}
|
|
188
|
+
return { isPivotTableUpdated, newPivotTable };
|
|
189
|
+
};
|
|
190
|
+
exports.default = {
|
|
191
|
+
updateDocProps,
|
|
192
|
+
updateConnections,
|
|
193
|
+
updateSharedStrings,
|
|
194
|
+
updateWorksheet,
|
|
195
|
+
updatePivotTablesandQueryTables,
|
|
196
|
+
updateQueryTable,
|
|
197
|
+
updatePivotTable,
|
|
198
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import JSZip from "jszip";
|
|
2
|
+
import { DocProps, TableData } from "../types";
|
|
3
|
+
declare const _default: {
|
|
4
|
+
updateWorkbookInitialDataIfNeeded: (zip: JSZip, docProps?: DocProps | undefined, tableData?: TableData | undefined, updateQueryTable?: boolean) => Promise<void>;
|
|
5
|
+
updateWorkbookPowerQueryDocument: (zip: JSZip, queryName: string, queryMashupDoc: string) => Promise<void>;
|
|
6
|
+
updateWorkbookSingleQueryAttributes: (zip: JSZip, queryName: string, refreshOnOpen: boolean) => Promise<void>;
|
|
7
|
+
};
|
|
8
|
+
export default _default;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const constants_1 = require("./constants");
|
|
16
|
+
const mashupDocumentParser_1 = __importDefault(require("./mashupDocumentParser"));
|
|
17
|
+
const pqUtils_1 = __importDefault(require("./pqUtils"));
|
|
18
|
+
const xmlInnerPartsUtils_1 = __importDefault(require("./xmlInnerPartsUtils"));
|
|
19
|
+
const tableUtils_1 = __importDefault(require("./tableUtils"));
|
|
20
|
+
const updateWorkbookInitialDataIfNeeded = (zip, docProps, tableData, updateQueryTable = false) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
+
yield xmlInnerPartsUtils_1.default.updateDocProps(zip, docProps);
|
|
22
|
+
yield tableUtils_1.default.updateTableInitialDataIfNeeded(zip, tableData, updateQueryTable);
|
|
23
|
+
});
|
|
24
|
+
const updateWorkbookPowerQueryDocument = (zip, queryName, queryMashupDoc) => __awaiter(void 0, void 0, void 0, function* () {
|
|
25
|
+
const old_base64 = yield pqUtils_1.default.getBase64(zip);
|
|
26
|
+
if (!old_base64) {
|
|
27
|
+
throw new Error(constants_1.base64NotFoundErr);
|
|
28
|
+
}
|
|
29
|
+
const new_base64 = yield new mashupDocumentParser_1.default().ReplaceSingleQuery(old_base64, queryName, queryMashupDoc);
|
|
30
|
+
yield pqUtils_1.default.setBase64(zip, new_base64);
|
|
31
|
+
});
|
|
32
|
+
const updateWorkbookSingleQueryAttributes = (zip, queryName, refreshOnOpen) => __awaiter(void 0, void 0, void 0, function* () {
|
|
33
|
+
var _a, _b, _c;
|
|
34
|
+
// Update connections
|
|
35
|
+
const connectionsXmlString = yield ((_a = zip.file(constants_1.connectionsXmlPath)) === null || _a === void 0 ? void 0 : _a.async(constants_1.textResultType));
|
|
36
|
+
if (connectionsXmlString === undefined) {
|
|
37
|
+
throw new Error(constants_1.connectionsNotFoundErr);
|
|
38
|
+
}
|
|
39
|
+
const { connectionId, connectionXmlFileString } = yield xmlInnerPartsUtils_1.default.updateConnections(connectionsXmlString, queryName, refreshOnOpen);
|
|
40
|
+
zip.file(constants_1.connectionsXmlPath, connectionXmlFileString);
|
|
41
|
+
// Update sharedStrings
|
|
42
|
+
const sharedStringsXmlString = yield ((_b = zip.file(constants_1.sharedStringsXmlPath)) === null || _b === void 0 ? void 0 : _b.async(constants_1.textResultType));
|
|
43
|
+
if (sharedStringsXmlString === undefined) {
|
|
44
|
+
throw new Error(constants_1.sharedStringsNotFoundErr);
|
|
45
|
+
}
|
|
46
|
+
const { sharedStringIndex, newSharedStrings } = yield xmlInnerPartsUtils_1.default.updateSharedStrings(sharedStringsXmlString, queryName);
|
|
47
|
+
zip.file(constants_1.sharedStringsXmlPath, newSharedStrings);
|
|
48
|
+
// Update sheet
|
|
49
|
+
const sheetsXmlString = yield ((_c = zip.file(constants_1.sheetsXmlPath)) === null || _c === void 0 ? void 0 : _c.async(constants_1.textResultType));
|
|
50
|
+
if (sheetsXmlString === undefined) {
|
|
51
|
+
throw new Error(constants_1.sheetsNotFoundErr);
|
|
52
|
+
}
|
|
53
|
+
const worksheetString = yield xmlInnerPartsUtils_1.default.updateWorksheet(sheetsXmlString, sharedStringIndex.toString());
|
|
54
|
+
zip.file(constants_1.sheetsXmlPath, worksheetString);
|
|
55
|
+
// Update tables
|
|
56
|
+
yield xmlInnerPartsUtils_1.default.updatePivotTablesandQueryTables(zip, queryName, refreshOnOpen, connectionId);
|
|
57
|
+
});
|
|
58
|
+
exports.default = {
|
|
59
|
+
updateWorkbookInitialDataIfNeeded,
|
|
60
|
+
updateWorkbookPowerQueryDocument,
|
|
61
|
+
updateWorkbookSingleQueryAttributes,
|
|
62
|
+
};
|
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
private generateSingleQueryWorkbookFromZip;
|
|
10
|
-
private setSingleQueryRefreshOnOpen;
|
|
11
|
-
private setBase64;
|
|
12
|
-
private getBase64;
|
|
13
|
-
}
|
|
1
|
+
import { DocProps, QueryInfo, Grid } from "./types";
|
|
2
|
+
declare const _default: {
|
|
3
|
+
generateSingleQueryWorkbook: (query: QueryInfo, initialDataGrid?: Grid | undefined, templateFile?: File | undefined, docProps?: DocProps | undefined) => Promise<Blob>;
|
|
4
|
+
generateTableWorkbookFromHtml: (htmlTable: HTMLTableElement, docProps?: DocProps | undefined) => Promise<Blob>;
|
|
5
|
+
generateTableWorkbookFromGrid: (initialDataGrid: Grid, docProps?: DocProps | undefined) => Promise<Blob>;
|
|
6
|
+
downloadWorkbook: (file: Blob, filename: string) => void;
|
|
7
|
+
};
|
|
8
|
+
export default _default;
|
package/dist/workbookManager.js
CHANGED
|
@@ -14,145 +14,90 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.WorkbookManager = exports.QueryInfo = void 0;
|
|
18
17
|
const jszip_1 = __importDefault(require("jszip"));
|
|
19
|
-
const
|
|
20
|
-
const mashupDocumentParser_1 = __importDefault(require("./mashupDocumentParser"));
|
|
18
|
+
const utils_1 = require("./utils");
|
|
21
19
|
const workbookTemplate_1 = __importDefault(require("./workbookTemplate"));
|
|
22
|
-
const
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
20
|
+
const constants_1 = require("./utils/constants");
|
|
21
|
+
const types_1 = require("./types");
|
|
22
|
+
const TableDataParserFactory_1 = __importDefault(require("./TableDataParserFactory"));
|
|
23
|
+
const generators_1 = require("./generators");
|
|
24
|
+
const htmlUtils_1 = require("./utils/htmlUtils");
|
|
25
|
+
const generateSingleQueryWorkbook = (query, initialDataGrid, templateFile, docProps) => __awaiter(void 0, void 0, void 0, function* () {
|
|
26
|
+
if (!query.queryMashup) {
|
|
27
|
+
throw new Error(constants_1.emptyQueryMashupErr);
|
|
30
28
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
class WorkbookManager {
|
|
34
|
-
constructor() {
|
|
35
|
-
this.mashupHandler = new mashupDocumentParser_1.default();
|
|
29
|
+
if (!query.queryName) {
|
|
30
|
+
query.queryName = constants_1.defaults.queryName;
|
|
36
31
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
let zip = templateFile === undefined
|
|
40
|
-
? yield jszip_1.default.loadAsync(workbookTemplate_1.default.SIMPLE_QUERY_WORKBOOK_TEMPLATE, { base64: true })
|
|
41
|
-
: yield jszip_1.default.loadAsync(templateFile);
|
|
42
|
-
return yield this.generateSingleQueryWorkbookFromZip(zip, query);
|
|
43
|
-
});
|
|
32
|
+
if (templateFile !== undefined && initialDataGrid !== undefined) {
|
|
33
|
+
throw new Error(constants_1.templateWithInitialDataErr);
|
|
44
34
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
35
|
+
utils_1.pqUtils.validateQueryName(query.queryName);
|
|
36
|
+
const zip = templateFile === undefined
|
|
37
|
+
? yield jszip_1.default.loadAsync(workbookTemplate_1.default.SIMPLE_QUERY_WORKBOOK_TEMPLATE, { base64: true })
|
|
38
|
+
: yield jszip_1.default.loadAsync(templateFile);
|
|
39
|
+
const tableData = yield parseInitialDataGrid(initialDataGrid);
|
|
40
|
+
return yield generateSingleQueryWorkbookFromZip(zip, query, docProps, tableData);
|
|
41
|
+
});
|
|
42
|
+
const generateTableWorkbookFromHtml = (htmlTable, docProps) => __awaiter(void 0, void 0, void 0, function* () {
|
|
43
|
+
const [headers, gridData] = htmlUtils_1.extractTableValues(htmlTable);
|
|
44
|
+
const header = headers.map((column) => ({ name: column, type: types_1.DataTypes.autodetect }));
|
|
45
|
+
return yield generateTableWorkbookFromGrid({ gridData, header }, docProps);
|
|
46
|
+
});
|
|
47
|
+
const generateTableWorkbookFromGrid = (initialDataGrid, docProps) => __awaiter(void 0, void 0, void 0, function* () {
|
|
48
|
+
const zip = yield jszip_1.default.loadAsync(workbookTemplate_1.default.SIMPLE_BLANK_TABLE_TEMPLATE, { base64: true });
|
|
49
|
+
const tableData = yield parseInitialDataGrid(initialDataGrid);
|
|
50
|
+
if (tableData === undefined) {
|
|
51
|
+
throw new Error(constants_1.tableNotFoundErr);
|
|
58
52
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
let connectionsDoc = parser.parseFromString(connectionsXmlString, "text/xml");
|
|
69
|
-
let connectionId = "-1";
|
|
70
|
-
let connectionsProperties = connectionsDoc.getElementsByTagName("dbPr");
|
|
71
|
-
for (let properties of connectionsProperties) {
|
|
72
|
-
if (properties.getAttribute("command") == "SELECT * FROM [Query1]") {
|
|
73
|
-
(_b = properties.parentElement) === null || _b === void 0 ? void 0 : _b.setAttribute("refreshOnLoad", "1");
|
|
74
|
-
connectionId = (_c = properties.parentElement) === null || _c === void 0 ? void 0 : _c.getAttribute("id");
|
|
75
|
-
let newConn = serializer.serializeToString(connectionsDoc);
|
|
76
|
-
zip.file(connectionsXmlPath, newConn);
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (connectionId == "-1") {
|
|
81
|
-
throw new Error("No connection found for Query1");
|
|
82
|
-
}
|
|
83
|
-
let found = false;
|
|
84
|
-
// Find Query Table
|
|
85
|
-
let queryTablePromises = [];
|
|
86
|
-
(_d = zip.folder(queryTablesPath)) === null || _d === void 0 ? void 0 : _d.forEach((relativePath, queryTableFile) => __awaiter(this, void 0, void 0, function* () {
|
|
87
|
-
queryTablePromises.push((() => {
|
|
88
|
-
return queryTableFile.async("text").then(queryTableString => {
|
|
89
|
-
return { path: relativePath, queryTableXmlString: queryTableString };
|
|
90
|
-
});
|
|
91
|
-
})());
|
|
92
|
-
}));
|
|
93
|
-
(yield Promise.all(queryTablePromises)).forEach(({ path, queryTableXmlString }) => {
|
|
94
|
-
let queryTableDoc = parser.parseFromString(queryTableXmlString, "text/xml");
|
|
95
|
-
let element = queryTableDoc.getElementsByTagName("queryTable")[0];
|
|
96
|
-
if (element.getAttribute("connectionId") == connectionId) {
|
|
97
|
-
element.setAttribute("refreshOnLoad", "1");
|
|
98
|
-
let newQT = serializer.serializeToString(queryTableDoc);
|
|
99
|
-
zip.file(queryTablesPath + path, newQT);
|
|
100
|
-
found = true;
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
if (found) {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
// Find Query Table
|
|
107
|
-
let pivotCachePromises = [];
|
|
108
|
-
(_e = zip.folder(pivotCachesPath)) === null || _e === void 0 ? void 0 : _e.forEach((relativePath, pivotCacheFile) => __awaiter(this, void 0, void 0, function* () {
|
|
109
|
-
if (relativePath.startsWith("pivotCacheDefinition")) {
|
|
110
|
-
pivotCachePromises.push((() => {
|
|
111
|
-
return pivotCacheFile.async("text").then(pivotCacheString => {
|
|
112
|
-
return { path: relativePath, pivotCacheXmlString: pivotCacheString };
|
|
113
|
-
});
|
|
114
|
-
})());
|
|
115
|
-
}
|
|
116
|
-
}));
|
|
117
|
-
(yield Promise.all(pivotCachePromises)).forEach(({ path, pivotCacheXmlString }) => {
|
|
118
|
-
let pivotCacheDoc = parser.parseFromString(pivotCacheXmlString, "text/xml");
|
|
119
|
-
let element = pivotCacheDoc.getElementsByTagName("cacheSource")[0];
|
|
120
|
-
if (element.getAttribute("connectionId") == connectionId) {
|
|
121
|
-
element.parentElement.setAttribute("refreshOnLoad", "1");
|
|
122
|
-
let newPC = serializer.serializeToString(pivotCacheDoc);
|
|
123
|
-
zip.file(pivotCachesPath + path, newPC);
|
|
124
|
-
found = true;
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
if (!found) {
|
|
128
|
-
throw new Error("No Query Table or Pivot Table found for Query1 in given template.");
|
|
129
|
-
}
|
|
130
|
-
});
|
|
53
|
+
yield utils_1.xmlPartsUtils.updateWorkbookInitialDataIfNeeded(zip, docProps, tableData);
|
|
54
|
+
return yield zip.generateAsync({
|
|
55
|
+
type: constants_1.blobFileType,
|
|
56
|
+
mimeType: constants_1.application,
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
const parseInitialDataGrid = (initialDataGrid) => __awaiter(void 0, void 0, void 0, function* () {
|
|
60
|
+
if (!initialDataGrid) {
|
|
61
|
+
return undefined;
|
|
131
62
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
63
|
+
const parser = TableDataParserFactory_1.default.createParser(initialDataGrid);
|
|
64
|
+
const tableData = parser.parseToTableData(initialDataGrid);
|
|
65
|
+
return tableData;
|
|
66
|
+
});
|
|
67
|
+
const generateSingleQueryWorkbookFromZip = (zip, query, docProps, tableData) => __awaiter(void 0, void 0, void 0, function* () {
|
|
68
|
+
if (!query.queryName) {
|
|
69
|
+
query.queryName = constants_1.defaults.queryName;
|
|
138
70
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
71
|
+
yield utils_1.xmlPartsUtils.updateWorkbookPowerQueryDocument(zip, query.queryName, generators_1.generateSingleQueryMashup(query.queryName, query.queryMashup));
|
|
72
|
+
yield utils_1.xmlPartsUtils.updateWorkbookSingleQueryAttributes(zip, query.queryName, query.refreshOnOpen);
|
|
73
|
+
yield utils_1.xmlPartsUtils.updateWorkbookInitialDataIfNeeded(zip, docProps, tableData, true /*updateQueryTable*/);
|
|
74
|
+
return yield zip.generateAsync({
|
|
75
|
+
type: constants_1.blobFileType,
|
|
76
|
+
mimeType: constants_1.application,
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
const downloadWorkbook = (file, filename) => {
|
|
80
|
+
const nav = window.navigator;
|
|
81
|
+
if (nav.msSaveOrOpenBlob)
|
|
82
|
+
// IE10+
|
|
83
|
+
nav.msSaveOrOpenBlob(file, filename);
|
|
84
|
+
else {
|
|
85
|
+
// Others
|
|
86
|
+
const a = document.createElement("a");
|
|
87
|
+
const url = URL.createObjectURL(file);
|
|
88
|
+
a.href = url;
|
|
89
|
+
a.download = filename;
|
|
90
|
+
document.body.appendChild(a);
|
|
91
|
+
a.click();
|
|
92
|
+
setTimeout(function () {
|
|
93
|
+
document.body.removeChild(a);
|
|
94
|
+
window.URL.revokeObjectURL(url);
|
|
95
|
+
}, 0);
|
|
155
96
|
}
|
|
156
|
-
}
|
|
157
|
-
exports.
|
|
158
|
-
|
|
97
|
+
};
|
|
98
|
+
exports.default = {
|
|
99
|
+
generateSingleQueryWorkbook,
|
|
100
|
+
generateTableWorkbookFromHtml,
|
|
101
|
+
generateTableWorkbookFromGrid,
|
|
102
|
+
downloadWorkbook,
|
|
103
|
+
};
|