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+',"")';

}

}

}

相关推荐
涛涛讲AI1 天前
如何使用WPS开发平台的API调试台获取WPS的多维表格的内容
wps
jiecy2 天前
WPS 的多条件更改单元格样式
wps
爱上妖精的尾巴3 天前
7-8 WPS JS宏 对象使用实例5--按多字段做多种汇总
javascript·后端·restful·wps·jsa
hrw_embedded5 天前
WPS最新版本离线使用设置
wps
默默提升实验室5 天前
禁止WPS自动添加计划任务
wps
熊文豪5 天前
WPS Excel 内嵌图片的程序化导入导出技术
excel·poi·wps
昵称什么的不存在6 天前
linux安装WPS和typora等deb包,以及typora的linux版本激活
linux·运维·wps
爱上妖精的尾巴9 天前
7-3 WPS JS宏 keys、values、entries、JSON.stringify 循环对象中的属性
后端·restful·wps·jsa
山风wind10 天前
WPS如何快速查询A列的数据是否在B列存在
wps
weixin_4404016910 天前
WPS Excel 宏使用
excel··wps