@microsoft/connected-workbooks 2.1.25 → 3.0.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.
package/README.md CHANGED
@@ -51,11 +51,17 @@ workbookManager.downloadWorkbook(blob, "MyTable.xlsx");
51
51
 
52
52
  ### 3. Control Document Properties:
53
53
  ```typescript
54
- const blob = await workbookManager.generateTableWorkbookFromHtml(
55
- document.querySelector('table') as HTMLTableElement,
56
- {createdBy: 'John Doe', lastModifiedBy: 'Jane Doe', description: 'This is a sample table'});
54
+ const blob = await workbookManager.generateTableWorkbookFromHtml(
55
+ document.querySelector('table') as HTMLTableElement, {
56
+ docProps: {
57
+ createdBy: 'John Doe',
58
+ lastModifiedBy: 'Jane Doe',
59
+ description: 'This is a sample table'
60
+ }
61
+ }
62
+ );
57
63
 
58
- workbookManager.downloadWorkbook(blob, "MyTable.xlsx");
64
+ workbookManager.downloadWorkbook(blob, "MyTable.xlsx");
59
65
  ```
60
66
  ![image](https://github.com/microsoft/connected-workbooks/assets/7674478/c267c9eb-6367-419d-832d-5a835c7683f9)
61
67
 
@@ -68,7 +74,9 @@ const blob = await workbookManager.generateSingleQueryWorkbook({
68
74
  Source = {1..10} \
69
75
  in \
70
76
  Source',
71
- refreshOnOpen: true});
77
+ refreshOnOpen: true
78
+ });
79
+
72
80
  workbookManager.downloadWorkbook(blob, "MyConnectedWorkbook.xlsx");
73
81
  ```
74
82
  ![image](https://github.com/microsoft/connected-workbooks/assets/7674478/57bd986c-6309-4963-8d86-911ccf496c3f)
@@ -126,7 +134,7 @@ async function `generateTableWorkbookFromHtml`: `Promise<Blob>`
126
134
  |Parameter | Type | Required | Description |
127
135
  |--- |--- |--- |--- |
128
136
  | htmlTable | HTMLTableElement | __required__ | Initial data loaded to workbook |
129
- | docProps | [DocProps](#docprops) | optional | Custom workbook properties |
137
+ | fileConfigs | [FileConfigs](#fileconfigs) | optional | Custom file configurations |
130
138
 
131
139
  #### 3. Generate a table workbook with raw data
132
140
  ```typescript
@@ -136,7 +144,7 @@ async function `generateTableWorkbookFromGrid`: `Promise<Blob>`
136
144
  |Parameter | Type | Required | Description |
137
145
  |--- |--- |--- |--- |
