How to make an auto suggestion in the Spreadsheet cell?
This article explains how to make an auto-suggestion in the JavaScript Spreadsheet cell. You can achieve this with some customization in the keyUp event. In this keyUp event, you can compare the editable text with other cell values. In this event, we have used the createRange method to create a new range object for the document and selected the range by using the window getSelection method.
[Html]
<div id="spreadsheet"></div>
[TS]
created: (): void => {
spreadsheet.cellFormat(
{ fontWeight: 'bold', textAlign: 'center', verticalAlign: 'middle' },
'A1:C1'
);
spreadsheet.element.addEventListener('keyup', event => {
let actSheet = spreadsheet.getActiveSheet();
let actCell = actSheet.activeCell;
let rangeIndex = getRangeIndexes(actCell);
for (let i = rangeIndex[2]; i > 0; i++) {
let cell = getCell(i + 1, rangeIndex[1], actSheet);
if (
event.key == 'Backspace' ||
event.key == 'Delete' ||
event.key == 'Escape'
) {
return;
} else if (cell && cell.value) {
let elem = document.getElementsByClassName('e-spreadsheet-edit')[0];
let textLen = elem.innerHTML.length;
if (
elem.innerHTML.toUpperCase() ==
cell.value.toUpperCase().slice(0, textLen)
) {
(elem as HTMLElement).innerText = cell.value;
let startNode = document.getElementsByClassName(
'e-spreadsheet-edit'
)[0].firstChild;
startNode.nodeValue = startNode.nodeValue.trim();
let range = document.createRange();
range.setStart(startNode, textLen);
range.setEnd(startNode, startNode.textContent.length);
let sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
return;
}
} else return;
}
});
}
Demo Sample: https://stackblitz.com/edit/hfm3bz?file=index.ts
Conclusion
We hope you enjoyed learning about how to make an auto suggestion in the Spreadsheet cell.
You can refer to our JavaScript Spreadsheet page to learn about its other groundbreaking feature representations. You can also explore our JavaScript Spreadsheet Documentation to understand how to manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!