本帖最后由 kc 于 2025-5-28 11:42 编辑
我有两个sheet都是填报的电子表格,现在想通过每个sheet中的L1单元格判断是否为1,去禁止填写,两个sheet的L1值可能不一样,现在代码可以实现控制第二个sheet的回写,但是第一个只能控制前后几行,求助各位大佬帮忙看看是哪里有问题,感谢!!!
服务端:
function main(spreadsheetReport) {
// 获取电子表格所有sheet对象,0表示第一个sheet,1表示第二个,2表示第三个
var sheet1 = spreadsheetReport.workbook.getWorksheets().get(0);
var value1 = sheet1.getCells().getCell(0, 11).value; // getCell(0, 11),表示获取L1单元格第1行,第12列的单元格的值
spreadsheetReport.customProperties.put("LValue1", value1);
var sheet11 = spreadsheetReport.sheets[0];
var posList1 = sheet11.getExpandedPositions("F4"); //获取F4单元格拓展出来的最大行数
spreadsheetReport.customProperties.put("maxrow1", posList1.length);
// 获取电子表格所有sheet对象,0表示第一个sheet,1表示第二个,2表示第三个
var sheet2 = spreadsheetReport.workbook.getWorksheets().get(1);
var value2 = sheet2.getCells().getCell(0, 11).value; // getCell(0, 11),表示获取L1单元格第1行,第12列的单元格的值
spreadsheetReport.customProperties.put("LValue2", value2);
var sheet22 = spreadsheetReport.sheets[1];
var posList2 = sheet22.getExpandedPositions("5"); //获取F4单元格拓展出来的最大行数
spreadsheetReport.customProperties.put("maxrow2", posList2.length);
}
客户端:
function main(spreadsheetReport) {
var customProperties = spreadsheetReport.elemSheetFrame.contentWindow.customProperties;
var cells = spreadsheetReport.elemSheetFrame.contentWindow;
var writableMap = cells.writableMap;
var targetCols1 = ["5", "6", "7"];
// 获取开始行数
var startRow1 = 3; // 第四行对应索引3
// 获取最大数据行数
var maxRow1 = customProperties["maxrow1"] + 100;
for (var row1 = startRow1; row1 <= maxRow1; row1++) {
var LValue1 = customProperties["LValue1"]
// alert(LValue1);
if (LValue1 == "1" || LValue1 == 1) {
// 禁止回写逻辑
targetCols1.forEach(function(col) {
var colKey = row1 + ":" + col; // 格式:"*:5" 表示所有行的第6列
// alert(colKey)
if (writableMap[colKey] && !writableMap["bak." + colKey]) {
writableMap["bak." + colKey] = writableMap[colKey]; // 备份原始状态
delete writableMap[colKey]; // 禁用回写
}
});
} else {
// 恢复回写逻辑
targetCols1.forEach(function(col) {
var colKey = row1 + ":" + col;
// alert(colKey)
if (writableMap["bak." + colKey]) {
writableMap[colKey] = writableMap["bak." + colKey]; // 恢复原始状态
delete writableMap["bak." + colKey]; // 清除备份
}
});
}
}
var targetCols2 = ["1", "2", "3", "4", "5", "6","7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"];
// 获取开始行数
var startRow2 = 4; // 第四行对应索引3
// 获取最大数据行数
var maxRow2 = customProperties["maxrow2"] + 50;
for (var row2 = startRow2; row2 <= maxRow2; row2++) {
var LValue2 = customProperties["LValue2"]
// alert(LValue2);
if (LValue2 == "1" || LValue2 == 1) {
// 禁止回写逻辑
targetCols2.forEach(function(col) {
var colKey = row2 + ":" + col; // 格式:"*:5" 表示所有行的第6列
// alert(colKey)
if (writableMap[colKey] && !writableMap["bak." + colKey]) {
writableMap["bak." + colKey] = writableMap[colKey]; // 备份原始状态
delete writableMap[colKey]; // 禁用回写
}
});
} else {
// 恢复回写逻辑
targetCols2.forEach(function(col) {
var colKey = row2 + ":" + col;
// alert(colKey)
if (writableMap["bak." + colKey]) {
writableMap[colKey] = writableMap["bak." + colKey]; // 恢复原始状态
delete writableMap["bak." + colKey]; // 清除备份
}
});
}
}
}
|