@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,98 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ const constants_1 = require("./constants");
15
+ const types_1 = require("../types");
16
+ const createOrUpdateProperty = (doc, parent, property, value) => {
17
+ if (value === undefined) {
18
+ return;
19
+ }
20
+ const elements = parent.getElementsByTagName(property);
21
+ if ((elements === null || elements === void 0 ? void 0 : elements.length) === 0) {
22
+ const newElement = doc.createElement(property);
23
+ newElement.textContent = value;
24
+ parent.appendChild(newElement);
25
+ }
26
+ else if (elements.length > 1) {
27
+ throw new Error(`Invalid DocProps core.xml, multiple ${property} elements`);
28
+ }
29
+ else if ((elements === null || elements === void 0 ? void 0 : elements.length) > 0) {
30
+ elements[0].textContent = value;
31
+ }
32
+ };
33
+ const getDocPropsProperties = (zip) => __awaiter(void 0, void 0, void 0, function* () {
34
+ var _a;
35
+ const docPropsCoreXmlString = yield ((_a = zip.file(constants_1.docPropsCoreXmlPath)) === null || _a === void 0 ? void 0 : _a.async(constants_1.textResultType));
36
+ if (docPropsCoreXmlString === undefined) {
37
+ throw new Error("DocProps core.xml was not found in template");
38
+ }
39
+ const parser = new DOMParser();
40
+ const doc = parser.parseFromString(docPropsCoreXmlString, constants_1.xmlTextResultType);
41
+ const properties = doc.getElementsByTagName(constants_1.docPropsRootElement).item(0);
42
+ if (properties === null) {
43
+ throw new Error("Invalid DocProps core.xml");
44
+ }
45
+ return { doc, properties };
46
+ });
47
+ const getCellReferenceAbsolute = (col, row) => {
48
+ // 65 is the ascii value of first column 'A'
49
+ return "$" + String.fromCharCode(col + 65) + "$" + row.toString();
50
+ };
51
+ const getCellReferenceRelative = (col, row) => {
52
+ // 65 is the ascii value of first column 'A'
53
+ return String.fromCharCode(col + 65) + row.toString();
54
+ };
55
+ const getTableReference = (numberOfCols, numberOfRows) => {
56
+ return `A1:${getCellReferenceRelative(numberOfCols, numberOfRows)}`;
57
+ };
58
+ const createCellElement = (doc, colIndex, rowIndex, data) => {
59
+ const cell = doc.createElementNS(doc.documentElement.namespaceURI, constants_1.element.kindCell);
60
+ cell.setAttribute(constants_1.elementAttributes.row, getCellReferenceRelative(colIndex, rowIndex + 1));
61
+ const cellData = doc.createElementNS(doc.documentElement.namespaceURI, constants_1.element.cellValue);
62
+ updateCellData(data, cell, cellData);
63
+ cell.appendChild(cellData);
64
+ return cell;
65
+ };
66
+ const updateCellData = (data, cell, cellData) => {
67
+ switch (resolveType(data)) {
68
+ case types_1.DataTypes.string:
69
+ cell.setAttribute(constants_1.element.text, constants_1.dataTypeKind.string);
70
+ break;
71
+ case types_1.DataTypes.number:
72
+ cell.setAttribute(constants_1.element.text, constants_1.dataTypeKind.number);
73
+ break;
74
+ case types_1.DataTypes.boolean:
75
+ cell.setAttribute(constants_1.element.text, constants_1.dataTypeKind.boolean);
76
+ break;
77
+ }
78
+ cellData.textContent = data;
79
+ };
80
+ const resolveType = (originalData) => {
81
+ const data = originalData;
82
+ let dataType = isNaN(Number(data)) ? types_1.DataTypes.string : types_1.DataTypes.number;
83
+ if (dataType == types_1.DataTypes.string) {
84
+ if (data.toLowerCase().trim() == constants_1.trueStr || data.toLowerCase().trim() == constants_1.falseStr) {
85
+ dataType = types_1.DataTypes.boolean;
86
+ }
87
+ }
88
+ return dataType;
89
+ };
90
+ exports.default = {
91
+ createOrUpdateProperty,
92
+ getDocPropsProperties,
93
+ getCellReferenceRelative,
94
+ getCellReferenceAbsolute,
95
+ createCell: createCellElement,
96
+ getTableReference,
97
+ resolveType,
98
+ };
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.extractTableValues = void 0;
4
+ const extractTableValues = (table) => {
5
+ const headers = [];
6
+ const rows = [];
7
+ // Extract values from each row
8
+ for (let i = 0; i < table.rows.length; i++) {
9
+ const row = table.rows[i];
10
+ const rowData = [];
11
+ for (let j = 0; j < row.cells.length; j++) {
12
+ const cell = row.cells[j];
13
+ rowData.push(cell.textContent || "");
14
+ }
15
+ rows.push(rowData);
16
+ }
17
+ return rows;
18
+ };
19
+ exports.extractTableValues = extractTableValues;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ // Copyright (c) Microsoft Corporation.
3
+ // Licensed under the MIT license.
4
+ var __importDefault = (this && this.__importDefault) || function (mod) {
5
+ return (mod && mod.__esModule) ? mod : { "default": mod };
6
+ };
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.tableUtils = exports.xmlInnerPartsUtils = exports.xmlPartsUtils = exports.documentUtils = exports.arrayUtils = exports.pqUtils = void 0;
9
+ var pqUtils_1 = require("./pqUtils");
10
+ Object.defineProperty(exports, "pqUtils", { enumerable: true, get: function () { return __importDefault(pqUtils_1).default; } });
11
+ var arrayUtils_1 = require("./arrayUtils");
12
+ Object.defineProperty(exports, "arrayUtils", { enumerable: true, get: function () { return __importDefault(arrayUtils_1).default; } });
13
+ var documentUtils_1 = require("./documentUtils");
14
+ Object.defineProperty(exports, "documentUtils", { enumerable: true, get: function () { return __importDefault(documentUtils_1).default; } });
15
+ var xmlPartsUtils_1 = require("./xmlPartsUtils");
16
+ Object.defineProperty(exports, "xmlPartsUtils", { enumerable: true, get: function () { return __importDefault(xmlPartsUtils_1).default; } });
17
+ var xmlInnerPartsUtils_1 = require("./xmlInnerPartsUtils");
18
+ Object.defineProperty(exports, "xmlInnerPartsUtils", { enumerable: true, get: function () { return __importDefault(xmlInnerPartsUtils_1).default; } });
19
+ var tableUtils_1 = require("./tableUtils");
20
+ Object.defineProperty(exports, "tableUtils", { enumerable: true, get: function () { return __importDefault(tableUtils_1).default; } });
@@ -0,0 +1,163 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
37
+ return (mod && mod.__esModule) ? mod : { "default": mod };
38
+ };
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.replaceSingleQuery = void 0;
41
+ const base64 = __importStar(require("base64-js"));
42
+ const jszip_1 = __importDefault(require("jszip"));
43
+ const constants_1 = require("./constants");
44
+ const _1 = require(".");
45
+ const replaceSingleQuery = (base64Str, queryName, queryMashupDoc) => __awaiter(void 0, void 0, void 0, function* () {
46
+ const { version, packageOPC, permissionsSize, permissions, metadata, endBuffer } = getPackageComponents(base64Str);
47
+ const newPackageBuffer = yield editSingleQueryPackage(packageOPC, queryMashupDoc);
48
+ const packageSizeBuffer = _1.arrayUtils.getInt32Buffer(newPackageBuffer.byteLength);
49
+ const permissionsSizeBuffer = _1.arrayUtils.getInt32Buffer(permissionsSize);
50
+ const newMetadataBuffer = editSingleQueryMetadata(metadata, { queryName });
51
+ const metadataSizeBuffer = _1.arrayUtils.getInt32Buffer(newMetadataBuffer.byteLength);
52
+ const newMashup = _1.arrayUtils.concatArrays(version, packageSizeBuffer, newPackageBuffer, permissionsSizeBuffer, permissions, metadataSizeBuffer, newMetadataBuffer, endBuffer);
53
+ return base64.fromByteArray(newMashup);
54
+ });
55
+ exports.replaceSingleQuery = replaceSingleQuery;
56
+ const getPackageComponents = (base64Str) => {
57
+ const buffer = base64.toByteArray(base64Str).buffer;
58
+ const mashupArray = new _1.arrayUtils.ArrayReader(buffer);
59
+ const version = mashupArray.getBytes(4);
60
+ const packageSize = mashupArray.getInt32();
61
+ const packageOPC = mashupArray.getBytes(packageSize);
62
+ const permissionsSize = mashupArray.getInt32();
63
+ const permissions = mashupArray.getBytes(permissionsSize);
64
+ const metadataSize = mashupArray.getInt32();
65
+ const metadata = mashupArray.getBytes(metadataSize);
66
+ const endBuffer = mashupArray.getBytes();
67
+ return {
68
+ version,
69
+ packageOPC,
70
+ permissionsSize,
71
+ permissions,
72
+ metadata,
73
+ endBuffer,
74
+ };
75
+ };
76
+ const editSingleQueryPackage = (packageOPC, queryMashupDoc) => __awaiter(void 0, void 0, void 0, function* () {
77
+ const packageZip = yield jszip_1.default.loadAsync(packageOPC);
78
+ setSection1m(queryMashupDoc, packageZip);
79
+ return yield packageZip.generateAsync({ type: constants_1.uint8ArrayType });
80
+ });
81
+ const setSection1m = (queryMashupDoc, zip) => {
82
+ var _a;
83
+ if (!((_a = zip.file(constants_1.section1mPath)) === null || _a === void 0 ? void 0 : _a.async(constants_1.textResultType))) {
84
+ throw new Error(constants_1.formulaSectionNotFoundErr);
85
+ }
86
+ const newSection1m = queryMashupDoc;
87
+ zip.file(constants_1.section1mPath, newSection1m, {
88
+ compression: constants_1.emptyValue,
89
+ });
90
+ };
91
+ const editSingleQueryMetadata = (metadataArray, metadata) => {
92
+ var _a;
93
+ //extract metadataXml
94
+ const mashupArray = new _1.arrayUtils.ArrayReader(metadataArray.buffer);
95
+ const metadataVersion = mashupArray.getBytes(4);
96
+ const metadataXmlSize = mashupArray.getInt32();
97
+ const metadataXml = mashupArray.getBytes(metadataXmlSize);
98
+ const endBuffer = mashupArray.getBytes();
99
+ //parse metdataXml
100
+ const textDecoder = new TextDecoder();
101
+ const metadataString = textDecoder.decode(metadataXml);
102
+ const parser = new DOMParser();
103
+ const serializer = new XMLSerializer();
104
+ const parsedMetadata = parser.parseFromString(metadataString, constants_1.xmlTextResultType);
105
+ // Update InfoPaths to new QueryName
106
+ const itemPaths = parsedMetadata.getElementsByTagName(constants_1.element.itemPath);
107
+ if (itemPaths && itemPaths.length) {
108
+ for (let i = 0; i < itemPaths.length; i++) {
109
+ const itemPath = itemPaths[i];
110
+ const content = itemPath.innerHTML;
111
+ if (content.includes(constants_1.section1PathPrefix)) {
112
+ const strArr = content.split(constants_1.divider);
113
+ strArr[1] = metadata.queryName;
114
+ const newContent = strArr.join(constants_1.divider);
115
+ itemPath.textContent = newContent;
116
+ }
117
+ }
118
+ }
119
+ const entries = parsedMetadata.getElementsByTagName(constants_1.element.entry);
120
+ if (entries && entries.length) {
121
+ for (let i = 0; i < entries.length; i++) {
122
+ const entry = entries[i];
123
+ const entryAttributes = entry.attributes;
124
+ const entryAttributesArr = [...entryAttributes];
125
+ const entryProp = entryAttributesArr.find((prop) => {
126
+ return (prop === null || prop === void 0 ? void 0 : prop.name) === constants_1.elementAttributes.type;
127
+ });
128
+ if ((entryProp === null || entryProp === void 0 ? void 0 : entryProp.nodeValue) == constants_1.elementAttributes.relationshipInfo) {
129
+ const newValue = (_a = entry
130
+ .getAttribute(constants_1.elementAttributes.value)) === null || _a === void 0 ? void 0 : _a.replace(/Query1/g, metadata.queryName);
131
+ if (newValue) {
132
+ entry.setAttribute(constants_1.elementAttributes.value, newValue);
133
+ }
134
+ }
135
+ if ((entryProp === null || entryProp === void 0 ? void 0 : entryProp.nodeValue) == constants_1.elementAttributes.resultType) {
136
+ entry.setAttribute(constants_1.elementAttributes.value, constants_1.elementAttributesValues.tableResultType());
137
+ }
138
+ if ((entryProp === null || entryProp === void 0 ? void 0 : entryProp.nodeValue) == constants_1.elementAttributes.fillColumnNames) {
139
+ const oldValue = entry.getAttribute(constants_1.elementAttributes.value);
140
+ if (oldValue) {
141
+ entry.setAttribute(constants_1.elementAttributes.value, oldValue.replace(constants_1.defaults.queryName, metadata.queryName));
142
+ }
143
+ }
144
+ if ((entryProp === null || entryProp === void 0 ? void 0 : entryProp.nodeValue) == constants_1.elementAttributes.fillTarget) {
145
+ const oldValue = entry.getAttribute(constants_1.elementAttributes.value);
146
+ if (oldValue) {
147
+ entry.setAttribute(constants_1.elementAttributes.value, oldValue.replace(constants_1.defaults.queryName, metadata.queryName));
148
+ }
149
+ }
150
+ if ((entryProp === null || entryProp === void 0 ? void 0 : entryProp.nodeValue) == constants_1.elementAttributes.fillLastUpdated) {
151
+ const nowTime = new Date().toISOString();
152
+ entry.setAttribute(constants_1.elementAttributes.value, (constants_1.elementAttributes.day + nowTime).replace(/Z/, "0000Z"));
153
+ }
154
+ }
155
+ }
156
+ // Convert new metadataXml to Uint8Array
157
+ const newMetadataString = serializer.serializeToString(parsedMetadata);
158
+ const encoder = new TextEncoder();
159
+ const newMetadataXml = encoder.encode(newMetadataString);
160
+ const newMetadataXmlSize = _1.arrayUtils.getInt32Buffer(newMetadataXml.byteLength);
161
+ const newMetadataArray = _1.arrayUtils.concatArrays(metadataVersion, newMetadataXmlSize, newMetadataXml, endBuffer);
162
+ return newMetadataArray;
163
+ };
@@ -0,0 +1,97 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ const constants_1 = require("./constants");
15
+ const generators_1 = require("../generators");
16
+ const getBase64 = (zip) => __awaiter(void 0, void 0, void 0, function* () {
17
+ const mashup = yield getDataMashupFile(zip);
18
+ return mashup.value;
19
+ });
20
+ const setBase64 = (zip, base64) => __awaiter(void 0, void 0, void 0, function* () {
21
+ const newXml = (0, generators_1.generateMashupXMLTemplate)(base64);
22
+ const encoded = Buffer.from(constants_1.BOM + newXml, 'ucs2');
23
+ const mashup = yield getDataMashupFile(zip);
24
+ zip.file(mashup === null || mashup === void 0 ? void 0 : mashup.path, encoded);
25
+ });
26
+ const getDataMashupFile = (zip) => __awaiter(void 0, void 0, void 0, function* () {
27
+ let mashup;
28
+ for (const url of constants_1.URLS.PQ) {
29
+ const item = yield getCustomXmlFile(zip, url);
30
+ if (item.found) {
31
+ mashup = item;
32
+ break;
33
+ }
34
+ }
35
+ if (!mashup) {
36
+ throw new Error("DataMashup XML is not found");
37
+ }
38
+ return mashup;
39
+ });
40
+ const getCustomXmlFile = (zip, url, encoding = 'utf16le') => __awaiter(void 0, void 0, void 0, function* () {
41
+ var _a, _b;
42
+ const parser = new DOMParser();
43
+ const itemsArray = yield zip.file(/customXml\/item\d.xml/);
44
+ if (!itemsArray || itemsArray.length === 0) {
45
+ throw new Error("No customXml files were found!");
46
+ }
47
+ let found = false;
48
+ let path;
49
+ let xmlString;
50
+ let value;
51
+ for (let i = 1; i <= itemsArray.length; i++) {
52
+ path = (0, generators_1.generateCustomXmlFilePath)(i);
53
+ const xmlValue = yield ((_a = zip.file(path)) === null || _a === void 0 ? void 0 : _a.async("uint8array"));
54
+ if (xmlValue === undefined) {
55
+ break;
56
+ }
57
+ xmlString = Buffer.from(xmlValue).toString(encoding).replace(/^\ufeff/, '');
58
+ const doc = parser.parseFromString(xmlString, "text/xml");
59
+ found = ((_b = doc === null || doc === void 0 ? void 0 : doc.documentElement) === null || _b === void 0 ? void 0 : _b.namespaceURI) === url;
60
+ if (found) {
61
+ value = doc.documentElement.innerHTML;
62
+ break;
63
+ }
64
+ }
65
+ return { found, path: path, xmlString: xmlString, value };
66
+ });
67
+ const queryNameHasInvalidChars = (queryName) => {
68
+ const invalidQueryNameChars = ['"', "."];
69
+ // Control characters as defined in Unicode
70
+ for (let c = 0; c <= 0x001f; ++c) {
71
+ invalidQueryNameChars.push(String.fromCharCode(c));
72
+ }
73
+ for (let c = 0x007f; c <= 0x009f; ++c) {
74
+ invalidQueryNameChars.push(String.fromCharCode(c));
75
+ }
76
+ return queryName.split("").some((ch) => invalidQueryNameChars.indexOf(ch) !== -1);
77
+ };
78
+ const validateQueryName = (queryName) => {
79
+ if (queryName) {
80
+ if (queryName.length > constants_1.maxQueryLength) {
81
+ throw new Error(constants_1.QueryNameMaxLengthErr);
82
+ }
83
+ if (queryNameHasInvalidChars(queryName)) {
84
+ throw new Error(constants_1.QueryNameMaxLengthErr);
85
+ }
86
+ }
87
+ if (!queryName.trim()) {
88
+ throw new Error(constants_1.EmptyQueryNameErr);
89
+ }
90
+ };
91
+ exports.default = {
92
+ getBase64,
93
+ setBase64,
94
+ getCustomXmlFile,
95
+ getDataMashupFile,
96
+ validateQueryName,
97
+ };
@@ -0,0 +1,144 @@
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 documentUtils_1 = __importDefault(require("./documentUtils"));
17
+ const uuid_1 = require("uuid");
18
+ const updateTableInitialDataIfNeeded = (zip, tableData, updateQueryTable) => __awaiter(void 0, void 0, void 0, function* () {
19
+ var _a, _b, _c, _d;
20
+ if (!tableData) {
21
+ return;
22
+ }
23
+ const sheetsXmlString = yield ((_a = zip.file(constants_1.sheetsXmlPath)) === null || _a === void 0 ? void 0 : _a.async(constants_1.textResultType));
24
+ if (sheetsXmlString === undefined) {
25
+ throw new Error(constants_1.sheetsNotFoundErr);
26
+ }
27
+ const newSheet = yield updateSheetsInitialData(sheetsXmlString, tableData);
28
+ zip.file(constants_1.sheetsXmlPath, newSheet);
29
+ if (updateQueryTable) {
30
+ const queryTableXmlString = yield ((_b = zip.file(constants_1.queryTableXmlPath)) === null || _b === void 0 ? void 0 : _b.async(constants_1.textResultType));
31
+ if (queryTableXmlString === undefined) {
32
+ throw new Error(constants_1.queryTableNotFoundErr);
33
+ }
34
+ const newQueryTable = yield updateQueryTablesInitialData(queryTableXmlString, tableData);
35
+ zip.file(constants_1.queryTableXmlPath, newQueryTable);
36
+ // update defined name
37
+ const workbookXmlString = yield ((_c = zip.file(constants_1.workbookXmlPath)) === null || _c === void 0 ? void 0 : _c.async(constants_1.textResultType));
38
+ if (workbookXmlString === undefined) {
39
+ throw new Error(constants_1.sheetsNotFoundErr);
40
+ }
41
+ const newWorkbook = yield updateWorkbookInitialData(workbookXmlString, tableData);
42
+ zip.file(constants_1.workbookXmlPath, newWorkbook);
43
+ }
44
+ const tableXmlString = yield ((_d = zip.file(constants_1.tableXmlPath)) === null || _d === void 0 ? void 0 : _d.async(constants_1.textResultType));
45
+ if (tableXmlString === undefined) {
46
+ throw new Error(constants_1.tableNotFoundErr);
47
+ }
48
+ const newTable = yield updateTablesInitialData(tableXmlString, tableData, updateQueryTable);
49
+ zip.file(constants_1.tableXmlPath, newTable);
50
+ });
51
+ const updateTablesInitialData = (tableXmlString, tableData, updateQueryTable = false) => __awaiter(void 0, void 0, void 0, function* () {
52
+ const parser = new DOMParser();
53
+ const serializer = new XMLSerializer();
54
+ const tableDoc = parser.parseFromString(tableXmlString, constants_1.xmlTextResultType);
55
+ const tableColumns = tableDoc.getElementsByTagName(constants_1.element.tableColumns)[0];
56
+ tableColumns.textContent = "";
57
+ tableData.columnNames.forEach((column, columnIndex) => {
58
+ const tableColumn = tableDoc.createElementNS(tableDoc.documentElement.namespaceURI, constants_1.element.tableColumn);
59
+ tableColumn.setAttribute(constants_1.elementAttributes.id, (columnIndex + 1).toString());
60
+ tableColumn.setAttribute(constants_1.elementAttributes.name, column);
61
+ tableColumns.appendChild(tableColumn);
62
+ tableColumn.setAttribute(constants_1.elementAttributes.xr3uid, "{" + (0, uuid_1.v4)().toUpperCase() + "}");
63
+ if (updateQueryTable) {
64
+ tableColumn.setAttribute(constants_1.elementAttributes.uniqueName, (columnIndex + 1).toString());
65
+ tableColumn.setAttribute(constants_1.elementAttributes.queryTableFieldId, (columnIndex + 1).toString());
66
+ }
67
+ });
68
+ tableColumns.setAttribute(constants_1.elementAttributes.count, tableData.columnNames.length.toString());
69
+ tableDoc
70
+ .getElementsByTagName(constants_1.element.table)[0]
71
+ .setAttribute(constants_1.elementAttributes.reference, `A1:${documentUtils_1.default.getCellReferenceRelative(tableData.columnNames.length - 1, tableData.rows.length + 1)}`);
72
+ tableDoc
73
+ .getElementsByTagName(constants_1.element.autoFilter)[0]
74
+ .setAttribute(constants_1.elementAttributes.reference, `A1:${documentUtils_1.default.getCellReferenceRelative(tableData.columnNames.length - 1, tableData.rows.length + 1)}`);
75
+ return serializer.serializeToString(tableDoc);
76
+ });
77
+ const updateWorkbookInitialData = (workbookXmlString, tableData) => __awaiter(void 0, void 0, void 0, function* () {
78
+ const newParser = new DOMParser();
79
+ const newSerializer = new XMLSerializer();
80
+ const workbookDoc = newParser.parseFromString(workbookXmlString, constants_1.xmlTextResultType);
81
+ const definedName = workbookDoc.getElementsByTagName(constants_1.element.definedName)[0];
82
+ definedName.textContent =
83
+ constants_1.defaults.sheetName +
84
+ `!$A$1:${documentUtils_1.default.getCellReferenceAbsolute(tableData.columnNames.length - 1, tableData.rows.length + 1)}`;
85
+ return newSerializer.serializeToString(workbookDoc);
86
+ });
87
+ const updateQueryTablesInitialData = (queryTableXmlString, tableData) => __awaiter(void 0, void 0, void 0, function* () {
88
+ const parser = new DOMParser();
89
+ const serializer = new XMLSerializer();
90
+ const queryTableDoc = parser.parseFromString(queryTableXmlString, constants_1.xmlTextResultType);
91
+ const queryTableFields = queryTableDoc.getElementsByTagName(constants_1.element.queryTableFields)[0];
92
+ queryTableFields.textContent = "";
93
+ tableData.columnNames.forEach((column, columnIndex) => {
94
+ const queryTableField = queryTableDoc.createElementNS(queryTableDoc.documentElement.namespaceURI, constants_1.element.queryTableField);
95
+ queryTableField.setAttribute(constants_1.elementAttributes.id, (columnIndex + 1).toString());
96
+ queryTableField.setAttribute(constants_1.elementAttributes.name, column);
97
+ queryTableField.setAttribute(constants_1.elementAttributes.tableColumnId, (columnIndex + 1).toString());
98
+ queryTableFields.appendChild(queryTableField);
99
+ });
100
+ queryTableFields.setAttribute(constants_1.elementAttributes.count, tableData.columnNames.length.toString());
101
+ queryTableDoc
102
+ .getElementsByTagName(constants_1.element.queryTableRefresh)[0]
103
+ .setAttribute(constants_1.elementAttributes.nextId, (tableData.columnNames.length + 1).toString());
104
+ return serializer.serializeToString(queryTableDoc);
105
+ });
106
+ const updateSheetsInitialData = (sheetsXmlString, tableData) => __awaiter(void 0, void 0, void 0, function* () {
107
+ const parser = new DOMParser();
108
+ const serializer = new XMLSerializer();
109
+ const sheetsDoc = parser.parseFromString(sheetsXmlString, constants_1.xmlTextResultType);
110
+ const sheetData = sheetsDoc.getElementsByTagName(constants_1.element.sheetData)[0];
111
+ sheetData.textContent = "";
112
+ let rowIndex = 0;
113
+ const columnRow = sheetsDoc.createElementNS(sheetsDoc.documentElement.namespaceURI, constants_1.element.row);
114
+ columnRow.setAttribute(constants_1.elementAttributes.row, (rowIndex + 1).toString());
115
+ columnRow.setAttribute(constants_1.elementAttributes.spans, "1:" + tableData.columnNames.length);
116
+ columnRow.setAttribute(constants_1.elementAttributes.x14acDyDescent, "0.3");
117
+ tableData.columnNames.forEach((col, colIndex) => {
118
+ columnRow.appendChild(documentUtils_1.default.createCell(sheetsDoc, colIndex, rowIndex, col));
119
+ });
120
+ sheetData.appendChild(columnRow);
121
+ rowIndex++;
122
+ tableData.rows.forEach((row) => {
123
+ const newRow = sheetsDoc.createElementNS(sheetsDoc.documentElement.namespaceURI, constants_1.element.row);
124
+ newRow.setAttribute(constants_1.elementAttributes.row, (rowIndex + 1).toString());
125
+ newRow.setAttribute(constants_1.elementAttributes.spans, "1:" + row.length);
126
+ newRow.setAttribute(constants_1.elementAttributes.x14acDyDescent, "0.3");
127
+ row.forEach((cellContent, colIndex) => {
128
+ newRow.appendChild(documentUtils_1.default.createCell(sheetsDoc, colIndex, rowIndex, cellContent));
129
+ });
130
+ sheetData.appendChild(newRow);
131
+ rowIndex++;
132
+ });
133
+ sheetsDoc
134
+ .getElementsByTagName(constants_1.element.dimension)[0]
135
+ .setAttribute(constants_1.elementAttributes.reference, documentUtils_1.default.getTableReference(tableData.rows[0].length - 1, tableData.rows.length));
136
+ return serializer.serializeToString(sheetsDoc);
137
+ });
138
+ exports.default = {
139
+ updateTableInitialDataIfNeeded,
140
+ updateSheetsInitialData,
141
+ updateWorkbookInitialData,
142
+ updateTablesInitialData,
143
+ updateQueryTablesInitialData,
144
+ };