Can't find what you're looking for?

Contact us or post in our forums and we'll help you find an answer.

How to display the formatting of a cell when the cell is in edit mode in JavaScript

Background:

To display a cell’s formatting during edit mode we will use the setEditorVale method to customize the editor
For example, a cell that displays 15% will show 15% while in edit mode, not .15

Steps to Complete:

  1. Use the setEditorValue method and set the parameters using the context parameters

  2. Set the editor value to display the same value and formatting

Getting Started:

Using the setEditorValue method

Use the setEditorValue method and get the cell value and formatter from the row and column indexes

GC.Spread.Sheets.CellTypes.Text.prototype.setEditorValue = function(editor, value, context) {
 var sheet = context.sheet, row = context.row, column = context.col;
 var cellValue = sheet.getValue(row, column);
 
 var formatter = sheet.getFormatter(row, column);
}

Set the editor value to display the same value and formatting

Set the value to be equal to SpreadJS’s the GeneralFormatter class with the parameter of formatter. This formats the specified object as a string with a formatted data Object based on the format of the cell that is being edited.

Then, invoke the format method to set the format of the cell.

var newForm = GC.Spread.Sheets.CellTypes.Text.prototype.setEditorValue;

GC.Spread.Sheets.CellTypes.Text.prototype.setEditorValue = 
  function(editor, value, context) {
    var sheet = context.sheet, row = context.row, column = context.col;
    var cellValue = sheet.getValue(row, column);
    var formatter = sheet.getFormatter(row, column);
    if (cellValue && formatter) {
     value = new GC.Spread.Formatter.GeneralFormatter(formatter).format(value);
     newForm.call(this, editor, value, context);
    }
    else {
        newForm.apply(this, arguments);
    }
  };