[]
        
(Showing Draft Content)

GC.Spread.Formatter.FormatterBase

Class: FormatterBase

Spread.Formatter.FormatterBase

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new FormatterBase(format, cultureName)

Represents a custom formatter with the specified format string.

Parameters

Name Type Description
format string The format.
cultureName string The culture name.

Properties

typeName

typeName: string

Represents the type name string used for supporting serialization.

example

//This example creates a custom formatter.
function MyFormatter(format, cultureName) {
  GC.Spread.Formatter.FormatterBase.apply(this, arguments);
  this.typeName = "MyFormatter";
  this.formatter = format;
}
MyFormatter.prototype = new GC.Spread.Formatter.FormatterBase();
MyFormatter.prototype.format = function (obj, options) {
    if (typeof obj === "number") {
        var colors = this.formatter.split(";");
        if (obj >= 0) {
            options && options.conditionalForeColor = colors[0];
            return "PositiveOrZero: " + obj;
        } else {
            options && options.conditionalForeColor = colors[1];
            return "Negative: " + obj;
        }
    }
    return obj ? obj.toString() : "";
};
MyFormatter.prototype.parse = function (str) {
    var index = str.indexOf(": ");
    if (index >= 0) {
        return +str.substr(index + 2);
    } else {
        return +str;
    }
};
MyFormatter.prototype.toJSON = function () {
    return {
        typeName: this.typeName,
        formatter: this.formatter
    };
};
MyFormatter.prototype.fromJSON = function (settings) {
    if (settings) {
        this.formatter = settings.formatter;
    }
};
var formatter = new MyFormatter("red;green");
formatter.format(12345); // "PositiveOrZero: 12345"
formatter.parse("PositiveOrZero: 12345"); // 12345

Methods

format

format(obj, options?): string

Formats the specified object as a string with a conditional color. This function should be overwritten.

example

//This example creates a custom formatter.
function CustomFormatterTest() {
}
CustomFormatterTest.prototype = new GC.Spread.Formatter.FormatterBase();
CustomFormatterTest.prototype.format = function (obj, options) {
\xa0 \xa0 //if the obj is color string, exp: red, blue.the text will render with obj color.
\xa0 \xa0 if (obj) {
\xa0 \xa0 \xa0 \xa0 options.conditionalForeColor = obj.toString()
\xa0 \xa0 \xa0 \xa0 return "My format result : " + obj.toString();
\xa0 \xa0 }
\xa0 \xa0 return "";
};
CustomFormatterTest.prototype.parse = function (str) {
\xa0 \xa0 if (!str) {
\xa0 \xa0 \xa0 \xa0 return "";
\xa0 \xa0 }
\xa0 \xa0 return str;
}
var sheet = spread.getActiveSheet();
sheet.getCell(1, 0).formatter(new CustomFormatterTest());
sheet.getCell(1, 0).value("red");

Parameters

Name Type Description
obj Object The object with cell data to format.
options? Object -

Returns

string

The formatted string.


fromJSON

fromJSON(settings): void

Loads the object state from the specified JSON string.

example

//This example creates a custom formatter.
function MyFormatter(format, cultureName) {
  GC.Spread.Formatter.FormatterBase.apply(this, arguments);
  this.typeName = "MyFormatter";
  this.formatter = format;
}
MyFormatter.prototype = new GC.Spread.Formatter.FormatterBase();
MyFormatter.prototype.format = function (obj, options) {
    if (typeof obj === "number") {
        var colors = this.formatter.split(";");
        if (obj >= 0) {
            options && options.conditionalForeColor = colors[0];
            return "PositiveOrZero: " + obj;
        } else {
            options && options.conditionalForeColor = colors[1];
            return "Negative: " + obj;
        }
    }
    return obj ? obj.toString() : "";
};
MyFormatter.prototype.parse = function (str) {
    var index = str.indexOf(": ");
    if (index >= 0) {
        return +str.substr(index + 2);
    } else {
        return +str;
    }
};
MyFormatter.prototype.toJSON = function () {
    return {
        typeName: this.typeName,
        formatter: this.formatter
    };
};
MyFormatter.prototype.fromJSON = function (settings) {
    if (settings) {
        this.formatter = settings.formatter;
    }
};
var formatter = new MyFormatter("red;green");
formatter.format(12345); // "PositiveOrZero: 12345"
formatter.parse("PositiveOrZero: 12345"); // 12345

Parameters

Name Type Description
settings Object The custom formatter data from deserialization.

Returns

void


parse

parse(str): Object

Parses the specified text. This function should be overwritten.

example

//This example creates a custom formatter.
function CustomFormatterTest() {
}
CustomFormatterTest.prototype = new GC.Spread.Formatter.FormatterBase();
CustomFormatterTest.prototype.format = function (obj, options) {
\xa0 \xa0 //if the obj is color string, exp: red, blue.the text will render with obj color.
\xa0 \xa0 if (obj) {
\xa0 \xa0 \xa0 \xa0 options.conditionalForeColor = obj.toString()
\xa0 \xa0 \xa0 \xa0 return "My format result : " + obj.toString();
\xa0 \xa0 }
\xa0 \xa0 return "";
};
CustomFormatterTest.prototype.parse = function (str) {
\xa0 \xa0 if (!str) {
\xa0 \xa0 \xa0 \xa0 return "";
\xa0 \xa0 }
\xa0 \xa0 return str;
}
var sheet = spread.getActiveSheet();
sheet.getCell(1, 0).formatter(new CustomFormatterTest());
sheet.getCell(1, 0).value("red");

Parameters

Name Type
str string

Returns

Object

The parsed object.


toJSON

toJSON(): Object

Saves the object state to a JSON string.

example

//This example creates a custom formatter.
function MyFormatter(format, cultureName) {
  GC.Spread.Formatter.FormatterBase.apply(this, arguments);
  this.typeName = "MyFormatter";
  this.formatter = format;
}
MyFormatter.prototype = new GC.Spread.Formatter.FormatterBase();
MyFormatter.prototype.format = function (obj, options) {
    if (typeof obj === "number") {
        var colors = this.formatter.split(";");
        if (obj >= 0) {
            options && options.conditionalForeColor = colors[0];
            return "PositiveOrZero: " + obj;
        } else {
            options && options.conditionalForeColor = colors[1];
            return "Negative: " + obj;
        }
    }
    return obj ? obj.toString() : "";
};
MyFormatter.prototype.parse = function (str) {
    var index = str.indexOf(": ");
    if (index >= 0) {
        return +str.substr(index + 2);
    } else {
        return +str;
    }
};
MyFormatter.prototype.toJSON = function () {
    return {
        typeName: this.typeName,
        formatter: this.formatter
    };
};
MyFormatter.prototype.fromJSON = function (settings) {
    if (settings) {
        this.formatter = settings.formatter;
    }
};
var formatter = new MyFormatter("red;green");
formatter.format(12345); // "PositiveOrZero: 12345"
formatter.parse("PositiveOrZero: 12345"); // 12345

Returns

Object

The custom formatter data.