SpreadJS 13
SpreadJS Documentation / Sample Code / Sample Code for Cells / Setting Conditional Formats
In This Topic
    Setting Conditional Formats
    In This Topic

    You can set conditional formats for cells.

    Using Code

    This example sets conditional formats.

    JavaScript
    Copy Code
    window.onload = function()
    {
      var spread =
      new GC.Spread.Sheets.Workbook(document.getElementById("ss"),{sheetCount:3});
      var activeSheet = spread.getActiveSheet();
    
      // Create respective conditional styles.
      var styleBlue = new GC.Spread.Sheets.Style();
      var stylePink = new GC.Spread.Sheets.Style();
      var styleLime = new GC.Spread.Sheets.Style();
      var styleYellow = new GC.Spread.Sheets.Style();
      var styleEmpty = new GC.Spread.Sheets.Style();
      styleBlue.backColor = "blue";
      styleBlue.foreColor = "white";
      stylePink.backColor = "pink";
      styleLime.backColor = "lime";
      styleYellow.backColor = "yellow";
      styleEmpty.backColor = undefined;
      styleEmpty.foreColor = undefined;
    
      // Set conditional formats to respective cells.
      activeSheet.conditionalFormats.addCellValueRule(
      GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.EqualsTo,
      0, undefined, styleEmpty,
      [new GC.Spread.Sheets.Range(1, 1, 1, 1)]);
      activeSheet.conditionalFormats.addCellValueRule(
      GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.LessThan,
      10,undefined,styleBlue,
      [new GC.Spread.Sheets.Range(1, 1, 1, 1)]);
      activeSheet.conditionalFormats.addCellValueRule(
      GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.Between,
      20,50,stylePink,
      [new GC.Spread.Sheets.Range(2, 1, 1, 1)]);
      activeSheet.conditionalFormats.addCellValueRule(
      GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.Between,
      50,80,styleLime,
      [new GC.Spread.Sheets.Range(3, 1, 1, 1)]);
      activeSheet.conditionalFormats.addCellValueRule(
      GC.Spread.Sheets.ConditionalFormatting.ComparisonOperators.GreaterThan,
      80, undefined, styleYellow,
      [new GC.Spread.Sheets.Range(4, 1, 1, 1)]);
      activeSheet.getCell(2, 1).value(25);
      activeSheet.getCell(3, 1).value(77);
      activeSheet.getCell(4, 1).value(88);
    }
    
    See Also

    Developer's Guide