@microsoft/connected-workbooks
Version:
Microsoft backed, Excel advanced xlsx workbook generation JavaScript library
135 lines (134 loc) • 8.29 kB
JavaScript
;
// 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 xmlInnerPartsUtils_1 = __importDefault(require("./xmlInnerPartsUtils"));
var tableUtils_1 = __importDefault(require("./tableUtils"));
var updateWorkbookDataAndConfigurations = function (zip, fileConfigs, tableData, updateQueryTable) {
if (updateQueryTable === void 0) { updateQueryTable = false; }
return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, xmlInnerPartsUtils_1.default.updateDocProps(zip, fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.docProps)];
case 1:
_a.sent();
if (!((fileConfigs === null || fileConfigs === void 0 ? void 0 : fileConfigs.templateFile) === undefined)) return [3 /*break*/, 3];
// If we are using our base template, we need to clear label info
return [4 /*yield*/, xmlInnerPartsUtils_1.default.clearLabelInfo(zip)];
case 2:
// If we are using our base template, we need to clear label info
_a.sent();
_a.label = 3;
case 3: return [4 /*yield*/, tableUtils_1.default.updateTableInitialDataIfNeeded(zip, tableData, updateQueryTable)];
case 4:
_a.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.base64NotFoundErr);
}
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) { return __awaiter(void 0, void 0, void 0, function () {
var connectionsXmlString, _a, connectionId, connectionXmlFileString, sharedStringsXmlString, _b, sharedStringIndex, newSharedStrings, 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.connectionsNotFoundErr);
}
_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.sharedStringsNotFoundErr);
}
_b = xmlInnerPartsUtils_1.default.updateSharedStrings(sharedStringsXmlString, queryName), sharedStringIndex = _b.sharedStringIndex, newSharedStrings = _b.newSharedStrings;
zip.file(constants_1.sharedStringsXmlPath, newSharedStrings);
return [4 /*yield*/, ((_e = zip.file(constants_1.sheetsXmlPath)) === null || _e === void 0 ? void 0 : _e.async(constants_1.textResultType))];
case 3:
sheetsXmlString = _f.sent();
if (sheetsXmlString === undefined) {
throw new Error(constants_1.sheetsNotFoundErr);
}
worksheetString = xmlInnerPartsUtils_1.default.updateWorksheet(sheetsXmlString, sharedStringIndex.toString());
zip.file(constants_1.sheetsXmlPath, worksheetString);
// Update tables
return [4 /*yield*/, xmlInnerPartsUtils_1.default.updatePivotTablesandQueryTables(zip, queryName, refreshOnOpen, connectionId)];
case 4:
// Update tables
_f.sent();
return [2 /*return*/];
}
});
}); };
exports.default = {
updateWorkbookDataAndConfigurations: updateWorkbookDataAndConfigurations,
updateWorkbookPowerQueryDocument: updateWorkbookPowerQueryDocument,
updateWorkbookSingleQueryAttributes: updateWorkbookSingleQueryAttributes,
};