@microsoft/connected-workbooks 3.2.2-beta → 3.3.1-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.
@@ -2,8 +2,8 @@
2
2
  // Copyright (c) Microsoft Corporation.
3
3
  // Licensed under the MIT license.
4
4
  Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.maxQueryLength = exports.divider = exports.section1PathPrefix = exports.emptyValue = exports.falseValue = exports.trueValue = exports.pivotCachesPathPrefix = exports.xmlTextResultType = exports.textResultType = exports.application = exports.uint8ArrayType = exports.blobFileType = exports.columnIndexOutOfRangeErr = exports.relsNotFoundErr = exports.arrayIsntMxNErr = exports.unexpectedErr = exports.promotedHeadersCannotBeUsedWithoutAdjustingColumnNamesErr = exports.InvalidColumnNameErr = exports.stylesNotFoundErr = exports.EmptyQueryNameErr = exports.QueryNameInvalidCharsErr = exports.QueryNameMaxLengthErr = exports.invalidDataTypeErr = exports.headerNotFoundErr = exports.invalidValueInColumnErr = exports.tableNotFoundErr = exports.queryTableNotFoundErr = exports.templateWithInitialDataErr = exports.formulaSectionNotFoundErr = exports.queryConnectionNotFoundErr = exports.queryAndPivotTableNotFoundErr = exports.queryNameNotFoundErr = exports.emptyQueryMashupErr = exports.base64NotFoundErr = exports.sheetsNotFoundErr = exports.connectionsNotFoundErr = exports.sharedStringsNotFoundErr = exports.docPropsRootElement = exports.docMetadataXmlPath = exports.relsXmlPath = exports.docPropsCoreXmlPath = exports.section1mPath = exports.pivotCachesPath = exports.queryTablesPath = exports.workbookXmlPath = exports.queryTableXmlPath = exports.tableXmlPath = exports.sheetsXmlPath = exports.sharedStringsXmlPath = exports.connectionsXmlPath = void 0;
6
- exports.OFU = exports.headers = exports.URLS = exports.defaults = exports.elementAttributesValues = exports.dataTypeKind = exports.elementAttributes = exports.element = exports.BOM = exports.falseStr = exports.trueStr = void 0;
5
+ exports.trueValue = exports.pivotCachesPathPrefix = exports.xmlTextResultType = exports.textResultType = exports.application = exports.uint8ArrayType = exports.blobFileType = exports.columnIndexOutOfRangeErr = exports.xlRelsNotFoundErr = exports.relsNotFoundErr = exports.arrayIsntMxNErr = exports.unexpectedErr = exports.promotedHeadersCannotBeUsedWithoutAdjustingColumnNamesErr = exports.InvalidColumnNameErr = exports.stylesNotFoundErr = exports.EmptyQueryNameErr = exports.QueryNameInvalidCharsErr = exports.QueryNameMaxLengthErr = exports.invalidDataTypeErr = exports.headerNotFoundErr = exports.invalidValueInColumnErr = exports.tableReferenceNotFoundErr = exports.tableNotFoundErr = exports.queryTableNotFoundErr = exports.templateWithInitialDataErr = exports.formulaSectionNotFoundErr = exports.queryConnectionNotFoundErr = exports.queryAndPivotTableNotFoundErr = exports.queryNameNotFoundErr = exports.emptyQueryMashupErr = exports.base64NotFoundErr = exports.sheetsNotFoundErr = exports.WorkbookNotFoundERR = exports.connectionsNotFoundErr = exports.sharedStringsNotFoundErr = exports.workbookRelsXmlPath = exports.docPropsRootElement = exports.docMetadataXmlPath = exports.relsXmlPath = exports.docPropsCoreXmlPath = exports.section1mPath = exports.pivotCachesPath = exports.tablesFolderPath = exports.queryTablesPath = exports.workbookXmlPath = exports.queryTableXmlPath = exports.tableXmlPath = exports.sheetsXmlPath = exports.sharedStringsXmlPath = exports.connectionsXmlPath = void 0;
6
+ exports.OFU = exports.headers = exports.URLS = exports.defaults = exports.elementAttributesValues = exports.dataTypeKind = exports.elementAttributes = exports.element = exports.BOM = exports.falseStr = exports.trueStr = exports.maxQueryLength = exports.divider = exports.section1PathPrefix = exports.emptyValue = exports.falseValue = void 0;
7
7
  exports.connectionsXmlPath = "xl/connections.xml";
8
8
  exports.sharedStringsXmlPath = "xl/sharedStrings.xml";
