[]
        
(Showing Draft Content)

GC.Spread.Formatter.FormatterBase

类: FormatterBase

Spread.Formatter.FormatterBase

Table of contents

构造方法

属性

方法

构造方法

constructor

new FormatterBase(format, cultureName)

具有指定格式字符串的自定义格式

参数

属性名 类型 说明
format string 格式化
cultureName string 本地化名称

属性

typeName

typeName: string

支持序列化的类型名称字符串

代码示例

//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

方法

format

format(obj, options?): string

将指定的对象格式化为带有条件颜色的字符串这个函数应该重写

代码示例

//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");

参数

属性名 类型 说明
obj Object The object with cell data to format.
options? Object -

返回值

string

格式化的字符串


fromJSON

fromJSON(settings): void

从指定的JSON字符串加载对象状态

代码示例

//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

参数

属性名 类型 说明
settings Object 来自反序列化的自定义格式化数据

返回值

void


parse

parse(str): Object

解析指定的文本这个函数应被重写

代码示例

//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");

参数

属性名 类型
str string

返回值

Object

解析的对象


toJSON

toJSON(): Object

将对象状态保存为JSON字符串

代码示例

//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

返回值

Object

自定义格式化数据