如何检查 Google 表格中的单元格是否受到 Google Apps 脚本的保护?具体来说,是范围保护,而不是表保护。
检查单元格是否具有坐标row
and col
(从1开始)有保护用途:
function has_protection(row,col) { // row and column starting from 1
var sheet = SpreadsheetApp.getActiveSheet();
var cell_has_protection = false;
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE); // get all protected ranges
if ( protections != '' ) {
for (var p = 0; p < protections.length; p++) {
var pro_range = protections[p].getRange(); // each protected range
if (row >= pro_range.getRow()
&& row <= (pro_range.getRow() + pro_range.getHeight() - 1)
&& col >= pro_range.getColumn()
&& col <= (pro_range.getColumn() + pro_range.getWidth() - 1)) {
cell_has_protection = true;
}
}
}
return cell_has_protection;
}
您还可以使用以下代码突出显示工作表上的所有受保护单元格(它将清除工作表上的所有其他突出显示并仅填充受保护的单元格):
function color_protected(){
var sheet = SpreadsheetApp.getActiveSheet();
var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
if ( protections != '' ) {
sheet.getRange('1:' + sheet.getMaxRows()).setBackground(null); // clear all background on the sheet
for (var p = 0; p < protections.length; p++) {
var pro_range = protections[p].getRange();
//Logger.log(pro_range.getA1Notation());
pro_range.setBackground('#fbbc04'); // color protected range
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)