9
9
  exports.sheetsXmlPath = "xl/worksheets/sheet1.xml";
@@ -11,14 +11,17 @@ exports.tableXmlPath = "xl/tables/table1.xml";
11
11
  exports.queryTableXmlPath = "xl/queryTables/queryTable1.xml";
12
12
  exports.workbookXmlPath = "xl/workbook.xml";
13
13
  exports.queryTablesPath = "xl/queryTables/";
14
+ exports.tablesFolderPath = "xl/tables/";
14
15
  exports.pivotCachesPath = "xl/pivotCache/";
15
16
  exports.section1mPath = "Formulas/Section1.m";
16
17
  exports.docPropsCoreXmlPath = "docProps/core.xml";
17
18
  exports.relsXmlPath = "_rels/.rels";
18
19
  exports.docMetadataXmlPath = "docMetadata";
19
20
  exports.docPropsRootElement = "cp:coreProperties";
21
+ exports.workbookRelsXmlPath = "xl/_rels/workbook.xml.rels";
20
22
  exports.sharedStringsNotFoundErr = "SharedStrings were not found in template";
21
23
  exports.connectionsNotFoundErr = "Connections were not found in template";
24
+ exports.WorkbookNotFoundERR = "workbook was not found in template";
22
25
  exports.sheetsNotFoundErr = "Sheets were not found in template";
23
26
  exports.base64NotFoundErr = "Base64 was not found in template";
24
27
  exports.emptyQueryMashupErr = "Query mashup is empty";
@@ -29,6 +32,7 @@ exports.formulaSectionNotFoundErr = "Formula section wasn't found in template";
29
32
  exports.templateWithInitialDataErr = "Cannot use a template file with initial data";
30
33
  exports.queryTableNotFoundErr = "Query table wasn't found in template";
31
34
  exports.tableNotFoundErr = "Table wasn't found in template";
35
+ exports.tableReferenceNotFoundErr = "Reference not found in the table XML.";
32
36
  exports.invalidValueInColumnErr = "Invalid cell value in column";
33
37
  exports.headerNotFoundErr = "Invalid JSON file, header is missing";
34
38
  exports.invalidDataTypeErr = "Invalid JSON file, invalid data type";
@@ -41,6 +45,7 @@ exports.promotedHeadersCannotBeUsedWithoutAdjustingColumnNamesErr = "Headers can
41
45
  exports.unexpectedErr = "Unexpected error";
42
46
  exports.arrayIsntMxNErr = "Array isn't MxN";
43
47
  exports.relsNotFoundErr = ".rels were not found in template";
48
+ exports.xlRelsNotFoundErr = "workbook.xml.rels were not found xl";
44
49
  exports.columnIndexOutOfRangeErr = "Column index out of range";
45
50
  exports.blobFileType = "blob";
46
51
  exports.uint8ArrayType = "uint8array";
@@ -85,6 +90,7 @@ exports.element = {
85
90
  dimension: "dimension",
86
91
  selection: "selection",
87
92
  kindCell: "c",
93
+ sheet: "sheet",
88
94
  };
