Skip to content

Commit

Permalink
Added handling of a special case - access to a non-existent defname i…
Browse files Browse the repository at this point in the history
…n an existing ER
  • Loading branch information
DimitryOrlov committed Dec 28, 2024
1 parent df19144 commit f5692e7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
8 changes: 8 additions & 0 deletions cell/model/FormulaObjects/parserFormula.js
Original file line number Diff line number Diff line change
Expand Up @@ -8136,6 +8136,14 @@ function parserFormula( formula, parent, _ws ) {
}

wsF = sheetName ? t.wb.getExternalWorksheet(externalLink, sheetName /*_3DRefTmp[1]*/) : null;

if (externalLink && !local && !wsF) {
// special case when opening a file:
// if we refer to defname that doesn't exist, but the ER itself exists, then we refer to the first existing worksheet
// since we don't know the name of the sheet in the short link and defname doesn't exist
wsF = t.wb.getExternalWorksheet(externalLink, sheetName, true /* getFirtsSheet */);
}

wsT = wsF;
} else {
wsF = t.wb.getWorksheetByName(sheetName/*_3DRefTmp[1]*/);
Expand Down
8 changes: 6 additions & 2 deletions cell/model/Workbook.js
Original file line number Diff line number Diff line change
Expand Up @@ -5220,9 +5220,13 @@
return null;
};

Workbook.prototype.getExternalWorksheet = function (val, sheet) {
Workbook.prototype.getExternalWorksheet = function (val, sheet, getFirstSheet) {
var extarnalLink = window['AscCommon'].isNumber(val) ? this.getExternalLinkByIndex(val - 1) : this.getExternalLinkByName(val);
if (extarnalLink) {
if (getFirstSheet) {
sheet = extarnalLink.SheetNames[0];
}

if (null == sheet) {
return extarnalLink;
}
Expand Down Expand Up @@ -23574,7 +23578,7 @@
}

let sheetName = _3DRefTmp[1] ? _3DRefTmp[1] : externalSheetName;
if (!sheetName) {
if (!sheetName && local) {
return false;
}

Expand Down
21 changes: 18 additions & 3 deletions tests/cell/spreadsheet-calculation/ExternalReference.js
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ $(function () {
shortLinkDefnameWithoutBrackets = "'1'!_s1",
externalWs;


let elemInStack;
// create external link
let cellWithFormula = new AscCommonExcel.CCellWithFormula(ws, 1, 0);
let parseResult = new AscCommonExcel.ParseResult([]);
Expand Down Expand Up @@ -1044,19 +1044,34 @@ $(function () {

oParser = new parserFormula(shortLinkDefname, cellWithFormula, ws);
assert.ok(oParser.parse(false, null, parseResult), "Short link to defname. isLocal = false. " + shortLinkDefname);
elemInStack = oParser.outStack && oParser.outStack[0];
if (elemInStack && (elemInStack.type === AscCommonExcel.cElementType.name3D)) {
assert.strictEqual(elemInStack.value, "_s1");
assert.ok(elemInStack.ws);
assert.strictEqual(elemInStack.ws && elemInStack.ws.sName, "Sheet1");
}

oParser = new parserFormula("[1]!_s223", cellWithFormula, ws);
assert.ok(oParser.parse(false, null, parseResult), "Short link to defname that not exist. isLocal = false. " + "[1]!_s223");
elemInStack = oParser.outStack && oParser.outStack[0];
if (elemInStack && (elemInStack.type === AscCommonExcel.cElementType.name3D)) {
assert.strictEqual(elemInStack.value, "_s223");
assert.ok(elemInStack.ws);
assert.strictEqual(elemInStack.ws && elemInStack.ws.sName, "Sheet1");
}

// inside the formula tests
oParser = new parserFormula("SUM([1]!_s1)", cellWithFormula, ws);
assert.ok(oParser.parse(false, null, parseResult), "SUM([1]!_s1). isLocal = false");

oParser = new parserFormula("SUM('[1]'!_s1)", cellWithFormula, ws);
assert.ok(oParser.parse(false, null, parseResult) === false, "SUM('[1]'!_s1). isLocal = false");
assert.ok(oParser.parse(false, null, parseResult), "SUM('[1]'!_s1). isLocal = false");

oParser = new parserFormula("SUM([1]!_s1,2,3)", cellWithFormula, ws);
assert.ok(oParser.parse(false, null, parseResult), "SUM([1]!_s1,2,3). isLocal = false");

oParser = new parserFormula("SUM('[1]'!_s1,2,3)", cellWithFormula, ws);
assert.ok(oParser.parse(false, null, parseResult) === false, "SUM('[1]'!_s1,2,3). isLocal = false");
assert.ok(oParser.parse(false, null, parseResult), "SUM('[1]'!_s1,2,3). isLocal = false");

// local = true. Manual input. Try parse string to external ref similiar as writing a string manually
oParser = new parserFormula(fullLinkLocal, cellWithFormula, ws);
Expand Down

0 comments on commit f5692e7

Please sign in to comment.