Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[se]Fix bug 46174 #4796

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cell/model/FormulaObjects/lookupandreferenceFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,7 @@ function (window, undefined) {
found_operand = new cRef(o.real_str ? o.real_str.toUpperCase() : o.operand_str.toUpperCase(), ws);
} else if (parserHelp.isName.call(o, o.Formula, o.pCurrPos)) {
found_operand = new cName(o.operand_str, ws);
} else if (_tableTMP = parserHelp.isTable.call(o, o.Formula, o.pCurrPos)) {
} else if (_tableTMP = parserHelp.isTable.call(o, o, o.pCurrPos)) {
found_operand = AscCommonExcel.cStrucTable.prototype.createFromVal(_tableTMP, wb, ws);

if (found_operand.type === cElementType.error) {
Expand Down
28 changes: 20 additions & 8 deletions cell/model/FormulaObjects/parserFormula.js
Original file line number Diff line number Diff line change
Expand Up @@ -2253,19 +2253,30 @@ parserHelp.setDigitSeparator(AscCommon.g_oDefaultCultureInfo.NumberDecimalSepara

this.isDynamic = false;//#This row
this.area = null;

this.shortName = null;// Short name can only be used inside the table cell: "=Table[Column]" === "=[Column]"
}

cStrucTable.prototype = Object.create(cBaseType.prototype);
cStrucTable.prototype.constructor = cStrucTable;
cStrucTable.prototype.type = cElementType.table;
cStrucTable.prototype.createFromVal = function (val, wb, ws, tablesMap) {
var res = new cStrucTable(val[0], wb, ws);
let res = new cStrucTable(val[0], wb, ws);
if (tablesMap && tablesMap[val["tableName"]]) {
val["tableName"] = tablesMap[val["tableName"]];
}
if (res._parseVal(val)) {
res._updateArea(null, false);
}
if (val["isCellInTable"]) {
res.isCellInTable = true;
}
if (val["shortName"]) {
// remove tableName in value string
res.value = val["shortName"];
res.shortName = val["shortName"];
}

return (res.area && res.area.type != cElementType.error) ? res : new cError(cErrorType.bad_reference);
};
cStrucTable.prototype.clone = function (opt_ws) {
Expand Down Expand Up @@ -2323,11 +2334,12 @@ parserHelp.setDigitSeparator(AscCommon.g_oDefaultCultureInfo.NumberDecimalSepara
// file works with "#This Row" - user with "@"
// isLocal - change "#This Row", to "@"
const table = this.wb.getDefinesNames(this.tableName, null);
let tblStr, columns_1, columns_2;
if (!table) {
tblStr = this.tableName;
} else {
tblStr = table.name;
let tblStr = "", columns_1, columns_2;

if (!(this.isCellInTable && this.oneColumnIndex && isLocal)) {
// only for oneColumn we use shorthand notation inside the table (as in ms)
// It's also important to remember that the full entry is always written into the cell, not a short one
tblStr = table ? table.name : this.tableName;
}

if (this.oneColumnIndex) {
Expand Down Expand Up @@ -7385,7 +7397,7 @@ function parserFormula( formula, parent, _ws ) {
parseResult.addRefPos(ph.pCurrPos - ph.operand_str.length, ph.pCurrPos, t.outStack.length, found_operand);

t.ca = isRecursiveFormula(found_operand, t);
} else if (_tableTMP = parserHelp.isTable.call(ph, t.Formula, ph.pCurrPos, local)) {
} else if (_tableTMP = parserHelp.isTable.call(ph, t, ph.pCurrPos)) {
found_operand = cStrucTable.prototype.createFromVal(_tableTMP, t.wb, t.ws, tablesMap);

//todo undo delete column
Expand Down Expand Up @@ -7436,7 +7448,7 @@ function parserFormula( formula, parent, _ws ) {
defName = found_operand.getDefName();
}

if (defName && defName.type === Asc.c_oAscDefNameType.table && (_tableTMP = parserHelp.isTable(sDefNameOperand + "[]", 0))) {
if (defName && defName.type === Asc.c_oAscDefNameType.table && (_tableTMP = parserHelp.isTable(t, 0,/*isLocal */ null,/*callFfromDefName*/ true))) {
found_operand = cStrucTable.prototype.createFromVal(_tableTMP, t.wb, t.ws);
//need assemble becase source formula wrong
needAssemble = true;
Expand Down
18 changes: 18 additions & 0 deletions cell/model/Workbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,11 @@
var sheetDefName = defNameParseRef[0];
var sRefDefName = defNameParseRef[1];

if (sheetDefName) {
// remove inner quotes from string
sheetDefName = sheetDefName.replace(/['"]+/g, '');
}

if (cellSheet && cellSheet.sName === sheetDefName) {
var refDefName = AscCommonExcel.g_oRangeCache.getAscRange(sRefDefName);
if (refDefName && refDefName.contains(col, row)) {
Expand Down Expand Up @@ -8528,6 +8533,19 @@
}
return res;
};
Worksheet.prototype.getTableByRowCol = function(row, col){
let res = null;
if (!this.TableParts || typeof(row) !== 'number' || typeof(col) !== 'number')
return res;

for (let i = 0; i < this.TableParts.length; i++) {
if (this.TableParts[i].Ref && this.TableParts[i].Ref.contains(col, row)) {
res = this.TableParts[i];
break;
}
}
return res;
};
Worksheet.prototype.getTableRangeColumnByName = function(tableName, columnName){
var res = null;
if(!this.TableParts || !tableName)
Expand Down
83 changes: 81 additions & 2 deletions cell/view/WorkbookView.js
Original file line number Diff line number Diff line change
Expand Up @@ -2809,6 +2809,7 @@
};

let defNamesList, defName, defNameStr, _lastFNameLength, _type;
let currentCell = oThis.cellEditor.options.bbox;
fName = fName.toUpperCase();
for (let i = 0; i < this.formulasList.length; ++i) {
if (0 === this.formulasList[i].indexOf(fName)) {
Expand All @@ -2819,13 +2820,85 @@
defNamesList = this.getDefinedNames(Asc.c_oAscGetDefinedNamesList.WorksheetWorkbook);
fName = fName.toLowerCase();
for (let i = 0; i < defNamesList.length; ++i) {
let activeWS = this.getActiveWS();
let isCurrentCellInTable, shortNameUsed;

defName = defNamesList[i];
defNameStr = defName.Name;

let tablePart = activeWS.getTableByName(defNameStr), tableRef = tablePart.Ref;

if (tableRef && tableRef.contains(currentCell.c1, currentCell.r1)) {
isCurrentCellInTable = true;
}

if (null !== defName.LocalSheetId && defNameStr.toLowerCase() === "print_area") {
defNameStr = AscCommon.translateManager.getValue("Print_Area");
}

if (0 === defNameStr.toLowerCase().indexOf(fName)) {
if (defName.type === Asc.c_oAscDefNameType.table && fName[0] === "[" && isCurrentCellInTable) {
// '=[Column1]' === '=Table[Column1]'
shortNameUsed = true;

// add only columns to the dropdown menu if @ the last element
// looking for matches by column names
let table = tablePart ? tablePart : this.model.getTableByName(defNameStr);
if (table) {
let tempfName = defNameStr.toLowerCase() + fName;
let sTableInner = _getInnerTable(tempfName, defNameStr.toLowerCase()); // значение внутри скобок
if (null !== sTableInner) {
let _str;
for (let j = 0; j < table.TableColumns.length; j++) {
_str = table.TableColumns[j].Name;
_type = c_oAscPopUpSelectorType.TableColumnName;

let newStr;
if (sTableInner[0] === "@") {
newStr = sTableInner.slice(1);
if (newStr === "" || 0 === _str.toLowerCase().indexOf(newStr)) {
arrResult.push(getCompleteMenu(_str, _type));
}
}
else if (sTableInner === "" || 0 === _str.toLowerCase().indexOf(sTableInner)) {
arrResult.push(getCompleteMenu(_str, _type));
}
}

// TODO в ms подсказка не отображается, но можно написать вручную и будет выполняться
// if (AscCommon.cStrucTableLocalColumns) {
// for (let j in AscCommon.cStrucTableLocalColumns) {
// _str = AscCommon.cStrucTableLocalColumns[j];
// if (j === "h") {
// _str = "#" + _str;
// _type = c_oAscPopUpSelectorType.TableHeaders;
// } else if (j === "d") {
// _str = "#" + _str;
// _type = c_oAscPopUpSelectorType.TableData;
// } else if (j === "a") {
// _str = "#" + _str;
// _type = c_oAscPopUpSelectorType.TableAll;
// } else if (j === "tr") {
// _str = "@" + " - " + _str;
// _type = c_oAscPopUpSelectorType.TableThisRow;
// } else if (j === "t") {
// _str = "#" + _str;
// _type = c_oAscPopUpSelectorType.TableTotals;
// }
// if (sTableInner === "" || (0 === _str.toLocaleLowerCase().indexOf(sTableInner) && _type !== c_oAscPopUpSelectorType.TableThisRow)) {
// arrResult.push(getCompleteMenu(_str, _type));
// }
// }
// }
fPos += defNameStr.length + (fName.length - defNameStr.length - sTableInner.length);
_lastFNameLength = sTableInner.length;
// shift for fPos for correct work when we use result from dropdown menu
if (sTableInner[0] === "@") {
fPos++
}
}
}
}
else if (0 === defNameStr.toLowerCase().indexOf(fName)) {
_type = c_oAscPopUpSelectorType.Range;
if (defName.type === Asc.c_oAscDefNameType.slicer) {
_type = c_oAscPopUpSelectorType.Slicer;
Expand Down Expand Up @@ -2928,8 +3001,14 @@
if (type === c_oAscPopUpSelectorType.TableThisRow) {
this.skipHelpSelector = false;
}
let newText = name;
if (type === c_oAscPopUpSelectorType.TableThisRow) {
newText = "@"
} else if (type === c_oAscPopUpSelectorType.TableColumnName) {
newText = name && name[0] === "#" ? ("'" + name) : name;
}

this.cellEditor.replaceText(this.lastFPos, this.lastFNameLength, type === c_oAscPopUpSelectorType.TableThisRow ? "@" : name);
this.cellEditor.replaceText(this.lastFPos, this.lastFNameLength, newText);
this.cellEditor.skipTLUpdate = tmp;
} else if (false === this.cellEditor.insertFormula(name, isNotFunction)) {
// Не смогли вставить формулу, закроем редактор, с сохранением текста
Expand Down
47 changes: 42 additions & 5 deletions common/editorscommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -3741,20 +3741,57 @@
return true;
}
};
parserHelper.prototype.isTable = function (formula, start_pos, local)
parserHelper.prototype.isTable = function (parserFormula, start_pos, local, callFromDefName)
{
let shortNameUsed, shortName, isCellInTable;
let formula = parserFormula.Formula,
ws = parserFormula.ws,
subSTR = formula.substring(start_pos);

if (callFromDefName) {
formula += "[]";
}

if (this instanceof parserHelper)
{
this._reset();
}

let subSTR = formula.substring(start_pos),
match = XRegExp.exec(subSTR, local ? rx_table_local : rx_table);

/* check if the cell is inside the table and whether short name is used "=[Column]" */
if (parserFormula.parent) {
let table = ws.getTableByRowCol(parserFormula.parent.nRow, parserFormula.parent.nCol);
isCellInTable = table ? true : false;
if (isCellInTable && subSTR && subSTR[0] === "[" && ws) {
// todo добавить другую регулярку?
let shorNameMatch = subSTR.match(/\[[^\[\]]*\]/);

if (shorNameMatch && shorNameMatch[0]) {
shortName = subSTR; // set shortName definitely before changing subSTR
subSTR = table.DisplayName + subSTR; // add table from cell coords in to the formula string and then call XRegExp.exec
// shortName = shorNameMatch[0];
shortNameUsed = true;
}
}
}

// let subSTR = formula.substring(start_pos),
let match = XRegExp.exec(subSTR, local ? rx_table_local : rx_table);

if (match != null && match["tableName"])
{
this.operand_str = match[0];
this.pCurrPos += match[0].length;
match["isCellInTable"] = isCellInTable;

if (shortName && shortNameUsed) {
this.operand_str = shortName;
this.pCurrPos += shortName.length;

match["shortName"] = shortName;
} else {
this.operand_str = match[0];
this.pCurrPos += match[0].length;
}

return match;
}

Expand Down
Loading
Loading