@microsoft/connected-workbooks 3.0.0 → 3.1.1-beta.2

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.
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  // Copyright (c) Microsoft Corporation.
3
2
  // Licensed under the MIT license.
4
3
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
@@ -10,134 +9,174 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
9
  step((generator = generator.apply(thisArg, _arguments || [])).next());
11
10
  });
12
11
  };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
12
+ var __generator = (this && this.__generator) || function (thisArg, body) {
13
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
14
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
15
+ function verb(n) { return function (v) { return step([n, v]); }; }
16
+ function step(op) {
17
+ if (f) throw new TypeError("Generator is already executing.");
18
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
19
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
20
+ if (y = 0, t) op = [op[0] & 2, t.value];
21
+ switch (op[0]) {
22
+ case 0: case 1: t = op; break;
23
+ case 4: _.label++; return { value: op[1], done: false };
24
+ case 5: _.label++; y = op[1]; op = [0]; continue;
25
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
26
+ default:
27
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
28
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
29
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
30
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
31
+ if (t[2]) _.ops.pop();
32
+ _.trys.pop(); continue;
33
+ }
34
+ op = body.call(thisArg, _);
35
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
36
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
37
+ }
15
38
  };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- const constants_1 = require("./constants");
18
- const documentUtils_1 = __importDefault(require("./documentUtils"));
19
- const uuid_1 = require("uuid");
20
- const updateTableInitialDataIfNeeded = (zip, tableData, updateQueryTable) => __awaiter(void 0, void 0, void 0, function* () {
39
+ import { defaults, element, elementAttributes, queryTableNotFoundErr, queryTableXmlPath, sheetsNotFoundErr, sheetsXmlPath, tableNotFoundErr, tableXmlPath, textResultType, workbookXmlPath, xmlTextResultType, } from "./constants";
40
+ import documentUtils from "./documentUtils";
41
+ import { v4 } from "uuid";
42
+ var updateTableInitialDataIfNeeded = function (zip, tableData, updateQueryTable) { return __awaiter(void 0, void 0, void 0, function () {
43
+ var sheetsXmlString, newSheet, queryTableXmlString, newQueryTable, workbookXmlString, newWorkbook, tableXmlString, newTable;
21
44
  var _a, _b, _c, _d;
22
- if (!tableData) {
23
- return;
24
- }
25
- const sheetsXmlString = yield ((_a = zip.file(constants_1.sheetsXmlPath)) === null || _a === void 0 ? void 0 : _a.async(constants_1.textResultType));
26
- if (sheetsXmlString === undefined) {
27
- throw new Error(constants_1.sheetsNotFoundErr);
28
- }
29
- const newSheet = updateSheetsInitialData(sheetsXmlString, tableData);
30
- zip.file(constants_1.sheetsXmlPath, newSheet);
31
- if (updateQueryTable) {
32
- const queryTableXmlString = yield ((_b = zip.file(constants_1.queryTableXmlPath)) === null || _b === void 0 ? void 0 : _b.async(constants_1.textResultType));
33
- if (queryTableXmlString === undefined) {
34
- throw new Error(constants_1.queryTableNotFoundErr);
35
- }
36
- const newQueryTable = yield updateQueryTablesInitialData(queryTableXmlString, tableData);
37
- zip.file(constants_1.queryTableXmlPath, newQueryTable);
38
- // update defined name
39
- const workbookXmlString = yield ((_c = zip.file(constants_1.workbookXmlPath)) === null || _c === void 0 ? void 0 : _c.async(constants_1.textResultType));
40
- if (workbookXmlString === undefined) {
41
- throw new Error(constants_1.sheetsNotFoundErr);
45
+ return __generator(this, function (_e) {
46
+ switch (_e.label) {
47
+ case 0:
48
+ if (!tableData) {
49
+ return [2 /*return*/];
50
+ }
51
+ return [4 /*yield*/, ((_a = zip.file(sheetsXmlPath)) === null || _a === void 0 ? void 0 : _a.async(textResultType))];
52
+ case 1:
53
+ sheetsXmlString = _e.sent();
54
+ if (sheetsXmlString === undefined) {
55
+ throw new Error(sheetsNotFoundErr);
56
+ }
57
+ newSheet = updateSheetsInitialData(sheetsXmlString, tableData);
58
+ zip.file(sheetsXmlPath, newSheet);
59
+ if (!updateQueryTable) return [3 /*break*/, 5];
60
+ return [4 /*yield*/, ((_b = zip.file(queryTableXmlPath)) === null || _b === void 0 ? void 0 : _b.async(textResultType))];
61
+ case 2:
62
+ queryTableXmlString = _e.sent();
63
+ if (queryTableXmlString === undefined) {
64
+ throw new Error(queryTableNotFoundErr);
65
+ }
66
+ return [4 /*yield*/, updateQueryTablesInitialData(queryTableXmlString, tableData)];
67
+ case 3:
68
+ newQueryTable = _e.sent();
69
+ zip.file(queryTableXmlPath, newQueryTable);
70
+ return [4 /*yield*/, ((_c = zip.file(workbookXmlPath)) === null || _c === void 0 ? void 0 : _c.async(textResultType))];
71
+ case 4:
72
+ workbookXmlString = _e.sent();
73
+ if (workbookXmlString === undefined) {
74
+ throw new Error(sheetsNotFoundErr);
75
+ }
76
+ newWorkbook = updateWorkbookInitialData(workbookXmlString, tableData);
77
+ zip.file(workbookXmlPath, newWorkbook);
78
+ _e.label = 5;
79
+ case 5: return [4 /*yield*/, ((_d = zip.file(tableXmlPath)) === null || _d === void 0 ? void 0 : _d.async(textResultType))];
80
+ case 6:
81
+ tableXmlString = _e.sent();
82
+ if (tableXmlString === undefined) {
83
+ throw new Error(tableNotFoundErr);
84
+ }
85
+ newTable = updateTablesInitialData(tableXmlString, tableData, updateQueryTable);
86
+ zip.file(tableXmlPath, newTable);
87
+ return [2 /*return*/];
42
88
  }
43
- const newWorkbook = updateWorkbookInitialData(workbookXmlString, tableData);
44
- zip.file(constants_1.workbookXmlPath, newWorkbook);
45
- }
46
- const tableXmlString = yield ((_d = zip.file(constants_1.tableXmlPath)) === null || _d === void 0 ? void 0 : _d.async(constants_1.textResultType));
47
- if (tableXmlString === undefined) {
48
- throw new Error(constants_1.tableNotFoundErr);
49
- }
50
- const newTable = updateTablesInitialData(tableXmlString, tableData, updateQueryTable);
51
- zip.file(constants_1.tableXmlPath, newTable);
52
- });
53
- const updateTablesInitialData = (tableXmlString, tableData, updateQueryTable = false) => {
54
- const parser = new DOMParser();
55
- const serializer = new XMLSerializer();
56
- const tableDoc = parser.parseFromString(tableXmlString, constants_1.xmlTextResultType);
57
- const tableColumns = tableDoc.getElementsByTagName(constants_1.element.tableColumns)[0];
89
+ });
90
+ }); };
91
+ var updateTablesInitialData = function (tableXmlString, tableData, updateQueryTable) {
92
+ if (updateQueryTable === void 0) { updateQueryTable = false; }
93
+ var parser = new DOMParser();
94
+ var serializer = new XMLSerializer();
95
+ var tableDoc = parser.parseFromString(tableXmlString, xmlTextResultType);
96
+ var tableColumns = tableDoc.getElementsByTagName(element.tableColumns)[0];
58
97
  tableColumns.textContent = "";
59
- tableData.columnNames.forEach((column, columnIndex) => {
60
- const tableColumn = tableDoc.createElementNS(tableDoc.documentElement.namespaceURI, constants_1.element.tableColumn);
61
- tableColumn.setAttribute(constants_1.elementAttributes.id, (columnIndex + 1).toString());
62
- tableColumn.setAttribute(constants_1.elementAttributes.name, column);
98
+ tableData.columnNames.forEach(function (column, columnIndex) {
99
+ var tableColumn = tableDoc.createElementNS(tableDoc.documentElement.namespaceURI, element.tableColumn);
100
+ tableColumn.setAttribute(elementAttributes.id, (columnIndex + 1).toString());
101
+ tableColumn.setAttribute(elementAttributes.name, column);
63
102
  tableColumns.appendChild(tableColumn);
64
- tableColumn.setAttribute(constants_1.elementAttributes.xr3uid, "{" + (0, uuid_1.v4)().toUpperCase() + "}");
103
+ tableColumn.setAttribute(elementAttributes.xr3uid, "{" + v4().toUpperCase() + "}");
65
104
  if (updateQueryTable) {
66
- tableColumn.setAttribute(constants_1.elementAttributes.uniqueName, (columnIndex + 1).toString());
67
- tableColumn.setAttribute(constants_1.elementAttributes.queryTableFieldId, (columnIndex + 1).toString());
105
+ tableColumn.setAttribute(elementAttributes.uniqueName, (columnIndex + 1).toString());
106
+ tableColumn.setAttribute(elementAttributes.queryTableFieldId, (columnIndex + 1).toString());
68
107
  }
69
108
  });
70
- tableColumns.setAttribute(constants_1.elementAttributes.count, tableData.columnNames.length.toString());
109
+ tableColumns.setAttribute(elementAttributes.count, tableData.columnNames.length.toString());
71
110
  tableDoc
72
- .getElementsByTagName(constants_1.element.table)[0]
73
- .setAttribute(constants_1.elementAttributes.reference, `A1:${documentUtils_1.default.getCellReferenceRelative(tableData.columnNames.length - 1, tableData.rows.length + 1)}`);
111
+ .getElementsByTagName(element.table)[0]
112
+ .setAttribute(elementAttributes.reference, "A1:".concat(documentUtils.getCellReferenceRelative(tableData.columnNames.length - 1, tableData.rows.length + 1)));
74
113
  tableDoc
75
- .getElementsByTagName(constants_1.element.autoFilter)[0]
76
- .setAttribute(constants_1.elementAttributes.reference, `A1:${documentUtils_1.default.getCellReferenceRelative(tableData.columnNames.length - 1, tableData.rows.length + 1)}`);
114
+ .getElementsByTagName(element.autoFilter)[0]
115
+ .setAttribute(elementAttributes.reference, "A1:".concat(documentUtils.getCellReferenceRelative(tableData.columnNames.length - 1, tableData.rows.length + 1)));
77
116
  return serializer.serializeToString(tableDoc);
78
117
  };
79
- const updateWorkbookInitialData = (workbookXmlString, tableData) => {
80
- const newParser = new DOMParser();
81
- const newSerializer = new XMLSerializer();
82
- const workbookDoc = newParser.parseFromString(workbookXmlString, constants_1.xmlTextResultType);
83
- const definedName = workbookDoc.getElementsByTagName(constants_1.element.definedName)[0];
118
+ var updateWorkbookInitialData = function (workbookXmlString, tableData) {
119
+ var newParser = new DOMParser();
120
+ var newSerializer = new XMLSerializer();
121
+ var workbookDoc = newParser.parseFromString(workbookXmlString, xmlTextResultType);
122
+ var definedName = workbookDoc.getElementsByTagName(element.definedName)[0];
84
123
  definedName.textContent =
85
- constants_1.defaults.sheetName + `!$A$1:${documentUtils_1.default.getCellReferenceAbsolute(tableData.columnNames.length - 1, tableData.rows.length + 1)}`;
124
+ defaults.sheetName + "!$A$1:".concat(documentUtils.getCellReferenceAbsolute(tableData.columnNames.length - 1, tableData.rows.length + 1));
86
125
  return newSerializer.serializeToString(workbookDoc);
87
126
  };
88
- const updateQueryTablesInitialData = (queryTableXmlString, tableData) => {
89
- const parser = new DOMParser();
90
- const serializer = new XMLSerializer();
91
- const queryTableDoc = parser.parseFromString(queryTableXmlString, constants_1.xmlTextResultType);
92
- const queryTableFields = queryTableDoc.getElementsByTagName(constants_1.element.queryTableFields)[0];
127
+ var updateQueryTablesInitialData = function (queryTableXmlString, tableData) {
128
+ var parser = new DOMParser();
129
+ var serializer = new XMLSerializer();
130
+ var queryTableDoc = parser.parseFromString(queryTableXmlString, xmlTextResultType);
131
+ var queryTableFields = queryTableDoc.getElementsByTagName(element.queryTableFields)[0];
93
132
  queryTableFields.textContent = "";
94
- tableData.columnNames.forEach((column, columnIndex) => {
95
- const queryTableField = queryTableDoc.createElementNS(queryTableDoc.documentElement.namespaceURI, constants_1.element.queryTableField);
96
- queryTableField.setAttribute(constants_1.elementAttributes.id, (columnIndex + 1).toString());
97
- queryTableField.setAttribute(constants_1.elementAttributes.name, column);
98
- queryTableField.setAttribute(constants_1.elementAttributes.tableColumnId, (columnIndex + 1).toString());
133
+ tableData.columnNames.forEach(function (column, columnIndex) {
134
+ var queryTableField = queryTableDoc.createElementNS(queryTableDoc.documentElement.namespaceURI, element.queryTableField);
135
+ queryTableField.setAttribute(elementAttributes.id, (columnIndex + 1).toString());
136
+ queryTableField.setAttribute(elementAttributes.name, column);
137
+ queryTableField.setAttribute(elementAttributes.tableColumnId, (columnIndex + 1).toString());
99
138
  queryTableFields.appendChild(queryTableField);
100
139
  });
101
- queryTableFields.setAttribute(constants_1.elementAttributes.count, tableData.columnNames.length.toString());
102
- queryTableDoc.getElementsByTagName(constants_1.element.queryTableRefresh)[0].setAttribute(constants_1.elementAttributes.nextId, (tableData.columnNames.length + 1).toString());
140
+ queryTableFields.setAttribute(elementAttributes.count, tableData.columnNames.length.toString());
141
+ queryTableDoc.getElementsByTagName(element.queryTableRefresh)[0].setAttribute(elementAttributes.nextId, (tableData.columnNames.length + 1).toString());
103
142
  return serializer.serializeToString(queryTableDoc);
104
143
  };
105
- const updateSheetsInitialData = (sheetsXmlString, tableData) => {
106
- const parser = new DOMParser();
107
- const serializer = new XMLSerializer();
108
- const sheetsDoc = parser.parseFromString(sheetsXmlString, constants_1.xmlTextResultType);
109
- const sheetData = sheetsDoc.getElementsByTagName(constants_1.element.sheetData)[0];
144
+ var updateSheetsInitialData = function (sheetsXmlString, tableData) {
145
+ var parser = new DOMParser();
146
+ var serializer = new XMLSerializer();
147
+ var sheetsDoc = parser.parseFromString(sheetsXmlString, xmlTextResultType);
148
+ var sheetData = sheetsDoc.getElementsByTagName(element.sheetData)[0];
110
149
  sheetData.textContent = "";
111
- let rowIndex = 0;
112
- const columnRow = sheetsDoc.createElementNS(sheetsDoc.documentElement.namespaceURI, constants_1.element.row);
113
- columnRow.setAttribute(constants_1.elementAttributes.row, (rowIndex + 1).toString());
114
- columnRow.setAttribute(constants_1.elementAttributes.spans, "1:" + tableData.columnNames.length);
115
- columnRow.setAttribute(constants_1.elementAttributes.x14acDyDescent, "0.3");
116
- tableData.columnNames.forEach((col, colIndex) => {
117
- columnRow.appendChild(documentUtils_1.default.createCell(sheetsDoc, colIndex, rowIndex, col));
150
+ var rowIndex = 0;
151
+ var columnRow = sheetsDoc.createElementNS(sheetsDoc.documentElement.namespaceURI, element.row);
152
+ columnRow.setAttribute(elementAttributes.row, (rowIndex + 1).toString());
153
+ columnRow.setAttribute(elementAttributes.spans, "1:" + tableData.columnNames.length);
154
+ columnRow.setAttribute(elementAttributes.x14acDyDescent, "0.3");
155
+ tableData.columnNames.forEach(function (col, colIndex) {
156
+ columnRow.appendChild(documentUtils.createCell(sheetsDoc, colIndex, rowIndex, col));
118
157
  });
119
158
  sheetData.appendChild(columnRow);
120
159
  rowIndex++;
121
- tableData.rows.forEach((row) => {
122
- const newRow = sheetsDoc.createElementNS(sheetsDoc.documentElement.namespaceURI, constants_1.element.row);
123
- newRow.setAttribute(constants_1.elementAttributes.row, (rowIndex + 1).toString());
124
- newRow.setAttribute(constants_1.elementAttributes.spans, "1:" + row.length);
125
- newRow.setAttribute(constants_1.elementAttributes.x14acDyDescent, "0.3");
126
- row.forEach((cellContent, colIndex) => {
127
- newRow.appendChild(documentUtils_1.default.createCell(sheetsDoc, colIndex, rowIndex, cellContent));
160
+ tableData.rows.forEach(function (row) {
161
+ var newRow = sheetsDoc.createElementNS(sheetsDoc.documentElement.namespaceURI, element.row);
162
+ newRow.setAttribute(elementAttributes.row, (rowIndex + 1).toString());
163
+ newRow.setAttribute(elementAttributes.spans, "1:" + row.length);
164
+ newRow.setAttribute(elementAttributes.x14acDyDescent, "0.3");
165
+ row.forEach(function (cellContent, colIndex) {
166
+ newRow.appendChild(documentUtils.createCell(sheetsDoc, colIndex, rowIndex, cellContent));
128
167
  });
129
168
  sheetData.appendChild(newRow);
130
169
  rowIndex++;
131
170
  });
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);
171
+ var reference = documentUtils.getTableReference(tableData.rows[0].length - 1, tableData.rows.length + 1);
172
+ sheetsDoc.getElementsByTagName(element.dimension)[0].setAttribute(elementAttributes.reference, reference);
173
+ sheetsDoc.getElementsByTagName(element.selection)[0].setAttribute(elementAttributes.sqref, reference);
135
174
  return serializer.serializeToString(sheetsDoc);
136
175
  };
137
- exports.default = {
138
- updateTableInitialDataIfNeeded,
139
- updateSheetsInitialData,
140
- updateWorkbookInitialData,
141
- updateTablesInitialData,
142
- updateQueryTablesInitialData,
176
+ export default {
177
+ updateTableInitialDataIfNeeded: updateTableInitialDataIfNeeded,
178
+ updateSheetsInitialData: updateSheetsInitialData,
179
+ updateWorkbookInitialData: updateWorkbookInitialData,
180
+ updateTablesInitialData: updateTablesInitialData,
181
+ updateQueryTablesInitialData: updateQueryTablesInitialData,
143
182
  };