@microsoft/connected-workbooks 3.3.2-beta → 3.4.0

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.
Files changed (37) hide show
  1. package/dist/utils/constants.js +87 -49
  2. package/dist/utils/documentUtils.js +1 -1
  3. package/dist/utils/gridUtils.js +8 -8
  4. package/dist/utils/mashupDocumentParser.js +1 -1
  5. package/dist/utils/pqUtils.js +3 -3
  6. package/dist/utils/tableUtils.js +11 -4
  7. package/dist/utils/xmlInnerPartsUtils.js +245 -13
  8. package/dist/utils/xmlPartsUtils.js +74 -4
  9. package/dist/workbookManager.js +3 -3
  10. package/package.json +7 -3
  11. package/dist/src/generators.js +0 -15
  12. package/dist/src/types.js +0 -28
  13. package/dist/src/utils/arrayUtils.js +0 -51
  14. package/dist/src/utils/constants.js +0 -175
  15. package/dist/src/utils/documentUtils.js +0 -167
  16. package/dist/src/utils/gridUtils.js +0 -103
  17. package/dist/src/utils/htmlUtils.js +0 -19
  18. package/dist/src/utils/index.js +0 -24
  19. package/dist/src/utils/mashupDocumentParser.js +0 -188
  20. package/dist/src/utils/pqUtils.js +0 -194
  21. package/dist/src/utils/tableUtils.js +0 -233
  22. package/dist/src/utils/xmlInnerPartsUtils.js +0 -431
  23. package/dist/src/utils/xmlPartsUtils.js +0 -178
  24. package/dist/src/workbookTemplate.js +0 -9
  25. package/dist/tests/arrayUtils.test.js +0 -66
  26. package/dist/tests/documentUtils.test.js +0 -70
  27. package/dist/tests/gridUtils.test.js +0 -214
  28. package/dist/tests/htmlUtils.test.js +0 -111
  29. package/dist/tests/mashupDocumentParser.test.js +0 -113
  30. package/dist/tests/mocks/PqMock.js +0 -7
  31. package/dist/tests/mocks/index.js +0 -24
  32. package/dist/tests/mocks/section1mSimpleQueryMock.js +0 -8
  33. package/dist/tests/mocks/xmlMocks.js +0 -14
  34. package/dist/tests/tableUtils.test.js +0 -70
  35. package/dist/tests/workbookQueryTemplate.test.js +0 -218
  36. package/dist/tests/workbookTableTemplate.test.js +0 -126
  37. package/dist/tests/xmlInnerPartsUtils.test.js +0 -273
