@microsoft/connected-workbooks
Version:
Microsoft backed, Excel advanced xlsx workbook generation JavaScript library
249 lines (248 loc) • 15.3 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
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;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var constants_1 = require("./constants");
var mashupDocumentParser_1 = require("./mashupDocumentParser");
var pqUtils_1 = __importDefault(require("./pqUtils"));
var tableUtils_1 = __importDefault(require("./tableUtils"));
var xmlInnerPartsUtils_1 = __importDefault(require("./xmlInnerPartsUtils"));
var documentUtils_1 = __importDefault(require("./documentUtils"));
var addCustomXMLToWorkbook = function (zip) { return __awaiter(void 0, void 0, void 0, function () {
var customXmlItemNumber, backupData, contentTypesPath, workbookRelsPath, _a, _b, _c, _d, error_1, _i, _e, _f, filePath, content;
var _g, _h;
return __generator(this, function (_j) {
switch (_j.label) {
case 0: return [4 /*yield*/, xmlInnerPartsUtils_1.default.isCustomXmlExists(zip)];
case 1:
if (_j.sent()) {
return [2 /*return*/, true];
}
return [4 /*yield*/, xmlInnerPartsUtils_1.default.getCustomXmlItemNumber(zip)];
case 2:
customXmlItemNumber = _j.sent();
backupData = {};
contentTypesPath = "[Content_Types].xml";
workbookRelsPath = "xl/_rels/workbook.xml.rels";
_j.label = 3;
case 3:
_j.trys.push([3, 8, , 9]);
// Backup existing files
_a = backupData;
_b = contentTypesPath;
return [4 /*yield*/, ((_g = zip.file(contentTypesPath)) === null || _g === void 0 ? void 0 : _g.async("text"))];
case 4:
// Backup existing files
_a[_b] = (_j.sent()) || null;
_c = backupData;
_d = workbookRelsPath;
return [4 /*yield*/, ((_h = zip.file(workbookRelsPath)) === null || _h === void 0 ? void 0 : _h.async("text"))];
case 5:
_c[_d] = (_j.sent()) || null;
// Perform modifications
return [4 /*yield*/, xmlInnerPartsUtils_1.default.addToContentType(zip, customXmlItemNumber.toString())];
case 6:
// Perform modifications
_j.sent();
return [4 /*yield*/, xmlInnerPartsUtils_1.default.addCustomXmlToRels(zip, customXmlItemNumber.toString())];
case 7:
_j.sent();
// Adding the custom XML files
zip.file(constants_1.customXML.itemPathTemplate(customXmlItemNumber), constants_1.customXML.customXMLItemContent);
zip.file(constants_1.customXML.itemPropsPathTemplate(customXmlItemNumber), constants_1.customXML.customXMLItemPropsContent);
zip.file(constants_1.customXML.itemRelsPathTemplate(customXmlItemNumber), constants_1.customXML.customXMLRelationships(customXmlItemNumber));
return [3 /*break*/, 9];
case 8:
error_1 = _j.sent();
// Rollback: restore backed up files
for (_i = 0, _e = Object.entries(backupData); _i < _e.length; _i++) {
_f = _e[_i], filePath = _f[0], content = _f[1];
if (content !== null) {
zip.file(filePath, content);
}
else {
// If file didn't exist before, remove it
zip.remove(filePath);
}
}
// Remove any custom XML files that might have been added
zip.remove(constants_1.customXML.itemPathTemplate(customXmlItemNumber));
zip.remove(constants_1.customXML.itemPropsPathTemplate(customXmlItemNumber));
zip.remove(constants_1.customXML.itemRelsPathTemplate(customXmlItemNumber));
return [2 /*return*/, false];
case 9: return [2 /*return*/, true];
}
});
}); };
var updateWorkbookDataAndConfigurations = function (zip, fileConfigs, tableData, updateQueryTable) {
if (updateQueryTable === void 0) { updateQueryTable = false; }
return __awaiter(void 0, void 0, void 0, function () {
var sheetName, tablePath, sheetPath, templateSettings, sheetLocation, _a, cellRangeRef, _b, row, column, endColumn, endRow;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
sheetName = constants_1.defaults.sheetName;
tablePath = constants_1.tableXmlPath;
sheetPath = constants_1.sheetsXmlPath;
if (!((fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateFile) !== undefined)) return [3 /*break*/, 4];
templateSettings = fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateSettings;
if (!((templateSettings === null || templateSettings === void 0 ? void 0 : templateSettings.sheetName) !== undefined)) return [3 /*break*/, 2];
return [4 /*yield*/, xmlInnerPartsUtils_1.default.getSheetPathByNameFromZip(zip, templateSettings.sheetName)];
case 1:
sheetLocation = _c.sent();
sheetName = templateSettings.sheetName;
sheetPath = "xl/" + sheetLocation;
_c.label = 2;
case 2:
if (!((templateSettings === null || templateSettings === void 0 ? void 0 : templateSettings.tableName) !== undefined)) return [3 /*break*/, 4];
_a = constants_1.tablesFolderPath;
return [4 /*yield*/, xmlInnerPartsUtils_1.default.findTablePathFromZip(zip, templateSettings === null || templateSettings === void 0 ? void 0 : templateSettings.tableName)];
case 3:
tablePath = _a + (_c.sent());
_c.label = 4;
case 4:
cellRangeRef = "A1";
if (!((fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateFile) !== undefined)) return [3 /*break*/, 6];
return [4 /*yield*/, xmlInnerPartsUtils_1.default.getReferenceFromTable(zip, tablePath)];
case 5:
cellRangeRef = _c.sent();
_c.label = 6;
case 6:
if (tableData) {
_b = documentUtils_1.default.GetStartPosition(cellRangeRef), row = _b.row, column = _b.column;
endColumn = column - 1 + tableData.columnNames.length;
endRow = row - 1 + tableData.rows.length;
// Extend the cell range to include the entire table span
cellRangeRef += ":".concat(documentUtils_1.default.getCellReferenceRelative(endColumn - 1, endRow + 1));
}
return [4 /*yield*/, xmlInnerPartsUtils_1.default.updateDocProps(zip, fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.docProps)];
case 7:
_c.sent();
if (!((fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateFile) === undefined)) return [3 /*break*/, 9];
// If we are using our base template, we need to clear label info
return [4 /*yield*/, xmlInnerPartsUtils_1.default.clearLabelInfo(zip)];
case 8:
// If we are using our base template, we need to clear label info
_c.sent();
_c.label = 9;
case 9: return [4 /*yield*/, tableUtils_1.default.updateTableInitialDataIfNeeded(zip, cellRangeRef, sheetPath, tablePath, sheetName, tableData, updateQueryTable)];
case 10:
_c.sent();
return [4 /*yield*/, addCustomXMLToWorkbook(zip)];
case 11:
_c.sent();
return [2 /*return*/];
}
});
});
};
var updateWorkbookPowerQueryDocument = function (zip, queryName, queryMashupDoc) { return __awaiter(void 0, void 0, void 0, function () {
var old_base64, new_base64;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, pqUtils_1.default.getBase64(zip)];
case 1:
old_base64 = _a.sent();
if (!old_base64) {
throw new Error(constants_1.Errors.base64NotFound);
}
return [4 /*yield*/, (0, mashupDocumentParser_1.replaceSingleQuery)(old_base64, queryName, queryMashupDoc)];
case 2:
new_base64 = _a.sent();
return [4 /*yield*/, pqUtils_1.default.setBase64(zip, new_base64)];
case 3:
_a.sent();
return [2 /*return*/];
}
});
}); };
var updateWorkbookSingleQueryAttributes = function (zip, queryName, refreshOnOpen, sheetName) { return __awaiter(void 0, void 0, void 0, function () {
var connectionsXmlString, _a, connectionId, connectionXmlFileString, sharedStringsXmlString, _b, sharedStringIndex, newSharedStrings, sheetPath, sheetLocation, sheetsXmlString, worksheetString;
var _c, _d, _e;
return __generator(this, function (_f) {
switch (_f.label) {
case 0: return [4 /*yield*/, ((_c = zip.file(constants_1.connectionsXmlPath)) === null || _c === void 0 ? void 0 : _c.async(constants_1.textResultType))];
case 1:
connectionsXmlString = _f.sent();
if (connectionsXmlString === undefined) {
throw new Error(constants_1.Errors.connectionsNotFound);
}
_a = xmlInnerPartsUtils_1.default.updateConnections(connectionsXmlString, queryName, refreshOnOpen), connectionId = _a.connectionId, connectionXmlFileString = _a.connectionXmlFileString;
zip.file(constants_1.connectionsXmlPath, connectionXmlFileString);
return [4 /*yield*/, ((_d = zip.file(constants_1.sharedStringsXmlPath)) === null || _d === void 0 ? void 0 : _d.async(constants_1.textResultType))];
case 2:
sharedStringsXmlString = _f.sent();
if (sharedStringsXmlString === undefined) {
throw new Error(constants_1.Errors.sharedStringsNotFound);
}
_b = xmlInnerPartsUtils_1.default.updateSharedStrings(sharedStringsXmlString, queryName), sharedStringIndex = _b.sharedStringIndex, newSharedStrings = _b.newSharedStrings;
zip.file(constants_1.sharedStringsXmlPath, newSharedStrings);
sheetPath = constants_1.sheetsXmlPath;
if (!(sheetName !== undefined)) return [3 /*break*/, 4];
return [4 /*yield*/, xmlInnerPartsUtils_1.default.getSheetPathByNameFromZip(zip, sheetName)];
case 3:
sheetLocation = _f.sent();
sheetPath = "xl/" + sheetLocation;
_f.label = 4;
case 4: return [4 /*yield*/, ((_e = zip.file(sheetPath)) === null || _e === void 0 ? void 0 : _e.async(constants_1.textResultType))];
case 5:
sheetsXmlString = _f.sent();
if (sheetsXmlString === undefined) {
throw new Error(constants_1.Errors.sheetsNotFound);
}
worksheetString = xmlInnerPartsUtils_1.default.updateWorksheet(sheetsXmlString, sharedStringIndex.toString());
zip.file(sheetPath, worksheetString);
// Update tables
return [4 /*yield*/, xmlInnerPartsUtils_1.default.updatePivotTablesandQueryTables(zip, queryName, refreshOnOpen, connectionId)];
case 6:
// Update tables
_f.sent();
return [2 /*return*/];
}
});
}); };
exports.default = {
updateWorkbookDataAndConfigurations: updateWorkbookDataAndConfigurations,
updateWorkbookPowerQueryDocument: updateWorkbookPowerQueryDocument,
updateWorkbookSingleQueryAttributes: updateWorkbookSingleQueryAttributes,
addCustomXMLToWorkbook: addCustomXMLToWorkbook
};