From c3929e20a7f18334e1cb036f09e00e9cf8f1d181 Mon Sep 17 00:00:00 2001 From: Alexey Matveev <34643750+AlexeyMatveev686@users.noreply.github.com> Date: Tue, 5 Feb 2019 17:44:07 +0300 Subject: [PATCH] Feature/new function (#456) * [Add function] Add new function Intersect in Api and function GetWorksheet and property Worksheet in ApiRange * [Add function] Add new function SetActive and property Active in ApiWorksheet and fucntions GetRows, Clear and property Rows in ApiRange. * Add new class ApiName * Add new class in apiBuilder and function in Workbook * Change CHANGELOG and delete comments in ApiWorksheet.Intersect. * Change Intersect, GetDefNames, GetWorksheet and getDefinedNamesWS. * Change GetDefNames, getDefinedNamesWS --- CHANGELOG.md | 7 ++ cell/apiBuilder.js | 263 ++++++++++++++++++++++++++++++++++++++++- cell/model/Workbook.js | 18 +++ 3 files changed, 287 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d26021b27..1540b7d56c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,13 @@ GetSelection Selection * Fix change active cell in selection across merge. Previously, passing through the first cell of the merge range, we fell into the merge range, even if it was not selected (through the selection of a row / column) * Fix selection when selecting row/col/all +* Add new class in apiBuilder: ApiName. +* Add new functions in ApiName: GetName, SetName, Delete, Add, GetRefersTo, SetRefersTo and properties: Name, RefersTo. +* Add new functions in ApiRange: GetDefName, GetRows, Clear, GetWorksheet and propoerties: DefName, Rows, Worksheet. +* Add new functions in ApiWorksheet: GetDefNames, SetActive and propoerties: DefNames, Active. +* Add new function in Api: Intersect. +* Add new functions in WorkBook: addDefName, getDefinedNamesWS. +* Add new function in DependencyGraph: getDefinedNamesWS. ### Presentation Editor * diff --git a/cell/apiBuilder.js b/cell/apiBuilder.js index 9e488e228c..34a8fc5b23 100644 --- a/cell/apiBuilder.js +++ b/cell/apiBuilder.js @@ -156,6 +156,15 @@ this.color = color; } + /** + * Class representing a names + * @constructor + */ + function ApiName(DefName, wb) { + this.DefName = DefName; + this.wb = wb; + } + /** * Returns a class formatted according to instructions contained in a format expression * @memberof Api @@ -306,6 +315,26 @@ return new ApiColor(AscCommonExcel.createRgbColor((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF)); }; + /** + * Returns a ApiRange object that represents the rectangular intersection of two or more ranges. If one or more ranges from a different worksheet are specified, an error will be returned. + * @memberof Api + * @param {ApiRange} Range1 The intersecting ranges. At least two Range objects must be specified. + * @param {ApiRange} Range2 The intersecting ranges. At least two Range objects must be specified. + * @returns {ApiRange | Error} + */ + Api.prototype.Intersect = function (Range1, Range2) { + if (Range1.Worksheet.Id === Range2.Worksheet.Id) { + var res = Range1.range.bbox.intersection(Range2.range.bbox); + if (!res) { + return "Ranges do not intersect."; + } else { + return new ApiRange(this.ActiveSheet.worksheet.getRange3(res.r1, res.c1, res.r2, res.c2)); + } + } else { + return new Error('Ranges should be from one worksheet.'); + } + }; + /** * Returns Visible of sheet * @memberof ApiWorksheet @@ -332,6 +361,19 @@ } }); + /** + * Makes the current sheet the active sheet. + * @memberof ApiWorksheet + */ + ApiWorksheet.prototype.SetActive = function () { + this.worksheet.workbook.setActive(this.worksheet.index); + }; + Object.defineProperty(ApiWorksheet.prototype, "Active", { + get: function () { + this.SetActive(); + } + }); + /** * Returns an object that represents the active cell * @memberof ApiWorksheet @@ -388,7 +430,9 @@ * @param {string | number} value */ ApiWorksheet.prototype.GetRows = function (value) { - if (typeof value == "number" || value.indexOf(':') == -1) { + if (typeof value === "undefined") { + return this.Rows; + } else if (typeof value == "number" || value.indexOf(':') == -1) { value = parseInt(value); if (value > 0) { value --; @@ -709,6 +753,29 @@ } }); + /** + * Returns array a ApiName. + * @typeofeditors ["CSE"] + * @memberof ApiWorksheet + * @returns {ApiName} + */ + ApiWorksheet.prototype.GetDefNames = function () { + var res = this.worksheet.workbook.getDefinedNamesWS(this.worksheet.getId()); + var name = []; + if (!res.length) { + return [new ApiName(undefined, this.worksheet.workbook)] + } + for (var i = 0; i < res.length; i++) { + name.push(new ApiName(res[i])); + } + return name; + }; + Object.defineProperty(ApiWorksheet.prototype, "DefNames", { + get: function () { + return this.GetDefNames(); + } + }); + /** * Add Hyperlink * @memberof ApiWorksheet @@ -1079,6 +1146,41 @@ } }); + /** + * Clears the entire object. + * @typeofeditors ["CSE"] + * @memberof ApiRange + */ + ApiRange.prototype.Clear = function () { + this.range.cleanAll(); + }; + + /** + * Returns a Range object that represents the rows in the specified range. + * @typeofeditors ["CSE"] + * @memberof ApiRange + * @param {number} number - The number of the row. * + * @returns {ApiRange} + */ + ApiRange.prototype.GetRows = function (number) { + if (typeof number === "undefined") { + var r1 = this.range.bbox.r1; + var r2 = this.range.bbox.r2; + return new ApiWorksheet(this.range.worksheet).GetRows(r1 + ":" + r2); + // return new ApiWorksheet(this.range.worksheet).Rows; // return all rows from current sheet + } else if ( (number >= this.range.bbox.r1) && (number <= this.range.bbox.r2) ) { + return new ApiWorksheet(this.range.worksheet).GetRows(number); + } else { + var bbox = this.range.bbox; + return new ApiRange(this.range.worksheet.getRange3(number, bbox.c1, number, bbox.c2)); + } + }; + Object.defineProperty(ApiRange.prototype, "Rows", { + get: function () { + return this.GetRows(); + } + }); + /** * Set cell offset * @memberof ApiRange @@ -1676,6 +1778,42 @@ } }; + /** + * Returns a Worksheet object that represents the worksheet containing the specified range. Read-only. + * @typeofeditors ["CSE"] + * @memberof ApiRange + */ + ApiRange.prototype.GetWorksheet = function () { + return new ApiWorksheet(this.range.worksheet); + }; + Object.defineProperty(ApiRange.prototype, "Worksheet", { + get: function () { + return this.GetWorksheet(); + } + }); + + /** + * Returns a ApiName. + * @typeofeditors ["CSE"] + * @memberof ApiRange + * @returns {ApiName} + */ + ApiRange.prototype.GetDefName = function () { + var defName = this.range.worksheet.getName() + "!" + this.range.bbox.getAbsName(); + var SheetId = this.range.worksheet.getId(); + defName = this.range.worksheet.workbook.findDefinesNames(defName, SheetId); + if (defName) { + // defName = this.range.worksheet.workbook.getDefinedName({Name : defName}); + defName = this.range.worksheet.workbook.getDefinesNames(defName, SheetId); + } + return new ApiName(defName, this.range.worksheet.workbook); + }; + Object.defineProperty(ApiRange.prototype, "DefName", { + get: function () { + return this.GetDefName(); + } + }); + //------------------------------------------------------------------------------------------------------------------ // // ApiDrawing @@ -2070,7 +2208,116 @@ ApiColor.prototype.GetClassType = function () { return "color"; }; + + //------------------------------------------------------------------------------------------------------------------ + // + // ApiName + // + //------------------------------------------------------------------------------------------------------------------ + /** + * Returns a String value representing the name of the object. + * @typeofeditors ["CSE"] + * @memberof ApiName + * @returns {string} + */ + ApiName.prototype.GetName = function () { + if (this.DefName) { + return this.DefName.name + } else { + return this.DefName; + } + }; + + /** + * Sets a String value representing the name of the object. + * @typeofeditors ["CSE"] + * @param {string} Name // new name for range + * @memberof ApiName + * @returns {Error} + */ + ApiName.prototype.SetName = function (Name) { + if (!Name || typeof Name !== 'string' || !this.DefName) { + return new Error('Invalid name or Defname is undefined.'); + } + var res = this.DefName.wb.checkDefName(Name); + if (!res.status) { + return new Error('Invalid name.'); // invalid name + } + var oldName = this.DefName.getAscCDefName(false); + var newName = this.DefName.getAscCDefName(false); + newName.Name = Name; + this.DefName.wb.editDefinesNames(oldName, newName); + }; + + Object.defineProperty(ApiName.prototype, "Name", { + get: function () { + return this.GetName(); + }, + set: function (Name) { + return this.SetName(Name); + } + }); + + /** + * Deletes the DefName object. + * @typeofeditors ["CSE"] + * @memberof ApiName + */ + ApiName.prototype.Delete = function () { + this.DefName.wb.delDefinesNames(this.DefName.getAscCDefName(false)); + }; + + /** + * Defines a new name for a range of cells. + * @typeofeditors ["CSE"] + * @memberof ApiName + * @returns {Error} + */ + ApiName.prototype.Add = function (name, ref, sheetId, hidden) { + var wb = (this.DefName) ? this.DefName.wb : this.wb; + var res = wb.checkDefName(name); + if (!res.status) { + return new Error('Invalid name.'); + } + res = wb.oApi.asc_checkDataRange(Asc.c_oAscSelectionDialogType.Chart, ref, false); + if (res === Asc.c_oAscError.ID.DataRangeError) { + return new Error('Invalid range.'); + } + if (sheetId) { + sheetId = (wb.getWorksheetById(sheetId)) ? sheetId : undefined; + } + wb.addDefName(name, ref, sheetId, hidden, false) + }; + + /** + * Sets the formula that the name is defined to refer to. + * @typeofeditors ["CSE"] + * @param {string} Ref // new refers to defined name + * @memberof ApiName + */ + ApiName.prototype.SetRefersTo = function (Ref) { + this.DefName.setRef(Ref); + }; + + /** + * Returns the formula that the name is defined to refer to. + * @typeofeditors ["CSE"] + * @memberof ApiName + * @returns {string} + */ + ApiName.prototype.GetRefersTo = function () { + return (this.DefName) ? this.DefName.ref : this.DefName; + }; + + Object.defineProperty(ApiName.prototype, "RefersTo", { + get: function () { + return this.GetRefersTo(); + }, + set: function (Ref) { + return this.SetRefersTo(Ref); + } + }); Api.prototype["Format"] = Api.prototype.Format; Api.prototype["AddSheet"] = Api.prototype.AddSheet; @@ -2084,9 +2331,11 @@ Api.prototype["CreateNewHistoryPoint"] = Api.prototype.CreateNewHistoryPoint; Api.prototype["CreateColorFromRGB"] = Api.prototype.CreateColorFromRGB; Api.prototype["CreateColorByName"] = Api.prototype.CreateColorByName; + Api.prototype["Intersect"] = Api.prototype.Intersect; ApiWorksheet.prototype["GetVisible"] = ApiWorksheet.prototype.GetVisible; ApiWorksheet.prototype["SetVisible"] = ApiWorksheet.prototype.SetVisible; + ApiWorksheet.prototype["SetActive"] = ApiWorksheet.prototype.SetActive; ApiWorksheet.prototype["GetActiveCell"] = ApiWorksheet.prototype.GetActiveCell; ApiWorksheet.prototype["GetSelection"] = ApiWorksheet.prototype.GetSelection; ApiWorksheet.prototype["GetCells"] = ApiWorksheet.prototype.GetCells; @@ -2113,6 +2362,7 @@ ApiWorksheet.prototype["GetBottomMargin"] = ApiWorksheet.prototype.GetBottomMargin; ApiWorksheet.prototype["SetPageOrientation"] = ApiWorksheet.prototype.SetPageOrientation; ApiWorksheet.prototype["GetPageOrientation"] = ApiWorksheet.prototype.GetPageOrientation; + ApiWorksheet.prototype["GetDefNames"] = ApiWorksheet.prototype.GetDefNames; ApiWorksheet.prototype["SetHyperlink"] = ApiWorksheet.prototype.SetHyperlink; ApiWorksheet.prototype["AddChart"] = ApiWorksheet.prototype.AddChart; ApiWorksheet.prototype["AddShape"] = ApiWorksheet.prototype.AddShape; @@ -2121,6 +2371,8 @@ ApiRange.prototype["GetRow"] = ApiRange.prototype.GetRow; ApiRange.prototype["GetCol"] = ApiRange.prototype.GetCol; + ApiRange.prototype["Clear"] = ApiRange.prototype.Clear; + ApiRange.prototype["GetRows"] = ApiRange.prototype.GetRows; ApiRange.prototype["SetOffset"] = ApiRange.prototype.SetOffset; ApiRange.prototype["GetAddress"] = ApiRange.prototype.GetAddress; ApiRange.prototype["GetCount"] = ApiRange.prototype.GetCount; @@ -2151,6 +2403,8 @@ ApiRange.prototype["UnMerge"] = ApiRange.prototype.UnMerge; ApiRange.prototype["ForEach"] = ApiRange.prototype.ForEach; ApiRange.prototype["AddComment"] = ApiRange.prototype.AddComment; + ApiRange.prototype["GetWorksheet"] = ApiRange.prototype.GetWorksheet; + ApiRange.prototype["GetDefName"] = ApiRange.prototype.GetDefName; ApiDrawing.prototype["GetClassType"] = ApiDrawing.prototype.GetClassType; @@ -2195,6 +2449,13 @@ ApiColor.prototype["GetClassType"] = ApiColor.prototype.GetClassType; + ApiName.prototype["GetName"] = ApiName.prototype.GetName; + ApiName.prototype["SetName"] = ApiName.prototype.SetName; + ApiName.prototype["Delete"] = ApiName.prototype.Delete; + ApiName.prototype["Add"] = ApiName.prototype.Add; + ApiName.prototype["GetRefersTo"] = ApiName.prototype.GetRefersTo; + ApiName.prototype["SetRefersTo"] = ApiName.prototype.SetRefersTo; + function private_SetCoords(oDrawing, oWorksheet, nExtX, nExtY, nFromCol, nColOffset, nFromRow, nRowOffset, pos){ oDrawing.x = 0; oDrawing.y = 0; diff --git a/cell/model/Workbook.js b/cell/model/Workbook.js index cfea80523e..ad4c841475 100644 --- a/cell/model/Workbook.js +++ b/cell/model/Workbook.js @@ -698,6 +698,18 @@ } return names.sort(sort); }, + getDefinedNamesWS: function(sheetId) { + var names = []; + + function getNames(defName) { + if (defName.ref) { + names.push(defName); + } + } + + this._foreachDefNameSheet(sheetId, getNames); + return names; + }, addDefNameOpen: function(name, ref, sheetIndex, hidden, isTable) { var sheetId = this.wb.getSheetIdByIndex(sheetIndex); var isXLNM = null; @@ -2405,6 +2417,12 @@ Workbook.prototype.getDefinedNamesWB = function (defNameListId, bLocale) { return this.dependencyFormulas.getDefinedNamesWB(defNameListId, bLocale); }; + Workbook.prototype.getDefinedNamesWS = function (sheetId) { + return this.dependencyFormulas.getDefinedNamesWS(sheetId); + }; + Workbook.prototype.addDefName = function (name, ref, sheetId, hidden, isTable) { + return this.dependencyFormulas.addDefName(name, ref, sheetId, hidden, isTable); + }; Workbook.prototype.getDefinesNames = function ( name, sheetId ) { return this.dependencyFormulas.getDefNameByName( name, sheetId ); };