@@ -1,188 +0,0 @@
1
- "use strict";
2
- // Copyright (c) Microsoft Corporation.
3
- // Licensed under the MIT license.
4
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
- if (k2 === undefined) k2 = k;
6
- var desc = Object.getOwnPropertyDescriptor(m, k);
7
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
- desc = { enumerable: true, get: function() { return m[k]; } };
9
- }
10
- Object.defineProperty(o, k2, desc);
11
- }) : (function(o, m, k, k2) {
12
- if (k2 === undefined) k2 = k;
13
- o[k2] = m[k];
14
- }));
15
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16
- Object.defineProperty(o, "default", { enumerable: true, value: v });
17
- }) : function(o, v) {
18
- o["default"] = v;
19
- });
20
- var __importStar = (this && this.__importStar) || function (mod) {
21
- if (mod && mod.__esModule) return mod;
22
- var result = {};
23
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24
- __setModuleDefault(result, mod);
25
- return result;
26
- };
27
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
28
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
29
- return new (P || (P = Promise))(function (resolve, reject) {
30
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
31
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
32
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
33
- step((generator = generator.apply(thisArg, _arguments || [])).next());
34
- });
35
- };
36
- var __generator = (this && this.__generator) || function (thisArg, body) {
37
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
38
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
39
- function verb(n) { return function (v) { return step([n, v]); }; }
40
- function step(op) {
41
- if (f) throw new TypeError("Generator is already executing.");
42
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
43
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
44
- if (y = 0, t) op = [op[0] & 2, t.value];
45
- switch (op[0]) {
46
- case 0: case 1: t = op; break;
47
- case 4: _.label++; return { value: op[1], done: false };
48
- case 5: _.label++; y = op[1]; op = [0]; continue;
49
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
50
- default:
51
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
52
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
53
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
54
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
55
- if (t[2]) _.ops.pop();
56
- _.trys.pop(); continue;
57
- }
58
- op = body.call(thisArg, _);
59
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
60
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
61
- }
62
- };
63
- var __importDefault = (this && this.__importDefault) || function (mod) {
64
- return (mod && mod.__esModule) ? mod : { "default": mod };
65
- };
66
- Object.defineProperty(exports, "__esModule", { value: true });
67
- exports.editSingleQueryMetadata = exports.getPackageComponents = exports.replaceSingleQuery = void 0;
68
- var base64 = __importStar(require("base64-js"));
69
- var jszip_1 = __importDefault(require("jszip"));
70
- var constants_1 = require("./constants");
71
- var _1 = require(".");
72
- var xmldom_qsa_1 = require("xmldom-qsa");
73
- var replaceSingleQuery = function (base64Str, queryName, queryMashupDoc) { return __awaiter(void 0, void 0, void 0, function () {
74
- var _a, version, packageOPC, permissionsSize, permissions, metadata, endBuffer, newPackageBuffer, packageSizeBuffer, permissionsSizeBuffer, newMetadataBuffer, metadataSizeBuffer, newMashup;
75
- return __generator(this, function (_b) {
76
- switch (_b.label) {
77
- case 0:
78
- _a = (0, exports.getPackageComponents)(base64Str), version = _a.version, packageOPC = _a.packageOPC, permissionsSize = _a.permissionsSize, permissions = _a.permissions, metadata = _a.metadata, endBuffer = _a.endBuffer;
79
- return [4 /*yield*/, editSingleQueryPackage(packageOPC, queryMashupDoc)];
80
- case 1:
81
- newPackageBuffer = _b.sent();
82
- packageSizeBuffer = _1.arrayUtils.getInt32Buffer(newPackageBuffer.byteLength);
83
- permissionsSizeBuffer = _1.arrayUtils.getInt32Buffer(permissionsSize);
84
- newMetadataBuffer = (0, exports.editSingleQueryMetadata)(metadata, { queryName: queryName });
85
- metadataSizeBuffer = _1.arrayUtils.getInt32Buffer(newMetadataBuffer.byteLength);
86
- newMashup = _1.arrayUtils.concatArrays(version, packageSizeBuffer, newPackageBuffer, permissionsSizeBuffer, permissions, metadataSizeBuffer, newMetadataBuffer, endBuffer);
87
- return [2 /*return*/, base64.fromByteArray(newMashup)];
88
- }
89
- });
90
- }); };
91
- exports.replaceSingleQuery = replaceSingleQuery;
92
- var getPackageComponents = function (base64Str) {
93
- var buffer = base64.toByteArray(base64Str).buffer;
94
- var mashupArray = new _1.arrayUtils.ArrayReader(buffer);
95
- var version = mashupArray.getBytes(4);
96
- var packageSize = mashupArray.getInt32();
97
- var packageOPC = mashupArray.getBytes(packageSize);
98
- var permissionsSize = mashupArray.getInt32();
99
- var permissions = mashupArray.getBytes(permissionsSize);
100
- var metadataSize = mashupArray.getInt32();
101
- var metadata = mashupArray.getBytes(metadataSize);
102
- var endBuffer = mashupArray.getBytes();
103
- return {
104
- version: version,
105
- packageOPC: packageOPC,
106
- permissionsSize: permissionsSize,
107
- permissions: permissions,
108
- metadata: metadata,
109
- endBuffer: endBuffer,
110
- };
111
- };
112
- exports.getPackageComponents = getPackageComponents;
113
- var editSingleQueryPackage = function (packageOPC, queryMashupDoc) { return __awaiter(void 0, void 0, void 0, function () {
114
- var packageZip;
115
- return __generator(this, function (_a) {
116
- switch (_a.label) {
117
- case 0: return [4 /*yield*/, jszip_1.default.loadAsync(packageOPC)];
118
- case 1:
119
- packageZip = _a.sent();
120
- setSection1m(queryMashupDoc, packageZip);
121
- return [4 /*yield*/, packageZip.generateAsync({ type: constants_1.uint8ArrayType })];
122
- case 2: return [2 /*return*/, _a.sent()];
123
- }
124
- });
125
- }); };
126
- var setSection1m = function (queryMashupDoc, zip) {
127
- var _a;
128
- if (!((_a = zip.file(constants_1.section1mPath)) === null || _a === void 0 ? void 0 : _a.async(constants_1.textResultType))) {
129
- throw new Error(constants_1.formulaSectionNotFoundErr);
130
- }
131
- var newSection1m = queryMashupDoc;
132
- zip.file(constants_1.section1mPath, newSection1m, {
133
- compression: constants_1.emptyValue,
134
- });
135
- };
136
- var editSingleQueryMetadata = function (metadataArray, metadata) {
137
- //extract metadataXml
138
- var mashupArray = new _1.arrayUtils.ArrayReader(metadataArray.buffer);
139
- var metadataVersion = mashupArray.getBytes(4);
140
- var metadataXmlSize = mashupArray.getInt32();
141
- var metadataXml = mashupArray.getBytes(metadataXmlSize);
142
- var endBuffer = mashupArray.getBytes();
143
- //parse metdataXml
144
- var textDecoder = new TextDecoder();
145
- var metadataString = textDecoder.decode(metadataXml);
146
- var parser = new xmldom_qsa_1.DOMParser();
147
- var serializer = new xmldom_qsa_1.XMLSerializer();
148
- var parsedMetadata = parser.parseFromString(metadataString, constants_1.xmlTextResultType);
149
- // Update InfoPaths to new QueryName
150
- var itemPaths = parsedMetadata.getElementsByTagName(constants_1.element.itemPath);
151
- if (itemPaths && itemPaths.length) {
152
- for (var i = 0; i < itemPaths.length; i++) {
153
- var itemPath = itemPaths[i];
154
- var content = itemPath.textContent;
155
- if (content.includes(constants_1.section1PathPrefix)) {
156
- var strArr = content.split(constants_1.divider);
157
- strArr[1] = encodeURIComponent(metadata.queryName);
158
- var newContent = strArr.join(constants_1.divider);
159
- itemPath.textContent = newContent;
160
- }
161
- }
162
- }
163
- var entries = parsedMetadata.getElementsByTagName(constants_1.element.entry);
164
- if (entries && entries.length) {
165
- for (var i = 0; i < entries.length; i++) {
166
- var entry = entries[i];
167
- var entryAttributesArr = Array.from(entry.attributes);
168
- var entryProp = entryAttributesArr.find(function (prop) {
169
- return (prop === null || prop === void 0 ? void 0 : prop.name) === constants_1.elementAttributes.type;
170
- });
171
- if ((entryProp === null || entryProp === void 0 ? void 0 : entryProp.nodeValue) == constants_1.elementAttributes.resultType) {
172
- entry.setAttribute(constants_1.elementAttributes.value, constants_1.elementAttributesValues.tableResultType());
173
- }
174
- if ((entryProp === null || entryProp === void 0 ? void 0 : entryProp.nodeValue) == constants_1.elementAttributes.fillLastUpdated) {
175
- var nowTime = new Date().toISOString();
176
- entry.setAttribute(constants_1.elementAttributes.value, (constants_1.elementAttributes.day + nowTime).replace(/Z/, "0000Z"));
177
- }
178
- }
179
- }
180
- // Convert new metadataXml to Uint8Array
181
- var newMetadataString = serializer.serializeToString(parsedMetadata);
182
- var encoder = new TextEncoder();
183
- var newMetadataXml = encoder.encode(newMetadataString);
184
- var newMetadataXmlSize = _1.arrayUtils.getInt32Buffer(newMetadataXml.byteLength);
185
- var newMetadataArray = _1.arrayUtils.concatArrays(metadataVersion, newMetadataXmlSize, newMetadataXml, endBuffer);
186
- return newMetadataArray;
187
- };
188
- exports.editSingleQueryMetadata = editSingleQueryMetadata;
@@ -1,194 +0,0 @@
1
- "use strict";
2
- // Copyright (c) Microsoft Corporation.
3
- // Licensed under the MIT license.
4
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6
- return new (P || (P = Promise))(function (resolve, reject) {
7
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10
- step((generator = generator.apply(thisArg, _arguments || [])).next());
11
- });
12
- };
13
- var __generator = (this && this.__generator) || function (thisArg, body) {
14
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
15
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
16
- function verb(n) { return function (v) { return step([n, v]); }; }
17
- function step(op) {
18
- if (f) throw new TypeError("Generator is already executing.");
19
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
20
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
21
- if (y = 0, t) op = [op[0] & 2, t.value];
22
- switch (op[0]) {
23
- case 0: case 1: t = op; break;
24
- case 4: _.label++; return { value: op[1], done: false };
25
- case 5: _.label++; y = op[1]; op = [0]; continue;
26
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
27
- default:
28
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
29
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
30
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
31
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
32
- if (t[2]) _.ops.pop();
33
- _.trys.pop(); continue;
34
- }
35
- op = body.call(thisArg, _);
36
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
37
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
38
- }
39
- };
40
- Object.defineProperty(exports, "__esModule", { value: true });
41
- var constants_1 = require("./constants");
42
- var generators_1 = require("../generators");
43
- var buffer_1 = require("buffer");
44
- var xmldom_qsa_1 = require("xmldom-qsa");
45
- var getBase64 = function (zip) { return __awaiter(void 0, void 0, void 0, function () {
46
- var mashup;
47
- return __generator(this, function (_a) {
48
- switch (_a.label) {
49
- case 0: return [4 /*yield*/, getDataMashupFile(zip)];
50
- case 1:
51
- mashup = _a.sent();
52
- return [2 /*return*/, mashup.value];
53
- }
54
- });
55
- }); };
56
- var setBase64 = function (zip, base64) { return __awaiter(void 0, void 0, void 0, function () {
57
- var newXml, encoded, mashup;
58
- return __generator(this, function (_a) {
59
- switch (_a.label) {
60
- case 0:
61
- newXml = (0, generators_1.generateMashupXMLTemplate)(base64);
62
- encoded = buffer_1.Buffer.from(constants_1.BOM + newXml, "ucs2");
63
- return [4 /*yield*/, getDataMashupFile(zip)];
64
- case 1:
65
- mashup = _a.sent();
66
- zip.file(mashup === null || mashup === void 0 ? void 0 : mashup.path, encoded);
67
- return [2 /*return*/];
68
- }
69
- });
70
- }); };
71
- var getDataMashupFile = function (zip) { return __awaiter(void 0, void 0, void 0, function () {
72
- var mashup, _i, _a, url, item;
73
- return __generator(this, function (_b) {
74
- switch (_b.label) {
75
- case 0:
76
- _i = 0, _a = constants_1.URLS.PQ;
77
- _b.label = 1;
78
- case 1:
79
- if (!(_i < _a.length)) return [3 /*break*/, 4];
80
- url = _a[_i];
81
- return [4 /*yield*/, getCustomXmlFile(zip, url)];
82
- case 2:
83
- item = _b.sent();
84
- if (item.found) {
85
- mashup = item;
86
- return [3 /*break*/, 4];
87
- }
88
- _b.label = 3;
89
- case 3:
90
- _i++;
91
- return [3 /*break*/, 1];
92
- case 4:
93
- if (!mashup) {
94
- throw new Error("DataMashup XML is not found");
95
- }
96
- return [2 /*return*/, mashup];
97
- }
98
- });
99
- }); };
100
- var getCustomXmlFile = function (zip, url) { return __awaiter(void 0, void 0, void 0, function () {
101
- var parser, itemsArray, found, path, xmlString, value, i, xmlValue, buffer, encoding, doc;
102
- var _a, _b;
103
- return __generator(this, function (_c) {
104
- switch (_c.label) {
105
- case 0:
106
- parser = new xmldom_qsa_1.DOMParser();
107
- return [4 /*yield*/, zip.file(/customXml\/item\d.xml/)];
108
- case 1:
109
- itemsArray = _c.sent();
110
- if (!itemsArray || itemsArray.length === 0) {
111
- throw new Error("No customXml files were found!");
112
- }
113
- found = false;
114
- value = null;
115
- i = 1;
116
- _c.label = 2;
117
- case 2:
118
- if (!(i <= itemsArray.length)) return [3 /*break*/, 5];
119
- path = (0, generators_1.generateCustomXmlFilePath)(i);
120
- return [4 /*yield*/, ((_a = zip.file(path)) === null || _a === void 0 ? void 0 : _a.async("uint8array"))];
121
- case 3:
122
- xmlValue = _c.sent();
123
- if (xmlValue === undefined) {
124
- return [3 /*break*/, 5];
125
- }
126
- buffer = buffer_1.Buffer.from(xmlValue);
127
- encoding = detectEncoding(xmlValue);
128
- if (!encoding) {
129
- throw new Error("Failed to detect xml encoding");
130
- }
131
- xmlString = buffer
132
- .toString(encoding)
133
- .replace(/^\ufeff/, "");
134
- doc = parser.parseFromString(xmlString, "text/xml");
135
- found = ((_b = doc === null || doc === void 0 ? void 0 : doc.documentElement) === null || _b === void 0 ? void 0 : _b.namespaceURI) === url;
136
- if (found) {
137
- value = doc.documentElement.textContent;
138
- return [3 /*break*/, 5];
139
- }
140
- _c.label = 4;
141
- case 4:
142
- i++;
143
- return [3 /*break*/, 2];
144
- case 5: return [2 /*return*/, { found: found, path: path, xmlString: xmlString, value: value }];
145
- }
146
- });
147
- }); };
148
- var queryNameHasInvalidChars = function (queryName) {
149
- var invalidQueryNameChars = ['"', "."];
150
- // Control characters as defined in Unicode
151
- for (var c = 0; c <= 0x001f; ++c) {
152
- invalidQueryNameChars.push(String.fromCharCode(c));
153
- }
154
- for (var c = 0x007f; c <= 0x009f; ++c) {
155
- invalidQueryNameChars.push(String.fromCharCode(c));
156
- }
157
- return queryName.split("").some(function (ch) { return invalidQueryNameChars.indexOf(ch) !== -1; });
158
- };
159
- var validateQueryName = function (queryName) {
160
- if (queryName) {
161
- if (queryName.length > constants_1.maxQueryLength) {
162
- throw new Error(constants_1.QueryNameMaxLengthErr);
163
- }
164
- if (queryNameHasInvalidChars(queryName)) {
165
- throw new Error(constants_1.QueryNameInvalidCharsErr);
166
- }
167
- }
168
- if (!queryName.trim()) {
169
- throw new Error(constants_1.EmptyQueryNameErr);
170
- }
171
- };
172
- var detectEncoding = function (xmlBytes) {
173
- if (!xmlBytes) {
174
- return null;
175
- }
176
- if (xmlBytes.length >= 3 && xmlBytes[0] === 0xEF && xmlBytes[1] === 0xBB && xmlBytes[2] === 0xBF) {
177
- return 'utf-8';
178
- }
179
- if (xmlBytes.length >= 3 && xmlBytes[0] === 0xFF && xmlBytes[1] === 0xFE) {
180
- return 'utf-16le';
181
- }
182
- if (xmlBytes.length >= 3 && xmlBytes[0] === 0xFE && xmlBytes[1] === 0xFF) {
183
- return 'utf-16be';
184
- }
185
- // Default to utf-8, that not required a BOM for encoding.
186
- return 'utf-8';
187
- };
188
- exports.default = {
189
- getBase64: getBase64,
190
- setBase64: setBase64,
191
- getCustomXmlFile: getCustomXmlFile,
192
- getDataMashupFile: getDataMashupFile,
193
- validateQueryName: validateQueryName,
194
- };
@@ -1,233 +0,0 @@
1
- "use strict";
2
- // Copyright (c) Microsoft Corporation.
3
- // Licensed under the MIT license.
4
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
5
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
6
- return new (P || (P = Promise))(function (resolve, reject) {
7
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
9
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
10
- step((generator = generator.apply(thisArg, _arguments || [])).next());
11
- });
12
- };
13
- var __generator = (this && this.__generator) || function (thisArg, body) {
14
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
15
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
16
- function verb(n) { return function (v) { return step([n, v]); }; }
17
- function step(op) {
18
- if (f) throw new TypeError("Generator is already executing.");
19
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
20
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
21
- if (y = 0, t) op = [op[0] & 2, t.value];
22
- switch (op[0]) {
23
- case 0: case 1: t = op; break;
24
- case 4: _.label++; return { value: op[1], done: false };
25
- case 5: _.label++; y = op[1]; op = [0]; continue;
26
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
27
- default:
28
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
29
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
30
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
31
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
32
- if (t[2]) _.ops.pop();
33
- _.trys.pop(); continue;
34
- }
35
- op = body.call(thisArg, _);
36
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
37
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
38
- }
39
- };
40
- var __importDefault = (this && this.__importDefault) || function (mod) {
41
- return (mod && mod.__esModule) ? mod : { "default": mod };
42
- };
43
- Object.defineProperty(exports, "__esModule", { value: true });
44
- var constants_1 = require("./constants");
45
- var documentUtils_1 = __importDefault(require("./documentUtils"));
46
- var uuid_1 = require("uuid");
47
- var xmldom_qsa_1 = require("xmldom-qsa");
48
- /**
49
- * Update initial data for a table, its sheet, query table, and defined name if provided.
50
- * @param zip - The JSZip instance containing workbook parts.
51
- * @param cellRangeRef - Cell range reference (e.g. "A1:C5").
52
- * @param sheetPath - Path to the sheet XML within the zip.
53
- * @param tablePath - Path to the table XML within the zip.
54
- * @param tableName - Name of the table.
55
- * @param tableData - Optional TableData containing headers and rows.
56
- * @param updateQueryTable - Whether to update the associated queryTable part.
57
- */
58
- var updateTableInitialDataIfNeeded = function (zip, cellRangeRef, sheetPath, tablePath, sheetName, tableData, updateQueryTable) { return __awaiter(void 0, void 0, void 0, function () {
59
- var sheetsXmlString, newSheet, queryTableXmlString, newQueryTable, workbookXmlString, newWorkbook, tableXmlString, newTable;
60
- var _a, _b, _c, _d;
61
- return __generator(this, function (_e) {
62
- switch (_e.label) {
63
- case 0:
64
- if (!tableData) {
65
- return [2 /*return*/];
66
- }
67
- return [4 /*yield*/, ((_a = zip.file(sheetPath)) === null || _a === void 0 ? void 0 : _a.async(constants_1.textResultType))];
68
- case 1:
69
- sheetsXmlString = _e.sent();
70
- if (sheetsXmlString === undefined) {
71
- throw new Error(constants_1.sheetsNotFoundErr);
72
- }
73
- newSheet = updateSheetsInitialData(sheetsXmlString, tableData, cellRangeRef);
74
- zip.file(sheetPath, newSheet);
75
- if (!updateQueryTable) return [3 /*break*/, 5];
76
- return [4 /*yield*/, ((_b = zip.file(constants_1.queryTableXmlPath)) === null || _b === void 0 ? void 0 : _b.async(constants_1.textResultType))];
77
- case 2:
78
- queryTableXmlString = _e.sent();
79
- if (queryTableXmlString === undefined) {
80
- throw new Error(constants_1.queryTableNotFoundErr);
81
- }
82
- return [4 /*yield*/, updateQueryTablesInitialData(queryTableXmlString, tableData)];
83
- case 3:
84
- newQueryTable = _e.sent();
85
- zip.file(constants_1.queryTableXmlPath, newQueryTable);
86
- return [4 /*yield*/, ((_c = zip.file(constants_1.workbookXmlPath)) === null || _c === void 0 ? void 0 : _c.async(constants_1.textResultType))];
87
- case 4:
88
- workbookXmlString = _e.sent();
89
- if (workbookXmlString === undefined) {
90
- throw new Error(constants_1.sheetsNotFoundErr);
91
- }
92
- newWorkbook = updateWorkbookInitialData(workbookXmlString, sheetName + GenerateReferenceFromString(cellRangeRef));
93
- zip.file(constants_1.workbookXmlPath, newWorkbook);
94
- _e.label = 5;
95
- case 5: return [4 /*yield*/, ((_d = zip.file(tablePath)) === null || _d === void 0 ? void 0 : _d.async(constants_1.textResultType))];
96
- case 6:
97
- tableXmlString = _e.sent();
98
- if (tableXmlString === undefined) {
99
- throw new Error(constants_1.tableNotFoundErr);
100
- }
101
- newTable = updateTablesInitialData(tableXmlString, tableData, cellRangeRef, updateQueryTable);
102
- zip.file(tablePath, newTable);
103
- return [2 /*return*/];
104
- }
105
- });
106
- }); };
107
- /**
108
- * Generate updated table XML string with new columns, reference, and filter range.
109
- * @param tableXmlString - Original table XML.
110
- * @param tableData - TableData containing column names.
111
- * @param cellRangeRef - Cell range reference.
112
- * @param updateQueryTable - Whether to include queryTable attributes.
113
- * @returns Serialized XML string of the updated table.
114
- */
115
- var updateTablesInitialData = function (tableXmlString, tableData, cellRangeRef, updateQueryTable) {
116
- if (updateQueryTable === void 0) { updateQueryTable = false; }
117
- var parser = new xmldom_qsa_1.DOMParser();
118
- var serializer = new xmldom_qsa_1.XMLSerializer();
119
- var tableDoc = parser.parseFromString(tableXmlString, constants_1.xmlTextResultType);
120
- var tableColumns = tableDoc.getElementsByTagName(constants_1.element.tableColumns)[0];
121
- tableColumns.textContent = "";
122
- tableData.columnNames.forEach(function (column, columnIndex) {
123
- var tableColumn = tableDoc.createElementNS(tableDoc.documentElement.namespaceURI, constants_1.element.tableColumn);
124
- tableColumn.setAttribute(constants_1.elementAttributes.id, (columnIndex + 1).toString());
125
- tableColumn.setAttribute(constants_1.elementAttributes.name, column);
126
- tableColumns.appendChild(tableColumn);
127
- tableColumn.setAttribute(constants_1.elementAttributes.xr3uid, "{" + (0, uuid_1.v4)().toUpperCase() + "}");
128
- if (updateQueryTable) {
129
- tableColumn.setAttribute(constants_1.elementAttributes.uniqueName, (columnIndex + 1).toString());
130
- tableColumn.setAttribute(constants_1.elementAttributes.queryTableFieldId, (columnIndex + 1).toString());
131
- }
132
- });
133
- tableColumns.setAttribute(constants_1.elementAttributes.count, tableData.columnNames.length.toString());
134
- tableDoc
135
- .getElementsByTagName(constants_1.element.table)[0]
136
- .setAttribute(constants_1.elementAttributes.reference, cellRangeRef);
137
- tableDoc
138
- .getElementsByTagName(constants_1.element.autoFilter)[0]
139
- .setAttribute(constants_1.elementAttributes.reference, cellRangeRef);
140
- return serializer.serializeToString(tableDoc);
141
- };
142
- /**
143
- * Update the definedName element in workbook XML to a custom name.
144
- * @param workbookXmlString - Original workbook XML string.
145
- * @param customDefinedName - New defined name text content (e.g. "!$A$1:$C$5").
146
- * @returns Serialized XML string of the updated workbook.
147
- */
148
- var updateWorkbookInitialData = function (workbookXmlString, customDefinedName) {
149
- var newParser = new xmldom_qsa_1.DOMParser();
150
- var newSerializer = new xmldom_qsa_1.XMLSerializer();
151
- var workbookDoc = newParser.parseFromString(workbookXmlString, constants_1.xmlTextResultType);
152
- var definedName = workbookDoc.getElementsByTagName(constants_1.element.definedName)[0];
153
- definedName.textContent = customDefinedName;
154
- return newSerializer.serializeToString(workbookDoc);
155
- };
156
- var updateQueryTablesInitialData = function (queryTableXmlString, tableData) {
157
- var parser = new xmldom_qsa_1.DOMParser();
158
- var serializer = new xmldom_qsa_1.XMLSerializer();
159
- var queryTableDoc = parser.parseFromString(queryTableXmlString, constants_1.xmlTextResultType);
160
- var queryTableFields = queryTableDoc.getElementsByTagName(constants_1.element.queryTableFields)[0];
161
- queryTableFields.textContent = "";
162
- tableData.columnNames.forEach(function (column, columnIndex) {
163
- var queryTableField = queryTableDoc.createElementNS(queryTableDoc.documentElement.namespaceURI, constants_1.element.queryTableField);
164
- queryTableField.setAttribute(constants_1.elementAttributes.id, (columnIndex + 1).toString());
165
- queryTableField.setAttribute(constants_1.elementAttributes.name, column);
166
- queryTableField.setAttribute(constants_1.elementAttributes.tableColumnId, (columnIndex + 1).toString());
167
- queryTableFields.appendChild(queryTableField);
168
- });
169
- queryTableFields.setAttribute(constants_1.elementAttributes.count, tableData.columnNames.length.toString());
170
- queryTableDoc.getElementsByTagName(constants_1.element.queryTableRefresh)[0].setAttribute(constants_1.elementAttributes.nextId, (tableData.columnNames.length + 1).toString());
171
- return serializer.serializeToString(queryTableDoc);
172
- };
173
- /**
174
- * Update sheet XML with header row and data rows based on TableData.
175
- * @param sheetsXmlString - Original sheet XML string.
176
- * @param tableData - TableData containing headers and rows.
177
- * @param cellRangeRef - Cell range reference.
178
- * @returns Serialized XML string of the updated sheet.
179
- */
180
- var updateSheetsInitialData = function (sheetsXmlString, tableData, cellRangeRef) {
181
- var _a = documentUtils_1.default.GetStartPosition(cellRangeRef), row = _a.row, column = _a.column;
182
- var parser = new xmldom_qsa_1.DOMParser();
183
- var serializer = new xmldom_qsa_1.XMLSerializer();
184
- var sheetsDoc = parser.parseFromString(sheetsXmlString, constants_1.xmlTextResultType);
185
- var sheetData = sheetsDoc.getElementsByTagName(constants_1.element.sheetData)[0];
186
- sheetData.textContent = "";
187
- var columnRow = sheetsDoc.createElementNS(sheetsDoc.documentElement.namespaceURI, constants_1.element.row);
188
- columnRow.setAttribute(constants_1.elementAttributes.row, row.toString());
189
- columnRow.setAttribute(constants_1.elementAttributes.spans, column + ":" + (column + tableData.columnNames.length - 1));
190
- columnRow.setAttribute(constants_1.elementAttributes.x14acDyDescent, "0.3");
191
- tableData.columnNames.forEach(function (col, colIndex) {
192
- columnRow.appendChild(documentUtils_1.default.createCell(sheetsDoc, colIndex + column - 1, row - 1, col));
193
- });
194
- sheetData.appendChild(columnRow);
195
- row++;
196
- tableData.rows.forEach(function (_row) {
197
- var newRow = sheetsDoc.createElementNS(sheetsDoc.documentElement.namespaceURI, constants_1.element.row);
198
- newRow.setAttribute(constants_1.elementAttributes.row, row.toString());
199
- newRow.setAttribute(constants_1.elementAttributes.spans, column + ":" + (column + tableData.columnNames.length - 1));
200
- newRow.setAttribute(constants_1.elementAttributes.x14acDyDescent, "0.3");
201
- _row.forEach(function (cellContent, colIndex) {
202
- newRow.appendChild(documentUtils_1.default.createCell(sheetsDoc, colIndex + column - 1, row - 1, cellContent));
203
- });
204
- sheetData.appendChild(newRow);
205
- row++;
206
- });
207
- sheetsDoc.getElementsByTagName(constants_1.element.dimension)[0].setAttribute(constants_1.elementAttributes.reference, cellRangeRef);
208
- sheetsDoc.getElementsByTagName(constants_1.element.selection)[0].setAttribute(constants_1.elementAttributes.sqref, cellRangeRef);
209
- return serializer.serializeToString(sheetsDoc);
210
- };
211
- /**
212
- * Add Excel-style dollar signs and a '!' prefix to a cell range.
213
- * Converts "A1:B2" into "!$A$1:$B$2".
214
- * @param cellRangeRef - Range reference string without dollar signs.
215
- * @returns Range with dollar signs and prefix.
216
- */
217
- var GenerateReferenceFromString = function (cellRangeRef) {
218
- return "!" + cellRangeRef.split(":").map(function (part) {
219
- var match = part.match(/^([A-Za-z]+)(\d+)$/);
220
- if (match) {
221
- var col = match[1], row = match[2];
222
- return "$".concat(col.toUpperCase(), "$").concat(row);
223
- }
224
- }).join(":");
225
- };
226
- exports.default = {
227
- updateTableInitialDataIfNeeded: updateTableInitialDataIfNeeded,
228
- updateSheetsInitialData: updateSheetsInitialData,
229
- updateWorkbookInitialData: updateWorkbookInitialData,
230
- updateTablesInitialData: updateTablesInitialData,
231
- updateQueryTablesInitialData: updateQueryTablesInitialData,
232
- GenerateReferenceFromString: GenerateReferenceFromString,
233
- };