您好,请参考以下自定义单元格示例来实现:
- function NumberCellType() {}
- NumberCellType.prototype = new GC.Spread.Sheets.CellTypes.Text();
- NumberCellType.prototype.createEditorElement = function (context) {
- let editor =
- GC.Spread.Sheets.CellTypes.Text.prototype.createEditorElement.call(
- this,
- context
- );
- editor.onkeyup = function (event) {
- console.log("keyup", event);
- if (event.srcElement.innerText.length >= 5) {
- this.innerText = this.innerText.substring(0, 4);
- const range = document.createRange();
- const selection = window.getSelection();
- range.selectNodeContents(this);
- range.collapse(false);
- selection.removeAllRanges();
- selection.addRange(range);
- }
- };
- return editor;
- };
- NumberCellType.prototype.activateEditor = function (
- editorContext,
- cellStyle,
- cellRect,
- context
- ) {
- GC.Spread.Sheets.CellTypes.Text.prototype.activateEditor.apply(this, [
- editorContext,
- cellStyle,
- cellRect,
- context,
- ]);
- let value = context.sheet.getCell(context.row, context.col).value();
- if (value && value.toString().includes("/")) {
- let a = value.toString().substring(0, 2);
- let b = value.toString().substring(3);
- let result = a + b;
- context.sheet.getCell(context.row, context.col).value(result);
- }
- };
- NumberCellType.prototype.deactivateEditor = function (editorContext, context) {
- GC.Spread.Sheets.CellTypes.Text.prototype.deactivateEditor.apply(this, [
- editorContext,
- context,
- ]);
- let originalValue = context.sheet.getCell(context.row, context.col).value();
- let a = originalValue.toString().substring(0, 2);
- let b = originalValue.toString().substring(2);
- let result = a + "/" + b;
- context.sheet.getCell(context.row, context.col).value(result);
- };
- let numberCellType = new NumberCellType();
- sheet.getRange(0, 0, 5, 5).cellType(numberCellType);
复制代码
|