diff --git a/browser/src/control/Control.FormulaAutoCompletePopup.ts b/browser/src/control/Control.FormulaAutoCompletePopup.ts index fe1263f45d913..d81218a005e15 100644 --- a/browser/src/control/Control.FormulaAutoCompletePopup.ts +++ b/browser/src/control/Control.FormulaAutoCompletePopup.ts @@ -64,10 +64,22 @@ class FormulaAutoCompletePopup extends L.Control.AutoCompletePopup { if (eventType === 'close') { this.closePopup(); } else if (eventType === 'select' || eventType === 'activate') { - var currentText = this.map._docLayer._lastFormula; - var chIndex = currentText.length - 1; - var functionName = this.functionList[index].name; - var namedRange = this.functionList[index].namedRange; + const namedRange: string = this.functionList[index].namedRange; + let currentText: string = this.map._docLayer._lastFormula; + if (namedRange) { + const openBracketIndex: number = currentText.indexOf('('); + const semicolonIndex: number = currentText.indexOf(';'); + if (semicolonIndex !== -1) { + let tmpCurrentText = currentText.substring(semicolonIndex + 1); + // trim white space if there is any + tmpCurrentText = tmpCurrentText.trim(); + currentText = ';' + tmpCurrentText; + } else if (openBracketIndex !== -1) { + currentText = currentText.substring(openBracketIndex); + } + } + const chIndex: number = currentText.length - 1; + let functionName: string = this.functionList[index].name; functionName = functionName.substring(chIndex); if (namedRange) this.map._textInput._sendText(functionName); else this.map._textInput._sendText(functionName + '(');