ARTICLE DETAIL

资讯详情

深耕网站建设、视觉设计与SEO优化的一线实战洞察。

7-15 WPS JS宏 class、constructor自定义关于[唯一性]的类

7-15 WPS JS宏 class、constructor自定义关于[唯一性]的类

一、函数

class

构造自定义类

格式:class 自定义函数名{}

constructor:

初始化设置函数

格式:constructor(参数值,参数值...){}

二、自定义类示例

class zidinyihanshu{

//初始化设置,只要被激活先运行初始化内容,保存结果。

constructor(Rngs1){//初始化设置函数constructor

this.Rngs=Rngs1;//重点:调用Rngs1可直接使用,调用Rngs需要用this.Rngs

this.Num=Rngs1.Cells.Count;//表达式返回属性值

this.arr=Rngs1.Value().flat();//表达式返回属性值

this.Max=this.arr.reduce((x,y)=>(x>y)?x:y);//表达式返回属性值

this.list=[...new Set(Rngs1.Value().flat())];//打平数组,集合清理重复值再用[]变为数组

}

//需要时在调用

Sum1(){return this.arr.reduce((x,y)=>x+y)};//有返回值(函数)

Rngcolor(color){this.Rngs.Interior.Colorlndex=color}; //无返回值(函数)

celor(){

for (var v of this.list){

var num=WorksheetFunction.RandBetween(1,999999);

for (var a of this.Rngs){

if (a.Value()==v){a.Interior.Color=num}

}}}

}

function test(){

var Diaoyon=new zidinyihanshu(Range("a1:c3"));

Console.log(Diaoyon.Num);

Console.log(Diaoyon.Max);

Console.log(Diaoyon.Sum1());

Diaoyon.Rngcolor(5);

Diaoyon.celor()

}

返回列表