WPS宏实现对表格选中区域数据进行遍历读取及动态赋值

VBA中好像可以直接用forEach进行遍历,JS宏中有对数组的forEach方法,我没试出来。现在做的这一版是用双重FOR循环组合完成的,赋值用的是Formula属性,这个可以写公式。

function JIn()

{

//1、作用于Sheet1页

Application.Worksheets.Item('Sheet1').Activate();

//2、获取当前选中区域的首行和首列

var a = ActiveCell.Cells.Row;

var b = ActiveCell.Cells.Column;

//3、获取当前选中区域有多少行和多少列

var c =Selection.Cells.Address();

var d = Range(c).Columns.Count;

var e = Range(c).Rows.Count;

//4、选中区域的列号放进数组

var arrcol = new Array();

for(var i = 1 ; i <= d ; i++){

arrcol[i-1]=b-1+i;

}

//5、选中区域的行号放进数组

var arrow = new Array();

for(var j = 1 ; j <= e ; j++){

arrow[j-1]=a-1+j;

}

//6、双重FOR循环组装行列号

for (let y = 0; y < arrcol.length; y++) {

for (let x = 0; x < arrow.length; x++) {

//选中区域读取

var f = Sheets("Sheet1").Cells.Item(arrow[x],arrcol[y]).Value2;

//console.log("选中区域的值:"+f);

//选中区域后22列及前9列单元格的地址

var g =Sheets("Sheet1").Cells.Item(arrow[x],arrcol[y]+22).Address();

var h =Sheets("Sheet1").Cells.Item(arrow[x],arrcol[y]-10).Address();

//console.log("选中区域的地址:"+h);

//选中区域赋值

Sheets("Sheet1").Cells.Item(arrow[x],arrcol[y]).Formula='=IF('+h+'<>"",'+g+',"")';

}

}

}

相关推荐
蒲公英100131 分钟前
在wps软件的word中使用js宏命令设置表格背景色
javascript·word·wps
winkel_wang1 天前
think-cell 无法与 WPS Office 搭配使用
windows·wps·think-cell
月清晖1 天前
安装了wps/卸载wps后office文件打开异常处理
wps
小龙在山东1 天前
基于 WPS TOROW 函数实现 VLOOKUP:多行多列转一行
wps
雾江流1 天前
WPS国际版18.22 | 集Word,PDF,Sheet,PowerPoint于一体的多功能免费办公套件
pdf·word·软件工程·wps
有过~2 天前
WPS Office国际版 办公软件v18.20.2完美去广解锁高级版
wps
不坑老师7 天前
怎么在PPT里面插入网页?
microsoft·powerpoint·wps·office
筑凡7 天前
PPT+配音生成带旁白的PPT演示视频
powerpoint·音视频·wps·mp4·ppt
不坑老师9 天前
利用不坑盒子在WPS中插入网页,放映的电脑无需安装插件,直接就能显示网页!
microsoft·word·powerpoint·wps
TracyDemo9 天前
删除wps的空白页
wps