# GC.Spread.Formatter.FormatterBase

## Content

# Class: FormatterBase

[Spread](../modules/GC.Spread).[Formatter](../modules/GC.Spread.Formatter).FormatterBase

## Table of contents

### Constructors

- [constructor](GC.Spread.Formatter.FormatterBase#constructor)

### Properties

- [typeName](GC.Spread.Formatter.FormatterBase#typename)

### Methods

- [format](GC.Spread.Formatter.FormatterBase#format)
- [fromJSON](GC.Spread.Formatter.FormatterBase#fromjson)
- [parse](GC.Spread.Formatter.FormatterBase#parse)
- [toJSON](GC.Spread.Formatter.FormatterBase#tojson)

## Constructors

### <a id="constructor" name="constructor"></a> 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

### <a id="typename" name="typename"></a> typeName

• **typeName**: `string`

Represents the type name string used for supporting serialization.

**`example`**
```javascript
//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

### <a id="format" name="format"></a> format

▸ **format**(`obj`, `options?`): `string`

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

**`example`**
```javascript
//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.

___

### <a id="fromjson" name="fromjson"></a> fromJSON

▸ **fromJSON**(`settings`): `void`

Loads the object state from the specified JSON string.

**`example`**
```javascript
//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`

___

### <a id="parse" name="parse"></a> parse

▸ **parse**(`str`): `Object`

Parses the specified text. This function should be overwritten.

**`example`**
```javascript
//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.

___

### <a id="tojson" name="tojson"></a> toJSON

▸ **toJSON**(): `Object`

Saves the object state to a JSON string.

**`example`**
```javascript
//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.