89
95
  exports.elementAttributes = {
90
96
  connection: "connection",
@@ -98,6 +104,7 @@ exports.elementAttributes = {
98
104
  name: "name",
99
105
  description: "description",
100
106
  id: "id",
107
+ relationId: "r:id",
101
108
  type: "Type",
102
109
  value: "Value",
103
110
  relationshipInfo: "RelationshipInfoContainer",
@@ -117,6 +124,7 @@ exports.elementAttributes = {
117
124
  x14acDyDescent: "x14ac:dyDescent",
118
125
  xr3uid: "xr3:uid",
119
126
  space: "xml:space",
127
+ target: "Target",
120
128
  };
121
129
  exports.dataTypeKind = {
122
130
  string: "str",
@@ -134,6 +142,7 @@ exports.defaults = {
134
142
  queryName: "Query1",
135
143
  sheetName: "Sheet1",
136
144
  columnName: "Column",
145
+ tableName: "Table1",
137
146
  };
138
147
  exports.URLS = {
139
148
  PQ: [
@@ -93,13 +93,27 @@ var convertToExcelColumn = function (index) {
93
93
  var base = 26; // number of letters in the alphabet
94
94
  while (index >= 0) {
95
95
  var remainder = index % base;
96
- columnStr = String.fromCharCode(remainder + 65) + columnStr; // ASCII 'A' is 65
96
+ columnStr = String.fromCharCode(remainder + 'A'.charCodeAt(0)) + columnStr;
97
97
  index = Math.floor(index / base) - 1;
98
98
  }
99
99
  return columnStr;
100
100
  };
101
- var getTableReference = function (numberOfCols, numberOfRows) {
102
- return "A1:".concat(getCellReferenceRelative(numberOfCols, numberOfRows));
101
+ /**
102
+ * Parse an Excel range (e.g. "B2:D10") and return its starting row and column indices.
103
+ * @param cellRangeRef - Range reference string.
104
+ * @returns Object with numeric row and column.
105
+ */
106
+ var GetStartPosition = function (cellRangeRef) {
107
+ var match = cellRangeRef.toUpperCase().match(/^([A-Z]+)(\d+):/);
108
+ if (!match) {
109
+ return { row: 0, column: 0 };
110
+ }
111
+ var colLetters = match[1], rowStr = match[2];
112
+ var row = parseInt(rowStr, 10);
113
+ var column = colLetters
114
+ .split("")
115
+ .reduce(function (acc, char) { return acc * 26 + (char.charCodeAt(0) - "A".charCodeAt(0) + 1); }, 0);
116
+ return { row: row, column: column };
103
117
  };
104
118
  var createCellElement = function (doc, colIndex, rowIndex, data) {
105
119
  var cell = doc.createElementNS(doc.documentElement.namespaceURI, constants_1.element.kindCell);
@@ -146,8 +160,8 @@ exports.default = {
146
160
  getCellReferenceRelative: getCellReferenceRelative,
147
161
  getCellReferenceAbsolute: getCellReferenceAbsolute,
148
162
  createCell: createCellElement,
149
- getTableReference: getTableReference,
150
163
  updateCellData: updateCellData,
151
164
  resolveType: resolveType,
152
165
  convertToExcelColumn: convertToExcelColumn,
166
+ GetStartPosition: GetStartPosition,
153
167
  };
@@ -45,7 +45,17 @@ var constants_1 = require("./constants");
45
45
  var documentUtils_1 = __importDefault(require("./documentUtils"));
46
46
  var uuid_1 = require("uuid");
47
47
  var xmldom_qsa_1 = require("xmldom-qsa");
48
- var updateTableInitialDataIfNeeded = function (zip, tableData, updateQueryTable) { return __awaiter(void 0, void 0, void 0, function () {
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 () {
49
59
  var sheetsXmlString, newSheet, queryTableXmlString, newQueryTable, workbookXmlString, newWorkbook, tableXmlString, newTable;
50
60
  var _a, _b, _c, _d;
51
61
  return __generator(this, function (_e) {
@@ -54,14 +64,14 @@ var updateTableInitialDataIfNeeded = function (zip, tableData, updateQueryTable)
54
64
  if (!tableData) {
55
65
  return [2 /*return*/];
56
66
  }
57
- return [4 /*yield*/, ((_a = zip.file(constants_1.sheetsXmlPath)) === null || _a === void 0 ? void 0 : _a.async(constants_1.textResultType))];
67
+ return [4 /*yield*/, ((_a = zip.file(sheetPath)) === null || _a === void 0 ? void 0 : _a.async(constants_1.textResultType))];
58
68
  case 1:
59
69
  sheetsXmlString = _e.sent();
60
70
  if (sheetsXmlString === undefined) {
61
71
  throw new Error(constants_1.sheetsNotFoundErr);
62
72
  }
63
- newSheet = updateSheetsInitialData(sheetsXmlString, tableData);
64
- zip.file(constants_1.sheetsXmlPath, newSheet);
73
+ newSheet = updateSheetsInitialData(sheetsXmlString, tableData, cellRangeRef);
74
+ zip.file(sheetPath, newSheet);
65
75
  if (!updateQueryTable) return [3 /*break*/, 5];
66
76
  return [4 /*yield*/, ((_b = zip.file(constants_1.queryTableXmlPath)) === null || _b === void 0 ? void 0 : _b.async(constants_1.textResultType))];
67
77
  case 2:
@@ -79,22 +89,30 @@ var updateTableInitialDataIfNeeded = function (zip, tableData, updateQueryTable)
79
89
  if (workbookXmlString === undefined) {
80
90
  throw new Error(constants_1.sheetsNotFoundErr);
81
91
  }
82
- newWorkbook = updateWorkbookInitialData(workbookXmlString, tableData);
92
+ newWorkbook = updateWorkbookInitialData(workbookXmlString, sheetName + GenerateReferenceFromString(cellRangeRef));
83
93
  zip.file(constants_1.workbookXmlPath, newWorkbook);
84
94
  _e.label = 5;
85
- case 5: return [4 /*yield*/, ((_d = zip.file(constants_1.tableXmlPath)) === null || _d === void 0 ? void 0 : _d.async(constants_1.textResultType))];
95
+ case 5: return [4 /*yield*/, ((_d = zip.file(tablePath)) === null || _d === void 0 ? void 0 : _d.async(constants_1.textResultType))];
86
96
  case 6:
87
97
  tableXmlString = _e.sent();
88
98
  if (tableXmlString === undefined) {
89
99
  throw new Error(constants_1.tableNotFoundErr);
90
100
  }
91
- newTable = updateTablesInitialData(tableXmlString, tableData, updateQueryTable);
92
- zip.file(constants_1.tableXmlPath, newTable);
101
+ newTable = updateTablesInitialData(tableXmlString, tableData, cellRangeRef, updateQueryTable);
102
+ zip.file(tablePath, newTable);
93
103
  return [2 /*return*/];
94
104
  }
95
105
  });
96
106
  }); };
97
- var updateTablesInitialData = function (tableXmlString, tableData, updateQueryTable) {
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) {
98
116
  if (updateQueryTable === void 0) { updateQueryTable = false; }
99
117
  var parser = new xmldom_qsa_1.DOMParser();
100
118
  var serializer = new xmldom_qsa_1.XMLSerializer();
@@ -115,19 +133,24 @@ var updateTablesInitialData = function (tableXmlString, tableData, updateQueryTa
115
133
  tableColumns.setAttribute(constants_1.elementAttributes.count, tableData.columnNames.length.toString());
116
134
  tableDoc
117
135
  .getElementsByTagName(constants_1.element.table)[0]
118
- .setAttribute(constants_1.elementAttributes.reference, "A1:".concat(documentUtils_1.default.getCellReferenceRelative(tableData.columnNames.length - 1, tableData.rows.length + 1)));
136
+ .setAttribute(constants_1.elementAttributes.reference, cellRangeRef);
119
137
  tableDoc
120
138
  .getElementsByTagName(constants_1.element.autoFilter)[0]
121
- .setAttribute(constants_1.elementAttributes.reference, "A1:".concat(documentUtils_1.default.getCellReferenceRelative(tableData.columnNames.length - 1, tableData.rows.length + 1)));
139
+ .setAttribute(constants_1.elementAttributes.reference, cellRangeRef);
122
140
  return serializer.serializeToString(tableDoc);
123
141
  };
124
- var updateWorkbookInitialData = function (workbookXmlString, tableData) {
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) {
125
149
  var newParser = new xmldom_qsa_1.DOMParser();
126
150
  var newSerializer = new xmldom_qsa_1.XMLSerializer();
127
151
  var workbookDoc = newParser.parseFromString(workbookXmlString, constants_1.xmlTextResultType);
128
152
  var definedName = workbookDoc.getElementsByTagName(constants_1.element.definedName)[0];
129
- definedName.textContent =
130
- constants_1.defaults.sheetName + "!$A$1:".concat(documentUtils_1.default.getCellReferenceAbsolute(tableData.columnNames.length - 1, tableData.rows.length + 1));
153
+ definedName.textContent = customDefinedName;
131
154
  return newSerializer.serializeToString(workbookDoc);
132
155
  };
133
156
  var updateQueryTablesInitialData = function (queryTableXmlString, tableData) {
@@ -147,42 +170,64 @@ var updateQueryTablesInitialData = function (queryTableXmlString, tableData) {
147
170
  queryTableDoc.getElementsByTagName(constants_1.element.queryTableRefresh)[0].setAttribute(constants_1.elementAttributes.nextId, (tableData.columnNames.length + 1).toString());
148
171
  return serializer.serializeToString(queryTableDoc);
149
172
  };
150
- var updateSheetsInitialData = function (sheetsXmlString, tableData) {
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;
151
182
  var parser = new xmldom_qsa_1.DOMParser();
152
183
  var serializer = new xmldom_qsa_1.XMLSerializer();
153
184
  var sheetsDoc = parser.parseFromString(sheetsXmlString, constants_1.xmlTextResultType);
154
185
  var sheetData = sheetsDoc.getElementsByTagName(constants_1.element.sheetData)[0];
155
186
  sheetData.textContent = "";
156
- var rowIndex = 0;
157
187
  var columnRow = sheetsDoc.createElementNS(sheetsDoc.documentElement.namespaceURI, constants_1.element.row);
158
- columnRow.setAttribute(constants_1.elementAttributes.row, (rowIndex + 1).toString());
159
- columnRow.setAttribute(constants_1.elementAttributes.spans, "1:" + tableData.columnNames.length);
188
+ columnRow.setAttribute(constants_1.elementAttributes.row, row.toString());
189
+ columnRow.setAttribute(constants_1.elementAttributes.spans, column + ":" + (column + tableData.columnNames.length - 1));
160
190
  columnRow.setAttribute(constants_1.elementAttributes.x14acDyDescent, "0.3");
161
191
  tableData.columnNames.forEach(function (col, colIndex) {
162
- columnRow.appendChild(documentUtils_1.default.createCell(sheetsDoc, colIndex, rowIndex, col));
192
+ columnRow.appendChild(documentUtils_1.default.createCell(sheetsDoc, colIndex + column - 1, row - 1, col));
163
193
  });
164
194
  sheetData.appendChild(columnRow);
165
- rowIndex++;
166
- tableData.rows.forEach(function (row) {
195
+ row++;
196
+ tableData.rows.forEach(function (_row) {
167
197
  var newRow = sheetsDoc.createElementNS(sheetsDoc.documentElement.namespaceURI, constants_1.element.row);
168
- newRow.setAttribute(constants_1.elementAttributes.row, (rowIndex + 1).toString());
169
- newRow.setAttribute(constants_1.elementAttributes.spans, "1:" + row.length);
198
+ newRow.setAttribute(constants_1.elementAttributes.row, row.toString());
199
+ newRow.setAttribute(constants_1.elementAttributes.spans, column + ":" + (column + tableData.columnNames.length - 1));
170
200
  newRow.setAttribute(constants_1.elementAttributes.x14acDyDescent, "0.3");
171
- row.forEach(function (cellContent, colIndex) {
172
- newRow.appendChild(documentUtils_1.default.createCell(sheetsDoc, colIndex, rowIndex, cellContent));
201
+ _row.forEach(function (cellContent, colIndex) {
202
+ newRow.appendChild(documentUtils_1.default.createCell(sheetsDoc, colIndex + column - 1, row - 1, cellContent));
173
203
  });
174
204
  sheetData.appendChild(newRow);
175
- rowIndex++;
205
+ row++;
176
206
  });
177
- var reference = documentUtils_1.default.getTableReference(tableData.rows[0].length - 1, tableData.rows.length + 1);
178
- sheetsDoc.getElementsByTagName(constants_1.element.dimension)[0].setAttribute(constants_1.elementAttributes.reference, reference);
179
- sheetsDoc.getElementsByTagName(constants_1.element.selection)[0].setAttribute(constants_1.elementAttributes.sqref, reference);
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);
180
209
  return serializer.serializeToString(sheetsDoc);
181
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
+ };
182
226
  exports.default = {
183
227
  updateTableInitialDataIfNeeded: updateTableInitialDataIfNeeded,
184
228
  updateSheetsInitialData: updateSheetsInitialData,
185
229
  updateWorkbookInitialData: updateWorkbookInitialData,
186
230
  updateTablesInitialData: updateTablesInitialData,
187
231
  updateQueryTablesInitialData: updateQueryTablesInitialData,
232
+ GenerateReferenceFromString: GenerateReferenceFromString,
188
233
  };
@@ -277,6 +277,114 @@ var updatePivotTable = function (tableXmlString, connectionId, refreshOnOpen) {
277
277
  }
278
278
  return { isPivotTableUpdated: isPivotTableUpdated, newPivotTable: newPivotTable };
279
279
  };
280
+ /**
281
+ * Retrieves the target path of a sheet from workbook relationships by its relationship ID.
282
+ */
283
+ function getSheetPathFromXlRelId(zip, rId) {
284
+ return __awaiter(this, void 0, void 0, function () {
285
+ var relsFile, relsString, relsDoc, relationship, target;
286
+ return __generator(this, function (_a) {
287
+ switch (_a.label) {
288
+ case 0:
289
+ relsFile = zip.file(constants_1.workbookRelsXmlPath);
290
+ if (!relsFile) {
291
+ throw new Error(constants_1.xlRelsNotFoundErr);
292
+ }
293
+ return [4 /*yield*/, relsFile.async(constants_1.textResultType)];
294
+ case 1:
295
+ relsString = _a.sent();
296
+ relsDoc = new xmldom_qsa_1.DOMParser().parseFromString(relsString, constants_1.xmlTextResultType);
297
+ relationship = relsDoc.querySelector("Relationship[Id=\"".concat(rId, "\"]"));
298
+ if (!relationship) {
299
+ throw new Error("Relationship not found for Id: ".concat(rId));
300
+ }
301
+ target = relationship.getAttribute(constants_1.elementAttributes.target);
302
+ if (!target) {
303
+ throw new Error("Target not found for Relationship Id: ".concat(rId));
304
+ }
305
+ return [2 /*return*/, target];
306
+ }
307
+ });
308
+ });
309
+ }
310
+ // get sheet name from workbook
311
+ var getSheetPathByNameFromZip = function (zip, sheetName) { return __awaiter(void 0, void 0, void 0, function () {
312
+ var workbookXmlString, parser, doc, sheetElements, i, rId;
313
+ var _a;
314
+ return __generator(this, function (_b) {
315
+ switch (_b.label) {
316
+ case 0: return [4 /*yield*/, ((_a = zip.file(constants_1.workbookXmlPath)) === null || _a === void 0 ? void 0 : _a.async("text"))];
317
+ case 1:
318
+ workbookXmlString = _b.sent();
319
+ if (!workbookXmlString) {
320
+ throw new Error(constants_1.WorkbookNotFoundERR);
321
+ }
322
+ parser = new xmldom_qsa_1.DOMParser();
323
+ doc = parser.parseFromString(workbookXmlString, constants_1.xmlTextResultType);
324
+ sheetElements = doc.getElementsByTagName(constants_1.element.sheet);
325
+ for (i = 0; i < sheetElements.length; i++) {
326
+ if (sheetElements[i].getAttribute(constants_1.elementAttributes.name) === sheetName) {
327
+ rId = sheetElements[i].getAttribute(constants_1.elementAttributes.relationId);
328
+ if (rId) {
329
+ return [2 /*return*/, getSheetPathFromXlRelId(zip, rId)];
330
+ }
331
+ }
332
+ }
333
+ throw new Error("Sheet with name ".concat(sheetName, " not found"));
334
+ }
335
+ });
336
+ }); };
337
+ // get definedName
338
+ var getReferenceFromTable = function (zip, tablePath) { return __awaiter(void 0, void 0, void 0, function () {
339
+ var tableXmlString, parser, doc, tableElements, reference;
340
+ var _a, _b;
341
+ return __generator(this, function (_c) {
342
+ switch (_c.label) {
343
+ case 0: return [4 /*yield*/, ((_a = zip.file(tablePath)) === null || _a === void 0 ? void 0 : _a.async("text"))];
344
+ case 1:
345
+ tableXmlString = _c.sent();
346
+ if (!tableXmlString) {
347
+ throw new Error(constants_1.WorkbookNotFoundERR);
348
+ }
349
+ parser = new xmldom_qsa_1.DOMParser();
350
+ doc = parser.parseFromString(tableXmlString, constants_1.xmlTextResultType);
351
+ tableElements = doc.getElementsByTagName(constants_1.element.table);
352
+ reference = (_b = tableElements[0]) === null || _b === void 0 ? void 0 : _b.getAttribute(constants_1.elementAttributes.reference);
353
+ if (!reference) {
354
+ throw new Error(constants_1.tableReferenceNotFoundErr);
355
+ }
356
+ return [2 /*return*/, reference.split(":")[0]]; // Return the start cell reference (e.g., "A1" from "A1:B10")
357
+ }
358
+ });
359
+ }); };
360
+ var findTablePathFromZip = function (zip, targetTableName) { return __awaiter(void 0, void 0, void 0, function () {
361
+ var tablesFolder, tableFilePromises, tableFiles, parser, _i, tableFiles_1, _a, path, content, doc, tableElem;
362
+ return __generator(this, function (_b) {
363
+ switch (_b.label) {
364
+ case 0:
365
+ tablesFolder = zip.folder("xl/tables");
366
+ if (!tablesFolder)
367
+ return [2 /*return*/, ""];
368
+ tableFilePromises = [];
369
+ tablesFolder.forEach(function (relativePath, file) {
370
+ tableFilePromises.push(file.async(constants_1.textResultType).then(function (content) { return ({ path: relativePath, content: content }); }));
371
+ });
372
+ return [4 /*yield*/, Promise.all(tableFilePromises)];
373
+ case 1:
374
+ tableFiles = _b.sent();
375
+ parser = new xmldom_qsa_1.DOMParser();
376
+ for (_i = 0, tableFiles_1 = tableFiles; _i < tableFiles_1.length; _i++) {
377
+ _a = tableFiles_1[_i], path = _a.path, content = _a.content;
378
+ doc = parser.parseFromString(content, constants_1.xmlTextResultType);
379
+ tableElem = doc.getElementsByTagName(constants_1.element.table)[0];
380
+ if (tableElem && tableElem.getAttribute(constants_1.elementAttributes.name) === targetTableName) {
381
+ return [2 /*return*/, path];
382
+ }
383
+ }
384
+ throw new Error(constants_1.tableNotFoundErr);
385
+ }
386
+ });
387
+ }); };
280
388
  exports.default = {
281
389
  updateDocProps: updateDocProps,
282
390
  clearLabelInfo: clearLabelInfo,
@@ -286,4 +394,7 @@ exports.default = {
286
394
  updatePivotTablesandQueryTables: updatePivotTablesandQueryTables,
287
395
  updateQueryTable: updateQueryTable,
288
396
  updatePivotTable: updatePivotTable,
397
+ getSheetPathByNameFromZip: getSheetPathByNameFromZip,
398
+ getReferenceFromTable: getReferenceFromTable,
399
+ findTablePathFromZip: findTablePathFromZip,
289
400
  };
@@ -44,26 +44,59 @@ Object.defineProperty(exports, "__esModule", { value: true });
44
44
  var constants_1 = require("./constants");
45
45
  var mashupDocumentParser_1 = require("./mashupDocumentParser");
46
46
  var pqUtils_1 = __importDefault(require("./pqUtils"));
47
- var xmlInnerPartsUtils_1 = __importDefault(require("./xmlInnerPartsUtils"));
48
47
  var tableUtils_1 = __importDefault(require("./tableUtils"));
48
+ var xmlInnerPartsUtils_1 = __importDefault(require("./xmlInnerPartsUtils"));
49
+ var documentUtils_1 = __importDefault(require("./documentUtils"));
49
50
  var updateWorkbookDataAndConfigurations = function (zip, fileConfigs, tableData, updateQueryTable) {
50
51
  if (updateQueryTable === void 0) { updateQueryTable = false; }
51
52
  return __awaiter(void 0, void 0, void 0, function () {
52
- return __generator(this, function (_a) {
53
- switch (_a.label) {
54
- case 0: return [4 /*yield*/, xmlInnerPartsUtils_1.default.updateDocProps(zip, fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.docProps)];
53
+ var sheetName, tablePath, sheetPath, templateSettings, sheetLocation, _a, cellRangeRef;
54
+ return __generator(this, function (_b) {
55
+ switch (_b.label) {
56
+ case 0:
57
+ sheetName = constants_1.defaults.sheetName;
58
+ tablePath = constants_1.tableXmlPath;
59
+ sheetPath = constants_1.sheetsXmlPath;
60
+ if (!((fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateFile) !== undefined)) return [3 /*break*/, 4];
61
+ templateSettings = fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateSettings;
62
+ if (!((templateSettings === null || templateSettings === void 0 ? void 0 : templateSettings.sheetName) !== undefined)) return [3 /*break*/, 2];
63
+ return [4 /*yield*/, xmlInnerPartsUtils_1.default.getSheetPathByNameFromZip(zip, templateSettings.sheetName)];
55
64
  case 1:
56
- _a.sent();
57
- if (!((fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateFile) === undefined)) return [3 /*break*/, 3];
65
+ sheetLocation = _b.sent();
66
+ sheetName = templateSettings.sheetName;
67
+ sheetPath = "xl/" + sheetLocation;
68
+ _b.label = 2;
69
+ case 2:
70
+ if (!((templateSettings === null || templateSettings === void 0 ? void 0 : templateSettings.tableName) !== undefined)) return [3 /*break*/, 4];
71
+ _a = constants_1.tablesFolderPath;
72
+ return [4 /*yield*/, xmlInnerPartsUtils_1.default.findTablePathFromZip(zip, templateSettings === null || templateSettings === void 0 ? void 0 : templateSettings.tableName)];
73
+ case 3:
74
+ tablePath = _a + (_b.sent());
75
+ _b.label = 4;
76
+ case 4:
77
+ cellRangeRef = "A1";
78
+ if (!((fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateFile) != null)) return [3 /*break*/, 6];
79
+ return [4 /*yield*/, xmlInnerPartsUtils_1.default.getReferenceFromTable(zip, tablePath)];
80
+ case 5:
81
+ cellRangeRef = _b.sent();
82
+ _b.label = 6;
83
+ case 6:
84
+ if (tableData) {
85
+ cellRangeRef += ":".concat(documentUtils_1.default.getCellReferenceRelative(tableData.columnNames.length - 1, tableData.rows.length + 1));
86
+ }
87
+ return [4 /*yield*/, xmlInnerPartsUtils_1.default.updateDocProps(zip, fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.docProps)];
88
+ case 7:
89
+ _b.sent();
90
+ if (!((fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateFile) === undefined)) return [3 /*break*/, 9];
58
91
  // If we are using our base template, we need to clear label info
59
92
  return [4 /*yield*/, xmlInnerPartsUtils_1.default.clearLabelInfo(zip)];
60
- case 2:
93
+ case 8:
61
94
  // If we are using our base template, we need to clear label info
62
- _a.sent();
63
- _a.label = 3;
64
- case 3: return [4 /*yield*/, tableUtils_1.default.updateTableInitialDataIfNeeded(zip, tableData, updateQueryTable)];
65
- case 4:
66
- _a.sent();
95
+ _b.sent();
96
+ _b.label = 9;
97
+ case 9: return [4 /*yield*/, tableUtils_1.default.updateTableInitialDataIfNeeded(zip, cellRangeRef, sheetPath, tablePath, sheetPath, tableData, updateQueryTable)];
98
+ case 10:
99
+ _b.sent();
67
100
  return [2 /*return*/];
68
101
  }
69
102
  });
@@ -89,8 +122,8 @@ var updateWorkbookPowerQueryDocument = function (zip, queryName, queryMashupDoc)
89
122
  }
90
123
  });
91
124
  }); };
92
- var updateWorkbookSingleQueryAttributes = function (zip, queryName, refreshOnOpen) { return __awaiter(void 0, void 0, void 0, function () {
93
- var connectionsXmlString, _a, connectionId, connectionXmlFileString, sharedStringsXmlString, _b, sharedStringIndex, newSharedStrings, sheetsXmlString, worksheetString;
125
+ var updateWorkbookSingleQueryAttributes = function (zip, queryName, refreshOnOpen, sheetName) { return __awaiter(void 0, void 0, void 0, function () {
126
+ var connectionsXmlString, _a, connectionId, connectionXmlFileString, sharedStringsXmlString, _b, sharedStringIndex, newSharedStrings, sheetPath, sheetLocation, sheetsXmlString, worksheetString;
94
127
  var _c, _d, _e;
95
128
  return __generator(this, function (_f) {
96
129
  switch (_f.label) {
@@ -110,17 +143,24 @@ var updateWorkbookSingleQueryAttributes = function (zip, queryName, refreshOnOpe
110
143
  }
111
144
  _b = xmlInnerPartsUtils_1.default.updateSharedStrings(sharedStringsXmlString, queryName), sharedStringIndex = _b.sharedStringIndex, newSharedStrings = _b.newSharedStrings;
112
145
  zip.file(constants_1.sharedStringsXmlPath, newSharedStrings);
113
- return [4 /*yield*/, ((_e = zip.file(constants_1.sheetsXmlPath)) === null || _e === void 0 ? void 0 : _e.async(constants_1.textResultType))];
146
+ sheetPath = constants_1.sheetsXmlPath;
147
+ if (!(sheetName !== undefined)) return [3 /*break*/, 4];
148
+ return [4 /*yield*/, xmlInnerPartsUtils_1.default.getSheetPathByNameFromZip(zip, sheetName)];
114
149
  case 3:
150
+ sheetLocation = _f.sent();
151
+ sheetPath = "xl/" + sheetLocation;
152
+ _f.label = 4;
153
+ case 4: return [4 /*yield*/, ((_e = zip.file(sheetPath)) === null || _e === void 0 ? void 0 : _e.async(constants_1.textResultType))];
154
+ case 5:
115
155
  sheetsXmlString = _f.sent();
116
156
  if (sheetsXmlString === undefined) {
117
157
  throw new Error(constants_1.sheetsNotFoundErr);
118
158
  }
119
159
  worksheetString = xmlInnerPartsUtils_1.default.updateWorksheet(sheetsXmlString, sharedStringIndex.toString());
120
- zip.file(constants_1.sheetsXmlPath, worksheetString);
160
+ zip.file(sheetPath, worksheetString);
121
161
  // Update tables
122
162
  return [4 /*yield*/, xmlInnerPartsUtils_1.default.updatePivotTablesandQueryTables(zip, queryName, refreshOnOpen, connectionId)];
123
- case 4:
163
+ case 6:
124
164
  // Update tables
125
165
  _f.sent();
126
166
  return [2 /*return*/];