[]
Spread.Formatter.FormatterBase
• new FormatterBase(format
, cultureName
)
具有指定格式字符串的自定义格式
属性名 | 类型 | 说明 |
---|---|---|
format |
string |
格式化 |
cultureName |
string |
本地化名称 |
• 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(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(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(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(): 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
自定义格式化数据