138
146
  | grid | [Grid](#grid) | __required__ | Initial data loaded to workbook |
139
- | docProps | [DocProps](#docprops) | optional | Custom workbook properties |
147
+ | fileConfigs | [FileConfigs](#fileconfigs) | optional | Custom file configurations |
140
148
  </br>
141
149
 
142
150
  ### Types
@@ -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.element = exports.BOM = exports.falseStr = exports.trueStr = 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.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.docPropsCoreXmlPath = exports.section1mPath = exports.pivotCachesPath = exports.queryTablesPath = exports.workbookXmlPath = exports.queryTableXmlPath = exports.tableXmlPath = exports.sheetsXmlPath = exports.sharedStringsXmlPath = exports.connectionsXmlPath = void 0;
6
- exports.URLS = exports.defaults = exports.elementAttributesValues = exports.dataTypeKind = exports.elementAttributes = void 0;
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.relsNotFoundErr = exports.templateFileNotSupportedErr = 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.URLS = exports.defaults = exports.elementAttributesValues = exports.dataTypeKind = exports.elementAttributes = exports.element = exports.BOM = exports.falseStr = exports.trueStr = void 0;
7
7
  exports.connectionsXmlPath = "xl/connections.xml";
8
8
  exports.sharedStringsXmlPath = "xl/sharedStrings.xml";
9
9
  exports.sheetsXmlPath = "xl/worksheets/sheet1.xml";
@@ -14,6 +14,8 @@ exports.queryTablesPath = "xl/queryTables/";
14
14
  exports.pivotCachesPath = "xl/pivotCache/";
15
15
  exports.section1mPath = "Formulas/Section1.m";
16
16
  exports.docPropsCoreXmlPath = "docProps/core.xml";
17
+ exports.relsXmlPath = "_rels/.rels";
18
+ exports.docMetadataXmlPath = "docMetadata";
17
19
  exports.docPropsRootElement = "cp:coreProperties";
18
20
  exports.sharedStringsNotFoundErr = "SharedStrings were not found in template";
19
21
  exports.connectionsNotFoundErr = "Connections were not found in template";
@@ -24,7 +26,7 @@ exports.queryNameNotFoundErr = "Query name was not found";
24
26
  exports.queryAndPivotTableNotFoundErr = "No such query found in Query Table or Pivot Table found in given template";
25
27
  exports.queryConnectionNotFoundErr = "No connection found for query";
26
28
  exports.formulaSectionNotFoundErr = "Formula section wasn't found in template";
27
- exports.templateWithInitialDataErr = "Cannot receive template file with initial data";
29
+ exports.templateWithInitialDataErr = "Cannot use a template file with initial data";
28
30
  exports.queryTableNotFoundErr = "Query table wasn't found in template";
29
31
  exports.tableNotFoundErr = "Table wasn't found in template";
30
32
  exports.invalidValueInColumnErr = "Invalid cell value in column";
@@ -38,6 +40,8 @@ exports.InvalidColumnNameErr = "Invalid column name";
38
40
  exports.promotedHeadersCannotBeUsedWithoutAdjustingColumnNamesErr = "Headers cannot be promoted without adjusting column names";
39
41
  exports.unexpectedErr = "Unexpected error";
40
42
  exports.arrayIsntMxNErr = "Array isn't MxN";
43
+ exports.templateFileNotSupportedErr = "Template file is not supported for this API call";
44
+ exports.relsNotFoundErr = ".rels were not found in template";
41
45
  exports.blobFileType = "blob";
42
46
  exports.uint8ArrayType = "uint8array";
43
47
  exports.application = "application/xlsx";
@@ -79,6 +83,7 @@ exports.element = {
79
83
  sheetData: "sheetData",
80
84
  row: "row",
81
85
  dimension: "dimension",
86
+ selection: "selection",
82
87
  kindCell: "c",
83
88
  };
84
89
  exports.elementAttributes = {
@@ -104,6 +109,7 @@ exports.elementAttributes = {
104
109
  uniqueName: "uniqueName",
105
110
  queryTableFieldId: "queryTableFieldId",
106
111
  reference: "ref",
112
+ sqref: "sqref",
107
113
  tableColumnId: "tableColumnId",
108
114
  nextId: "nextId",
109
115
  row: "r",
@@ -129,9 +129,9 @@ const updateSheetsInitialData = (sheetsXmlString, tableData) => {
129
129
  sheetData.appendChild(newRow);
130
130
  rowIndex++;
131
131
  });
132
- sheetsDoc
133
- .getElementsByTagName(constants_1.element.dimension)[0]
134
- .setAttribute(constants_1.elementAttributes.reference, documentUtils_1.default.getTableReference(tableData.rows[0].length - 1, tableData.rows.length));
132
+ const reference = documentUtils_1.default.getTableReference(tableData.rows[0].length - 1, tableData.rows.length + 1);
133
+ sheetsDoc.getElementsByTagName(constants_1.element.dimension)[0].setAttribute(constants_1.elementAttributes.reference, reference);
134
+ sheetsDoc.getElementsByTagName(constants_1.element.selection)[0].setAttribute(constants_1.elementAttributes.sqref, reference);
135
135
  return serializer.serializeToString(sheetsDoc);
136
136
  };
137
137
  exports.default = {
@@ -39,6 +39,32 @@ const updateDocProps = (zip, docProps = {}) => __awaiter(void 0, void 0, void 0,
39
39
  const newDoc = serializer.serializeToString(doc);
40
40
  zip.file(constants_1.docPropsCoreXmlPath, newDoc);
41
41
  });
42
+ const clearLabelInfo = (zip) => __awaiter(void 0, void 0, void 0, function* () {
43
+ var _a, _b, _c, _d;
44
+ // remove docMetadata folder that contains only LabelInfo.xml in template file.
45
+ zip.remove(constants_1.docMetadataXmlPath);
46
+ // fix rels
47
+ const relsString = yield ((_a = zip.file(constants_1.relsXmlPath)) === null || _a === void 0 ? void 0 : _a.async(constants_1.textResultType));
48
+ if (relsString === undefined) {
49
+ throw new Error(constants_1.relsNotFoundErr);
50
+ }
51
+ const parser = new DOMParser();
52
+ const doc = parser.parseFromString(relsString, constants_1.xmlTextResultType);
53
+ const relationships = doc.querySelector("Relationships");
54
+ if (relationships === null) {
55
+ throw new Error(constants_1.unexpectedErr);
56
+ }
57
+ const element = relationships.querySelector('Relationship[Target="docMetadata/LabelInfo.xml"]');
58
+ if (element) {
59
+ relationships.removeChild(element);
60
+ }
61
+ (_b = relationships.querySelector('Relationship[Target="xl/workbook.xml"]')) === null || _b === void 0 ? void 0 : _b.setAttribute("Id", "rId1");
62
+ (_c = relationships.querySelector('Relationship[Target="docProps/core.xml"]')) === null || _c === void 0 ? void 0 : _c.setAttribute("Id", "rId2");
63
+ (_d = relationships.querySelector('Relationship[Target="docProps/app.xml"]')) === null || _d === void 0 ? void 0 : _d.setAttribute("Id", "rId3");
64
+ const serializer = new XMLSerializer();
65
+ const newDoc = serializer.serializeToString(doc);
66
+ zip.file(constants_1.relsXmlPath, newDoc);
67
+ });
42
68
  const updateConnections = (connectionsXmlString, queryName, refreshOnOpen) => {
43
69
  var _a, _b, _c;
44
70
  const parser = new DOMParser();
@@ -109,11 +135,11 @@ const updateWorksheet = (sheetsXmlString, sharedStringIndex) => {
109
135
  return newSheet;
110
136
  };
111
137
  const updatePivotTablesandQueryTables = (zip, queryName, refreshOnOpen, connectionId) => __awaiter(void 0, void 0, void 0, function* () {
112
- var _a, _b;
138
+ var _e, _f;
113
139
  // Find Query Table
114
140
  let found = false;
115
141
  const queryTablePromises = [];
116
- (_a = zip.folder(constants_1.queryTablesPath)) === null || _a === void 0 ? void 0 : _a.forEach((relativePath, queryTableFile) => __awaiter(void 0, void 0, void 0, function* () {
142
+ (_e = zip.folder(constants_1.queryTablesPath)) === null || _e === void 0 ? void 0 : _e.forEach((relativePath, queryTableFile) => __awaiter(void 0, void 0, void 0, function* () {
117
143
  queryTablePromises.push((() => {
118
144
  return queryTableFile.async(constants_1.textResultType).then((queryTableString) => {
119
145
  return {
@@ -135,7 +161,7 @@ const updatePivotTablesandQueryTables = (zip, queryName, refreshOnOpen, connecti
135
161
  }
136
162
  // Find Pivot Table
137
163
  const pivotCachePromises = [];
138
- (_b = zip.folder(constants_1.pivotCachesPath)) === null || _b === void 0 ? void 0 : _b.forEach((relativePath, pivotCacheFile) => __awaiter(void 0, void 0, void 0, function* () {
164
+ (_f = zip.folder(constants_1.pivotCachesPath)) === null || _f === void 0 ? void 0 : _f.forEach((relativePath, pivotCacheFile) => __awaiter(void 0, void 0, void 0, function* () {
139
165
  if (relativePath.startsWith(constants_1.pivotCachesPathPrefix)) {
140
166
  pivotCachePromises.push((() => {
141
167
  return pivotCacheFile.async(constants_1.textResultType).then((pivotCacheString) => {
@@ -191,6 +217,7 @@ const updatePivotTable = (tableXmlString, connectionId, refreshOnOpen) => {
191
217
  };
192
218
  exports.default = {
193
219
  updateDocProps,
220
+ clearLabelInfo,
194
221
  updateConnections,
195
222
  updateSharedStrings,
196
223
  updateWorksheet,
@@ -19,8 +19,12 @@ const mashupDocumentParser_1 = require("./mashupDocumentParser");
19
19
  const pqUtils_1 = __importDefault(require("./pqUtils"));
20
20
  const xmlInnerPartsUtils_1 = __importDefault(require("./xmlInnerPartsUtils"));
21
21
  const tableUtils_1 = __importDefault(require("./tableUtils"));
22
- const updateWorkbookInitialDataIfNeeded = (zip, docProps, tableData, updateQueryTable = false) => __awaiter(void 0, void 0, void 0, function* () {
23
- yield xmlInnerPartsUtils_1.default.updateDocProps(zip, docProps);
22
+ const updateWorkbookDataAndConfigurations = (zip, fileConfigs, tableData, updateQueryTable = false) => __awaiter(void 0, void 0, void 0, function* () {
23
+ yield xmlInnerPartsUtils_1.default.updateDocProps(zip, fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.docProps);
24
+ if ((fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateFile) === undefined) {
25
+ // If we are using our base template, we need to clear label info
26
+ yield xmlInnerPartsUtils_1.default.clearLabelInfo(zip);
27
+ }
24
28
  yield tableUtils_1.default.updateTableInitialDataIfNeeded(zip, tableData, updateQueryTable);
25
29
  });
26
30
  const updateWorkbookPowerQueryDocument = (zip, queryName, queryMashupDoc) => __awaiter(void 0, void 0, void 0, function* () {
@@ -58,7 +62,7 @@ const updateWorkbookSingleQueryAttributes = (zip, queryName, refreshOnOpen) => _
58
62
  yield xmlInnerPartsUtils_1.default.updatePivotTablesandQueryTables(zip, queryName, refreshOnOpen, connectionId);
59
63
  });
60
64
  exports.default = {
61
- updateWorkbookInitialDataIfNeeded,
65
+ updateWorkbookDataAndConfigurations,
62
66
  updateWorkbookPowerQueryDocument,
63
67
  updateWorkbookSingleQueryAttributes,
64
68
  };
@@ -45,7 +45,7 @@ describe("Table Utils tests", () => {
45
45
  expect(tableXmlSheet).not.toContain('queryTableFieldId="2"');
46
46
  });
47
47
  test("test valid initial data in SheetsXML", () => {
48
- const defaultString = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac xr xr2 xr3" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" xr:uid="{EDF0138E-D216-4CD1-8EFA-1396A1BB4478}"><sheetPr codeName="Sheet1"/><dimension ref="A1:A2"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"/></sheetViews><sheetFormatPr defaultRowHeight="14.4" x14ac:dyDescent="0.3"/><cols><col min="1" max="1" width="9.6640625" bestFit="1" customWidth="1"/></cols><sheetData><row r="1" spans="1:1" x14ac:dyDescent="0.3"><c r="A1" t="s"><v>0</v></c></row><row r="2" spans="1:1" x14ac:dyDescent="0.3"><c r="A2" t="s"><v>1</v></c></row></sheetData><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/><tableParts count="1"><tablePart r:id="rId1"/></tableParts></worksheet>';
48
+ const defaultString = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac xr xr2 xr3" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" xr:uid="{EDF0138E-D216-4CD1-8EFA-1396A1BB4478}"><sheetPr codeName="Sheet1"/><dimension ref="A1:B2"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection sqref="A1:B2"/></sheetView></sheetViews><sheetFormatPr defaultRowHeight="14.4" x14ac:dyDescent="0.3"/><cols><col min="1" max="1" width="9.6640625" bestFit="1" customWidth="1"/></cols><sheetData><row r="1" spans="1:1" x14ac:dyDescent="0.3"><c r="A1" t="s"><v>0</v></c></row><row r="2" spans="1:1" x14ac:dyDescent="0.3"><c r="A2" t="s"><v>1</v></c></row></sheetData><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/><tableParts count="1"><tablePart r:id="rId1"/></tableParts></worksheet>';
49
49
  const sheetsXmlString = utils_1.tableUtils.updateSheetsInitialData(defaultString, {
50
50
  columnNames: ["Column1", "Column2"],
51
51
  rows: [["1", "2"]],
@@ -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.element = exports.BOM = exports.falseStr = exports.trueStr = 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.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.docPropsCoreXmlPath = exports.section1mPath = exports.pivotCachesPath = exports.queryTablesPath = exports.workbookXmlPath = exports.queryTableXmlPath = exports.tableXmlPath = exports.sheetsXmlPath = exports.sharedStringsXmlPath = exports.connectionsXmlPath = void 0;
6
- exports.URLS = exports.defaults = exports.elementAttributesValues = exports.dataTypeKind = exports.elementAttributes = void 0;
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.relsNotFoundErr = exports.templateFileNotSupportedErr = 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.URLS = exports.defaults = exports.elementAttributesValues = exports.dataTypeKind = exports.elementAttributes = exports.element = exports.BOM = exports.falseStr = exports.trueStr = void 0;
7
7
  exports.connectionsXmlPath = "xl/connections.xml";
8
8
  exports.sharedStringsXmlPath = "xl/sharedStrings.xml";
9
9
  exports.sheetsXmlPath = "xl/worksheets/sheet1.xml";
@@ -14,6 +14,8 @@ exports.queryTablesPath = "xl/queryTables/";
14
14
  exports.pivotCachesPath = "xl/pivotCache/";
15
15
  exports.section1mPath = "Formulas/Section1.m";
16
16
  exports.docPropsCoreXmlPath = "docProps/core.xml";
17
+ exports.relsXmlPath = "_rels/.rels";
18
+ exports.docMetadataXmlPath = "docMetadata";
17
19
  exports.docPropsRootElement = "cp:coreProperties";
18
20
  exports.sharedStringsNotFoundErr = "SharedStrings were not found in template";
19
21
  exports.connectionsNotFoundErr = "Connections were not found in template";
@@ -24,7 +26,7 @@ exports.queryNameNotFoundErr = "Query name was not found";
24
26
  exports.queryAndPivotTableNotFoundErr = "No such query found in Query Table or Pivot Table found in given template";
25
27
  exports.queryConnectionNotFoundErr = "No connection found for query";
26
28
  exports.formulaSectionNotFoundErr = "Formula section wasn't found in template";
27
- exports.templateWithInitialDataErr = "Cannot receive template file with initial data";
29
+ exports.templateWithInitialDataErr = "Cannot use a template file with initial data";
28
30
  exports.queryTableNotFoundErr = "Query table wasn't found in template";
29
31
  exports.tableNotFoundErr = "Table wasn't found in template";
30
32
  exports.invalidValueInColumnErr = "Invalid cell value in column";
@@ -38,6 +40,8 @@ exports.InvalidColumnNameErr = "Invalid column name";
38
40
  exports.promotedHeadersCannotBeUsedWithoutAdjustingColumnNamesErr = "Headers cannot be promoted without adjusting column names";
39
41
  exports.unexpectedErr = "Unexpected error";
40
42
  exports.arrayIsntMxNErr = "Array isn't MxN";
43
+ exports.templateFileNotSupportedErr = "Template file is not supported for this API call";
44
+ exports.relsNotFoundErr = ".rels were not found in template";
41
45
  exports.blobFileType = "blob";
42
46
  exports.uint8ArrayType = "uint8array";
43
47
  exports.application = "application/xlsx";
@@ -39,6 +39,32 @@ const updateDocProps = (zip, docProps = {}) => __awaiter(void 0, void 0, void 0,
39
39
  const newDoc = serializer.serializeToString(doc);
40
40
  zip.file(constants_1.docPropsCoreXmlPath, newDoc);
41
41
  });
42
+ const clearLabelInfo = (zip) => __awaiter(void 0, void 0, void 0, function* () {
43
+ var _a, _b, _c, _d;
44
+ // remove docMetadata folder that contains only LabelInfo.xml in template file.
45
+ zip.remove(constants_1.docMetadataXmlPath);
46
+ // fix rels
47
+ const relsString = yield ((_a = zip.file(constants_1.relsXmlPath)) === null || _a === void 0 ? void 0 : _a.async(constants_1.textResultType));
48
+ if (relsString === undefined) {
49
+ throw new Error(constants_1.relsNotFoundErr);
50
+ }
51
+ const parser = new DOMParser();
52
+ const doc = parser.parseFromString(relsString, constants_1.xmlTextResultType);
53
+ const relationships = doc.querySelector("Relationships");
54
+ if (relationships === null) {
55
+ throw new Error(constants_1.unexpectedErr);
56
+ }
57
+ const element = relationships.querySelector('Relationship[Target="docMetadata/LabelInfo.xml"]');
58
+ if (element) {
59
+ relationships.removeChild(element);
60
+ }
61
+ (_b = relationships.querySelector('Relationship[Target="xl/workbook.xml"]')) === null || _b === void 0 ? void 0 : _b.setAttribute("Id", "rId1");
62
+ (_c = relationships.querySelector('Relationship[Target="docProps/core.xml"]')) === null || _c === void 0 ? void 0 : _c.setAttribute("Id", "rId2");
63
+ (_d = relationships.querySelector('Relationship[Target="docProps/app.xml"]')) === null || _d === void 0 ? void 0 : _d.setAttribute("Id", "rId3");
64
+ const serializer = new XMLSerializer();
65
+ const newDoc = serializer.serializeToString(doc);
66
+ zip.file(constants_1.relsXmlPath, newDoc);
67
+ });
42
68
  const updateConnections = (connectionsXmlString, queryName, refreshOnOpen) => {
43
69
  var _a, _b, _c;
44
70
  const parser = new DOMParser();
@@ -109,11 +135,11 @@ const updateWorksheet = (sheetsXmlString, sharedStringIndex) => {
109
135
  return newSheet;
110
136
  };
111
137
  const updatePivotTablesandQueryTables = (zip, queryName, refreshOnOpen, connectionId) => __awaiter(void 0, void 0, void 0, function* () {
112
- var _a, _b;
138
+ var _e, _f;
113
139
  // Find Query Table
114
140
  let found = false;
115
141
  const queryTablePromises = [];
116
- (_a = zip.folder(constants_1.queryTablesPath)) === null || _a === void 0 ? void 0 : _a.forEach((relativePath, queryTableFile) => __awaiter(void 0, void 0, void 0, function* () {
142
+ (_e = zip.folder(constants_1.queryTablesPath)) === null || _e === void 0 ? void 0 : _e.forEach((relativePath, queryTableFile) => __awaiter(void 0, void 0, void 0, function* () {
117
143
  queryTablePromises.push((() => {
118
144
  return queryTableFile.async(constants_1.textResultType).then((queryTableString) => {
119
145
  return {
@@ -135,7 +161,7 @@ const updatePivotTablesandQueryTables = (zip, queryName, refreshOnOpen, connecti
135
161
  }
136
162
  // Find Pivot Table
137
163
  const pivotCachePromises = [];
138
- (_b = zip.folder(constants_1.pivotCachesPath)) === null || _b === void 0 ? void 0 : _b.forEach((relativePath, pivotCacheFile) => __awaiter(void 0, void 0, void 0, function* () {
164
+ (_f = zip.folder(constants_1.pivotCachesPath)) === null || _f === void 0 ? void 0 : _f.forEach((relativePath, pivotCacheFile) => __awaiter(void 0, void 0, void 0, function* () {
139
165
  if (relativePath.startsWith(constants_1.pivotCachesPathPrefix)) {
140
166
  pivotCachePromises.push((() => {
141
167
  return pivotCacheFile.async(constants_1.textResultType).then((pivotCacheString) => {
@@ -191,6 +217,7 @@ const updatePivotTable = (tableXmlString, connectionId, refreshOnOpen) => {
191
217
  };
192
218
  exports.default = {
193
219
  updateDocProps,
220
+ clearLabelInfo,
194
221
  updateConnections,
195
222
  updateSharedStrings,
196
223
  updateWorksheet,
@@ -19,8 +19,12 @@ const mashupDocumentParser_1 = require("./mashupDocumentParser");
19
19
  const pqUtils_1 = __importDefault(require("./pqUtils"));
20
20
  const xmlInnerPartsUtils_1 = __importDefault(require("./xmlInnerPartsUtils"));
21
21
  const tableUtils_1 = __importDefault(require("./tableUtils"));
22
- const updateWorkbookInitialDataIfNeeded = (zip, docProps, tableData, updateQueryTable = false) => __awaiter(void 0, void 0, void 0, function* () {
23
- yield xmlInnerPartsUtils_1.default.updateDocProps(zip, docProps);
22
+ const updateWorkbookDataAndConfigurations = (zip, fileConfigs, tableData, updateQueryTable = false) => __awaiter(void 0, void 0, void 0, function* () {
23
+ yield xmlInnerPartsUtils_1.default.updateDocProps(zip, fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.docProps);
24
+ if ((fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateFile) === undefined) {
25
+ // If we are using our base template, we need to clear label info
26
+ yield xmlInnerPartsUtils_1.default.clearLabelInfo(zip);
27
+ }
24
28
  yield tableUtils_1.default.updateTableInitialDataIfNeeded(zip, tableData, updateQueryTable);
25
29
  });
26
30
  const updateWorkbookPowerQueryDocument = (zip, queryName, queryMashupDoc) => __awaiter(void 0, void 0, void 0, function* () {
@@ -58,7 +62,7 @@ const updateWorkbookSingleQueryAttributes = (zip, queryName, refreshOnOpen) => _
58
62
  yield xmlInnerPartsUtils_1.default.updatePivotTablesandQueryTables(zip, queryName, refreshOnOpen, connectionId);
59
63
  });
60
64
  exports.default = {
61
- updateWorkbookInitialDataIfNeeded,
65
+ updateWorkbookDataAndConfigurations,
62
66
  updateWorkbookPowerQueryDocument,
63
67
  updateWorkbookSingleQueryAttributes,
64
68
  };
@@ -1,5 +1,5 @@
1
- import { DocProps, QueryInfo, Grid, FileConfigs } from "./types";
1
+ import { QueryInfo, Grid, FileConfigs } from "./types";
2
2
  export declare const generateSingleQueryWorkbook: (query: QueryInfo, initialDataGrid?: Grid, fileConfigs?: FileConfigs) => Promise<Blob>;
3
- export declare const generateTableWorkbookFromHtml: (htmlTable: HTMLTableElement, docProps?: DocProps) => Promise<Blob>;
4
- export declare const generateTableWorkbookFromGrid: (grid: Grid, docProps?: DocProps) => Promise<Blob>;
3
+ export declare const generateTableWorkbookFromHtml: (htmlTable: HTMLTableElement, fileConfigs?: FileConfigs) => Promise<Blob>;
4
+ export declare const generateTableWorkbookFromGrid: (grid: Grid, fileConfigs?: FileConfigs) => Promise<Blob>;
5
5
  export declare const downloadWorkbook: (file: Blob, filename: string) => void;
@@ -34,34 +34,40 @@ const generateSingleQueryWorkbook = (query, initialDataGrid, fileConfigs) => __a
34
34
  utils_1.pqUtils.validateQueryName(query.queryName);
35
35
  const zip = templateFile === undefined ? yield jszip_1.default.loadAsync(workbookTemplate_1.SIMPLE_QUERY_WORKBOOK_TEMPLATE, { base64: true }) : yield jszip_1.default.loadAsync(templateFile);
36
36
  const tableData = initialDataGrid ? utils_1.gridUtils.parseToTableData(initialDataGrid) : undefined;
37
- return yield generateSingleQueryWorkbookFromZip(zip, query, fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.docProps, tableData);
37
+ return yield generateSingleQueryWorkbookFromZip(zip, query, fileConfigs, tableData);
38
38
  });
39
39
  exports.generateSingleQueryWorkbook = generateSingleQueryWorkbook;
40
- const generateTableWorkbookFromHtml = (htmlTable, docProps) => __awaiter(void 0, void 0, void 0, function* () {
40
+ const generateTableWorkbookFromHtml = (htmlTable, fileConfigs) => __awaiter(void 0, void 0, void 0, function* () {
41
+ if ((fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateFile) !== undefined) {
42
+ throw new Error(constants_1.templateFileNotSupportedErr);
43
+ }
41
44
  const gridData = utils_1.htmlUtils.extractTableValues(htmlTable);
42
- return yield (0, exports.generateTableWorkbookFromGrid)({ data: gridData, config: { promoteHeaders: true } }, docProps);
45
+ return yield (0, exports.generateTableWorkbookFromGrid)({ data: gridData, config: { promoteHeaders: true } }, fileConfigs);
43
46
  });
44
47
  exports.generateTableWorkbookFromHtml = generateTableWorkbookFromHtml;
45
- const generateTableWorkbookFromGrid = (grid, docProps) => __awaiter(void 0, void 0, void 0, function* () {
48
+ const generateTableWorkbookFromGrid = (grid, fileConfigs) => __awaiter(void 0, void 0, void 0, function* () {
49
+ if ((fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateFile) !== undefined) {
50
+ throw new Error(constants_1.templateFileNotSupportedErr);
51
+ }
46
52
  const zip = yield jszip_1.default.loadAsync(workbookTemplate_1.SIMPLE_BLANK_TABLE_TEMPLATE, { base64: true });
47
53
  const tableData = utils_1.gridUtils.parseToTableData(grid);
48
54
  if (tableData === undefined) {
49
55
  throw new Error(constants_1.tableNotFoundErr);
50
56
  }
51
- yield utils_1.xmlPartsUtils.updateWorkbookInitialDataIfNeeded(zip, docProps, tableData);
57
+ yield utils_1.xmlPartsUtils.updateWorkbookDataAndConfigurations(zip, fileConfigs, tableData);
52
58
  return yield zip.generateAsync({
53
59
  type: constants_1.blobFileType,
54
60
  mimeType: constants_1.application,
55
61
  });
56
62
  });
57
63
  exports.generateTableWorkbookFromGrid = generateTableWorkbookFromGrid;
58
- const generateSingleQueryWorkbookFromZip = (zip, query, docProps, tableData) => __awaiter(void 0, void 0, void 0, function* () {
64
+ const generateSingleQueryWorkbookFromZip = (zip, query, fileConfigs, tableData) => __awaiter(void 0, void 0, void 0, function* () {
59
65
  if (!query.queryName) {
60
66
  query.queryName = constants_1.defaults.queryName;
61
67
  }
62
68
  yield utils_1.xmlPartsUtils.updateWorkbookPowerQueryDocument(zip, query.queryName, (0, generators_1.generateSingleQueryMashup)(query.queryName, query.queryMashup));
63
69
  yield utils_1.xmlPartsUtils.updateWorkbookSingleQueryAttributes(zip, query.queryName, query.refreshOnOpen);
64
- yield utils_1.xmlPartsUtils.updateWorkbookInitialDataIfNeeded(zip, docProps, tableData, true /*updateQueryTable*/);
70
+ yield utils_1.xmlPartsUtils.updateWorkbookDataAndConfigurations(zip, fileConfigs, tableData, true /*updateQueryTable*/);
65
71
  return yield zip.generateAsync({
66
72
  type: constants_1.blobFileType,
67
73
  mimeType: constants_1.application,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@microsoft/connected-workbooks",
3
- "version": "2.1.25",
3
+ "version": "3.0.0",
4
4
  "description": "Microsoft backed, Excel advanced xlsx workbook generation JavaScript library",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -39,7 +39,8 @@
39
39
  "dependencies": {
40
40
  "base64-js": "^1.5.1",
41
41
  "buffer": "^6.0.3",
42
- "jszip": "^3.5.0"
42
+ "jszip": "^3.5.0",
43
+ "uuid": "^9.0.0"
43
44
  },
44
45
  "devDependencies": {
45
46
  "@babel/core": "^7.14.6",