@microsoft/connected-workbooks 3.1.2-beta.2 → 3.2.0-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.
@@ -151,5 +151,7 @@ exports.headers = {
151
151
  exports.OFU = {
152
152
  ViewUrl: "https://view.officeapps.live.com/op/view.aspx?src=http://connectedWorkbooks.excel/",
153
153
  PostUrl: "https://view.officeapps.live.com/op/viewpost.aspx?src=http://connectedWorkbooks.excel/",
154
- allowTyping: "AllowTyping",
154
+ AllowTyping: "AllowTyping",
155
+ WdOrigin: "wdOrigin",
156
+ OpenInExcelOririgin: "OpenInExcel",
155
157
  };
@@ -97,52 +97,54 @@ var getDataMashupFile = function (zip) { return __awaiter(void 0, void 0, void 0
97
97
  }
98
98
  });
99
99
  }); };
100
- var getCustomXmlFile = function (zip, url, encoding) {
101
- if (encoding === void 0) { encoding = "utf16le"; }
102
- return __awaiter(void 0, void 0, void 0, function () {
103
- var parser, itemsArray, found, path, xmlString, value, i, xmlValue, doc;
104
- var _a, _b;
105
- return __generator(this, function (_c) {
106
- switch (_c.label) {
107
- case 0:
108
- parser = new xmldom_qsa_1.DOMParser();
109
- return [4 /*yield*/, zip.file(/customXml\/item\d.xml/)];
110
- case 1:
111
- itemsArray = _c.sent();
112
- if (!itemsArray || itemsArray.length === 0) {
113
- throw new Error("No customXml files were found!");
114
- }
115
- found = false;
116
- value = null;
117
- i = 1;
118
- _c.label = 2;
119
- case 2:
120
- if (!(i <= itemsArray.length)) return [3 /*break*/, 5];
121
- path = (0, generators_1.generateCustomXmlFilePath)(i);
122
- return [4 /*yield*/, ((_a = zip.file(path)) === null || _a === void 0 ? void 0 : _a.async("uint8array"))];
123
- case 3:
124
- xmlValue = _c.sent();
125
- if (xmlValue === undefined) {
126
- return [3 /*break*/, 5];
127
- }
128
- xmlString = buffer_1.Buffer.from(xmlValue)
129
- .toString(encoding)
130
- .replace(/^\ufeff/, "");
131
- doc = parser.parseFromString(xmlString, "text/xml");
132
- found = ((_b = doc === null || doc === void 0 ? void 0 : doc.documentElement) === null || _b === void 0 ? void 0 : _b.namespaceURI) === url;
133
- if (found) {
134
- value = doc.documentElement.textContent;
135
- return [3 /*break*/, 5];
136
- }
137
- _c.label = 4;
138
- case 4:
139
- i++;
140
- return [3 /*break*/, 2];
141
- case 5: return [2 /*return*/, { found: found, path: path, xmlString: xmlString, value: value }];
142
- }
143
- });
100
+ var getCustomXmlFile = function (zip, url) { return __awaiter(void 0, void 0, void 0, function () {
101
+ var parser, itemsArray, found, path, xmlString, value, i, xmlValue, buffer, encoding, doc;
102
+ var _a, _b;
103
+ return __generator(this, function (_c) {
104
+ switch (_c.label) {
105
+ case 0:
106
+ parser = new xmldom_qsa_1.DOMParser();
107
+ return [4 /*yield*/, zip.file(/customXml\/item\d.xml/)];
108
+ case 1:
109
+ itemsArray = _c.sent();
110
+ if (!itemsArray || itemsArray.length === 0) {
111
+ throw new Error("No customXml files were found!");
112
+ }
113
+ found = false;
114
+ value = null;
115
+ i = 1;
116
+ _c.label = 2;
117
+ case 2:
118
+ if (!(i <= itemsArray.length)) return [3 /*break*/, 5];
119
+ path = (0, generators_1.generateCustomXmlFilePath)(i);
120
+ return [4 /*yield*/, ((_a = zip.file(path)) === null || _a === void 0 ? void 0 : _a.async("uint8array"))];
121
+ case 3:
122
+ xmlValue = _c.sent();
123
+ if (xmlValue === undefined) {
124
+ return [3 /*break*/, 5];
125
+ }
126
+ buffer = buffer_1.Buffer.from(xmlValue);
127
+ encoding = detectEncoding(xmlValue);
128
+ if (!encoding) {
129
+ throw new Error("Failed to detect xml encoding");
130
+ }
131
+ xmlString = buffer
132
+ .toString(encoding)
133
+ .replace(/^\ufeff/, "");
134
+ doc = parser.parseFromString(xmlString, "text/xml");
135
+ found = ((_b = doc === null || doc === void 0 ? void 0 : doc.documentElement) === null || _b === void 0 ? void 0 : _b.namespaceURI) === url;
136
+ if (found) {
137
+ value = doc.documentElement.textContent;
138
+ return [3 /*break*/, 5];
139
+ }
140
+ _c.label = 4;
141
+ case 4:
142
+ i++;
143
+ return [3 /*break*/, 2];
144
+ case 5: return [2 /*return*/, { found: found, path: path, xmlString: xmlString, value: value }];
145
+ }
144
146
  });
145
- };
147
+ }); };
146
148
  var queryNameHasInvalidChars = function (queryName) {
147
149
  var invalidQueryNameChars = ['"', "."];
148
150
  // Control characters as defined in Unicode
@@ -167,6 +169,22 @@ var validateQueryName = function (queryName) {
167
169
  throw new Error(constants_1.EmptyQueryNameErr);
168
170
  }
169
171
  };
172
+ var detectEncoding = function (xmlBytes) {
173
+ if (!xmlBytes) {
174
+ return null;
175
+ }
176
+ if (xmlBytes.length >= 3 && xmlBytes[0] === 0xEF && xmlBytes[1] === 0xBB && xmlBytes[2] === 0xBF) {
177
+ return 'utf-8';
178
+ }
179
+ if (xmlBytes.length >= 3 && xmlBytes[0] === 0xFF && xmlBytes[1] === 0xFE) {
180
+ return 'utf-16le';
181
+ }
182
+ if (xmlBytes.length >= 3 && xmlBytes[0] === 0xFE && xmlBytes[1] === 0xFF) {
183
+ return 'utf-16be';
184
+ }
185
+ // Default to utf-8, that not required a BOM for encoding.
186
+ return 'utf-8';
187
+ };
170
188
  exports.default = {
171
189
  getBase64: getBase64,
172
190
  setBase64: setBase64,
@@ -4,3 +4,4 @@ export declare const generateTableWorkbookFromHtml: (htmlTable: HTMLTableElement
4
4
  export declare const generateTableWorkbookFromGrid: (grid: Grid, fileConfigs?: FileConfigs) => Promise<Blob>;
5
5
  export declare const downloadWorkbook: (file: Blob, filename: string) => void;
6
6
  export declare const openInExcelWeb: (file: Blob, filename?: string, allowTyping?: boolean) => Promise<void>;
7
+ export declare const getExcelForWebWorkbookUrl: (file: Blob, filename?: string, allowTyping?: boolean) => Promise<string>;
@@ -41,7 +41,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
41
41
  return (mod && mod.__esModule) ? mod : { "default": mod };
42
42
  };
43
43
  Object.defineProperty(exports, "__esModule", { value: true });
44
- exports.openInExcelWeb = exports.downloadWorkbook = exports.generateTableWorkbookFromGrid = exports.generateTableWorkbookFromHtml = exports.generateSingleQueryWorkbook = void 0;
44
+ exports.getExcelForWebWorkbookUrl = exports.openInExcelWeb = exports.downloadWorkbook = exports.generateTableWorkbookFromGrid = exports.generateTableWorkbookFromHtml = exports.generateSingleQueryWorkbook = void 0;
45
45
  var jszip_1 = __importDefault(require("jszip"));
46
46
  var utils_1 = require("./utils");
47
47
  var workbookTemplate_1 = require("./workbookTemplate");
@@ -170,18 +170,41 @@ var downloadWorkbook = function (file, filename) {
170
170
  };
171
171
  exports.downloadWorkbook = downloadWorkbook;
172
172
  var openInExcelWeb = function (file, filename, allowTyping) { return __awaiter(void 0, void 0, void 0, function () {
173
- var fileContent, fileNameGuid, allowTypingParam, response, error_1;
173
+ var url, error_1;
174
174
  return __generator(this, function (_a) {
175
175
  switch (_a.label) {
176
176
  case 0:
177
- if (!(file.size > 0)) return [3 /*break*/, 4];
177
+ _a.trys.push([0, 2, , 3]);
178
+ return [4 /*yield*/, (0, exports.getExcelForWebWorkbookUrl)(file, filename, allowTyping)];
179
+ case 1:
180
+ url = _a.sent();
181
+ window.open(url, "_blank");
182
+ return [3 /*break*/, 3];
183
+ case 2:
184
+ error_1 = _a.sent();
185
+ console.error("An error occurred:", error_1);
186
+ return [3 /*break*/, 3];
187
+ case 3: return [2 /*return*/];
188
+ }
189
+ });
190
+ }); };
191
+ exports.openInExcelWeb = openInExcelWeb;
192
+ var getExcelForWebWorkbookUrl = function (file, filename, allowTyping) { return __awaiter(void 0, void 0, void 0, function () {
193
+ var fileContent, fileNameGuid, allowTypingParam, response, error_2;
194
+ return __generator(this, function (_a) {
195
+ switch (_a.label) {
196
+ case 0:
197
+ // Check if the file exists
198
+ if (file.size < 0) {
199
+ throw new Error("File is empty");
200
+ }
178
201
  fileContent = file;
179
202
  fileNameGuid = new Date().getTime().toString() + (filename ? "_" + filename : "") + ".xlsx";
180
203
  allowTypingParam = allowTyping ? 1 : 0;
181
204
  _a.label = 1;
182
205
  case 1:
183
206
  _a.trys.push([1, 3, , 4]);
184
- return [4 /*yield*/, fetch("".concat(constants_1.OFU.PostUrl).concat(fileNameGuid), {
207
+ return [4 /*yield*/, fetch("".concat(constants_1.OFU.PostUrl).concat(fileNameGuid, "&").concat(constants_1.OFU.WdOrigin, "=").concat(constants_1.OFU.OpenInExcelOririgin), {
185
208
  method: "POST",
186
209
  headers: constants_1.headers,
187
210
  body: fileContent,
@@ -191,18 +214,17 @@ var openInExcelWeb = function (file, filename, allowTyping) { return __awaiter(v
191
214
  // Check if the response is successful
192
215
  if (response.ok) {
193
216
  // if upload was successful - open the file in a new tab
194
- window.open("".concat(constants_1.OFU.ViewUrl).concat(fileNameGuid, "&").concat(constants_1.OFU.allowTyping, "=").concat(allowTypingParam), "_blank");
217
+ return [2 /*return*/, "".concat(constants_1.OFU.ViewUrl).concat(fileNameGuid, "&").concat(constants_1.OFU.AllowTyping, "=").concat(allowTypingParam, "&").concat(constants_1.OFU.WdOrigin, "=").concat(constants_1.OFU.OpenInExcelOririgin)];
195
218
  }
196
219
  else {
197
220
  throw new Error("File upload failed. Status code: ".concat(response.status));
198
221
  }
199
222
  return [3 /*break*/, 4];
200
223
  case 3:
201
- error_1 = _a.sent();
202
- console.error("An error occurred:", error_1);
203
- return [3 /*break*/, 4];
224
+ error_2 = _a.sent();
225
+ throw new Error("An error occurred: ".concat(error_2));
204
226
  case 4: return [2 /*return*/];
205
227
  }
206
228
  });
207
229
  }); };
208
- exports.openInExcelWeb = openInExcelWeb;
230
+ exports.getExcelForWebWorkbookUrl = getExcelForWebWorkbookUrl;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@microsoft/connected-workbooks",
3
- "version": "3.1.2-beta.2",
3
+ "version": "3.2.0-beta",
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",