@microsoft/connected-workbooks 1.0.0 → 2.1.24-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.
@@ -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,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 = 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 (0, mashupDocumentParser_1.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,5 @@
1
- export declare class QueryInfo {
2
- queryMashup: string;
3
- refreshOnOpen: boolean;
4
- constructor(queryMashup: string, refreshOnOpen: boolean);
5
- }
6
- export declare class WorkbookManager {
7
- private mashupHandler;
8
- generateSingleQueryWorkbook(query: QueryInfo, templateFile?: File): Promise<Blob>;
9
- private generateSingleQueryWorkbookFromZip;
10
- private setSingleQueryRefreshOnOpen;
11
- private setBase64;
12
- private getBase64;
13
- }
1
+ import { DocProps, QueryInfo, Grid, FileConfigs } from "./types";
2
+ export declare const generateSingleQueryWorkbook: (query: QueryInfo, initialDataGrid?: Grid, fileConfigs?: FileConfigs) => Promise<Blob>;
3
+ export declare const generateTableWorkbookFromHtml: (htmlTable: HTMLTableElement, docProps?: DocProps) => Promise<Blob>;
4
+ export declare const generateTableWorkbookFromGrid: (grid: Grid, docProps?: DocProps) => Promise<Blob>;
5
+ export declare const downloadWorkbook: (file: Blob, filename: string) => void;
@@ -14,145 +14,87 @@ 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;
17
+ exports.downloadWorkbook = exports.generateTableWorkbookFromGrid = exports.generateTableWorkbookFromHtml = exports.generateSingleQueryWorkbook = void 0;
18
18
  const jszip_1 = __importDefault(require("jszip"));
19
- const iconv_lite_1 = __importDefault(require("iconv-lite"));
20
- const mashupDocumentParser_1 = __importDefault(require("./mashupDocumentParser"));
21
- const workbookTemplate_1 = __importDefault(require("./workbookTemplate"));
22
- const pqCustomXmlPath = "customXml/item1.xml";
23
- const connectionsXmlPath = "xl/connections.xml";
24
- const queryTablesPath = "xl/queryTables/";
25
- const pivotCachesPath = "xl/pivotCache/";
26
- class QueryInfo {
27
- constructor(queryMashup, refreshOnOpen) {
28
- this.queryMashup = queryMashup;
29
- this.refreshOnOpen = refreshOnOpen;
19
+ const utils_1 = require("./utils");
20
+ const workbookTemplate_1 = require("./workbookTemplate");
21
+ const constants_1 = require("./utils/constants");
22
+ const generators_1 = require("./generators");
23
+ const htmlUtils_1 = require("./utils/htmlUtils");
24
+ const GridParser_1 = require("./GridParser");
25
+ const generateSingleQueryWorkbook = (query, initialDataGrid, fileConfigs) => __awaiter(void 0, void 0, void 0, function* () {
26
+ if (!query.queryMashup) {
27
+ throw new Error(constants_1.emptyQueryMashupErr);
30
28
  }
31
- }
32
- exports.QueryInfo = QueryInfo;
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
- generateSingleQueryWorkbook(query, templateFile) {
38
- return __awaiter(this, void 0, void 0, function* () {
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
+ const templateFile = fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateFile;
33
+ if (templateFile !== undefined && initialDataGrid !== undefined) {
34
+ throw new Error(constants_1.templateWithInitialDataErr);
44
35
  }
45
- generateSingleQueryWorkbookFromZip(zip, query) {
46
- return __awaiter(this, void 0, void 0, function* () {
47
- let old_base64 = yield this.getBase64(zip);
48
- let new_base64 = yield this.mashupHandler.ReplaceSingleQuery(old_base64, query.queryMashup);
49
- yield this.setBase64(zip, new_base64);
50
- if (query.refreshOnOpen) {
51
- yield this.setSingleQueryRefreshOnOpen(zip);
52
- }
53
- return yield zip.generateAsync({
54
- type: "blob",
55
- mimeType: "application/xlsx",
56
- });
57
- });
36
+ utils_1.pqUtils.validateQueryName(query.queryName);
37
+ const zip = templateFile === undefined
38
+ ? yield jszip_1.default.loadAsync(workbookTemplate_1.SIMPLE_QUERY_WORKBOOK_TEMPLATE, { base64: true })
39
+ : yield jszip_1.default.loadAsync(templateFile);
40
+ const tableData = yield parseInitialDataGrid(initialDataGrid);
41
+ return yield generateSingleQueryWorkbookFromZip(zip, query, fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.docProps, tableData);
42
+ });
43
+ exports.generateSingleQueryWorkbook = generateSingleQueryWorkbook;
44
+ const generateTableWorkbookFromHtml = (htmlTable, docProps) => __awaiter(void 0, void 0, void 0, function* () {
45
+ const gridData = (0, htmlUtils_1.extractTableValues)(htmlTable);
46
+ return yield (0, exports.generateTableWorkbookFromGrid)({ data: gridData, promoteHeaders: false }, docProps);
47
+ });
48
+ exports.generateTableWorkbookFromHtml = generateTableWorkbookFromHtml;
49
+ const generateTableWorkbookFromGrid = (grid, docProps) => __awaiter(void 0, void 0, void 0, function* () {
50
+ const zip = yield jszip_1.default.loadAsync(workbookTemplate_1.SIMPLE_BLANK_TABLE_TEMPLATE, { base64: true });
51
+ const tableData = yield parseInitialDataGrid(grid);
52
+ if (tableData === undefined) {
53
+ throw new Error(constants_1.tableNotFoundErr);
58
54
  }
59
- setSingleQueryRefreshOnOpen(zip) {
60
- var _a, _b, _c, _d, _e;
61
- return __awaiter(this, void 0, void 0, function* () {
62
- let connectionsXmlString = yield ((_a = zip.file(connectionsXmlPath)) === null || _a === void 0 ? void 0 : _a.async("text"));
63
- if (connectionsXmlString === undefined) {
64
- throw new Error("Connections were not found in template");
65
- }
66
- let parser = new DOMParser();
67
- let serializer = new XMLSerializer();
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
- });
55
+ yield utils_1.xmlPartsUtils.updateWorkbookInitialDataIfNeeded(zip, docProps, tableData);
56
+ return yield zip.generateAsync({
57
+ type: constants_1.blobFileType,
58
+ mimeType: constants_1.application,
59
+ });
60
+ });
61
+ exports.generateTableWorkbookFromGrid = generateTableWorkbookFromGrid;
62
+ const parseInitialDataGrid = (grid) => __awaiter(void 0, void 0, void 0, function* () {
63
+ if (!grid) {
64
+ return undefined;
131
65
  }
132
- setBase64(zip, base64) {
133
- return __awaiter(this, void 0, void 0, function* () {
134
- let newXml = `<?xml version="1.0" encoding="utf-16"?><DataMashup xmlns="http://schemas.microsoft.com/DataMashup">${base64}</DataMashup>`;
135
- let encoded = iconv_lite_1.default.encode(newXml, "UCS2", { addBOM: true });
136
- zip.file(pqCustomXmlPath, encoded);
137
- });
66
+ const tableData = (0, GridParser_1.parseToTableData)(grid);
67
+ return tableData;
68
+ });
69
+ const generateSingleQueryWorkbookFromZip = (zip, query, docProps, tableData) => __awaiter(void 0, void 0, void 0, function* () {
70
+ if (!query.queryName) {
71
+ query.queryName = constants_1.defaults.queryName;
138
72
  }
139
- getBase64(zip) {
140
- var _a;
141
- return __awaiter(this, void 0, void 0, function* () {
142
- let xmlValue = yield ((_a = zip.file(pqCustomXmlPath)) === null || _a === void 0 ? void 0 : _a.async("uint8array"));
143
- if (xmlValue === undefined) {
144
- throw new Error("PQ document wasn't found in zip");
145
- }
146
- let xmlString = iconv_lite_1.default.decode(xmlValue.buffer, "UTF-16");
147
- let parser = new DOMParser();
148
- let doc = parser.parseFromString(xmlString, "text/xml");
149
- let result = doc.childNodes[0].textContent;
150
- if (result === null) {
151
- throw Error("Base64 wasn't found in zip");
152
- }
153
- return result;
154
- });
73
+ yield utils_1.xmlPartsUtils.updateWorkbookPowerQueryDocument(zip, query.queryName, (0, generators_1.generateSingleQueryMashup)(query.queryName, query.queryMashup));
74
+ yield utils_1.xmlPartsUtils.updateWorkbookSingleQueryAttributes(zip, query.queryName, query.refreshOnOpen);
75
+ yield utils_1.xmlPartsUtils.updateWorkbookInitialDataIfNeeded(zip, docProps, tableData, true /*updateQueryTable*/);
76
+ return yield zip.generateAsync({
77
+ type: constants_1.blobFileType,
78
+ mimeType: constants_1.application,
79
+ });
80
+ });
81
+ const downloadWorkbook = (file, filename) => {
82
+ const nav = window.navigator;
83
+ if (nav.msSaveOrOpenBlob)
84
+ // IE10+
85
+ nav.msSaveOrOpenBlob(file, filename);
86
+ else {
87
+ // Others
88
+ const a = document.createElement("a");
89
+ const url = URL.createObjectURL(file);
90
+ a.href = url;
91
+ a.download = filename;
92
+ document.body.appendChild(a);
93
+ a.click();
94
+ setTimeout(function () {
95
+ document.body.removeChild(a);
96
+ window.URL.revokeObjectURL(url);
97
+ }, 0);
155
98
  }
156
- }
157
- exports.WorkbookManager = WorkbookManager;
158
- //# sourceMappingURL=workbookManager.js.map
99
+ };
100
+ exports.downloadWorkbook = downloadWorkbook;