快速入门二

先创建一个表单并进行以下操作。

模板配置

设计一个带明细行的表格,如下图:

image-20250416084411533

明细行配置每一列都使用默认名称并设置为可编辑,明细行名称设置为明细行

image-20250416085146585

脚本编辑

使用脚本实现以下3个功能

1、输入员工编号弹出自定义弹窗;

2、选中自定义弹窗内容后确认回填员工年龄、员工性别、员工姓名;

3、当输入的员工编号已经在表格中出现过时将当前行背景色设置成红色。

在脚本编辑的作用范围下拉选中【Sheet1】,触发事件选中【单元格更新后】,并编写脚本内容,如下图:

image-20250416084839063

// 定义明细行开始和结束行
let startRow = 3;
let endRow = 12;

// 定义要触发脚本的列
let column = 2;

// 获取当前操作单元格对象
let cell = Cells.currentCell();

// 获取当前操作单元格行号和列号
let rowNum = cell.getRowNumber();
let colNum = cell.getColumnNumber();

// 定义弹窗数据的列
let columnList = [
        {
            label:"员工编号",
            prop:"userId",
            width:"100"
        },
        {
            label:"员工姓名",
            prop:"userName",
            width:"100"
        },
        {
            label:"员工年龄",
            prop:"age",
            width:"100"
        },
        {
            label:"员工性别",
            prop:"gender"
        }
    ];

// 定义弹窗数据
let dataList = [
        {
            userId:"1001",
            userName:"张三",
            age:"25",
            gender:"男"
        },
        {
            userId:"1002",
            userName:"李四",
            age:"26",
            gender:"男"
        },
        {
            userId:"1003",
            userName:"王五",
            age:"27",
            gender:"女"
        }
    ];

// 定义一个回调函数
let callBackFun = (res) => {

    // 获取当前明细行的员工编号、员工姓名、员工年龄、员工性别单元格对象
    let idCell = Cells.getCell(rowNum, column);
    let nameCell = Cells.getCell(rowNum, column + 1);
    let ageCell = Cells.getCell(rowNum, column + 2);
    let genderCell = Cells.getCell(rowNum, column + 3);

    // 从返回对象中取出参数信息
    const {userName, age, gender, userId} = res;

    // 赋值给对应单元格
    nameCell.setValue(userName);
    ageCell.setValue(age);
    genderCell.setValue(gender);

    // 获取明细行数据
    let detailList = Form.getDetailData('明细行', ['员工编号']);

    // 过滤明细行查找是否有相同的员工编号
    let fdl = detailList.filter(item => {return item["员工编号"] == userId});

    if (fdl.length >= 2) {

        // 设置单元格填充色
        idCell.setFillColor('#FF0000');
        nameCell.setFillColor('#FF0000');
        ageCell.setFillColor('#FF0000');
        genderCell.setFillColor('#FF0000');
    }
};

// 判断当前操作单元格是否在触发脚本范围内
if (rowNum >= startRow && rowNum <= endRow && column == colNum) {
    // 打开弹窗
    Form.openSelect(false, columnList, dataList, callBackFun);
}

results matching ""

    No results matching ""