How do I stop Google AppScripts (Sheets) from creating two protected range entries

I have a Google sheet where I have set a row to become protected when I check a tickbox in Col U. But when I perform this action with the account I setup the code with, it makes two entries of protect ranges.

When I tick the box from another account, I get only 1 entry and it seems to work correctly.

Double entry of range protection with main account

Correct single entry when using a different account

My code is the following. I made it to only protect rows with a warning.

function onEdit() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = ss.getActiveSheet();
  let cell = sheet.getActiveCell()
  let col = cell.getColumn();//gets column numnber
  let rownum = cell.getRow();//gets row number
 

  if(col==21 && cell.getValue() == true){//if changed to true protects row to end of data
    let protRange = sheet.getRange(rownum,1,1,22).protect().setWarningOnly(true).setDescription(rownum);
    }
  
  else if(col == 21 && cell.getValue() ==false){//if changed to false cycles through all the protected ranges on the sheet and unprotects if the row number is the same
    let protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE)
    for (let i = 0;i<protections.length;i++){
      let proDesc = protections[i].getDescription();
      if(parseInt(proDesc)==rownum){
      protections[i].remove()
      }
    }
  }
}

New contributor

Skanda Narayanan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

I changed this part of your code:

if (col == 21 && cell.getValue() == true) {//if changed to true protects row to end of data
  let protRange = sheet.getRange(rownum, 1, 1, 22).protect().setWarningOnly(true).setDescription(rownum);
}

Your code does not check for existing protections. As a result, every time the checkbox is checked, it creates a new protection, which causes the main problem. To handle duplicate protections for the same row, I use the some() method before creating a new one to avoid duplicate entries.

I also converted rownum to a string using toString(), to ensure compatibility with setDescription().

Complete Code:

function onEdit() {
  let ss = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = ss.getActiveSheet();
  let cell = sheet.getActiveCell();
  let col = cell.getColumn();
  let rownum = cell.getRow();

  if (col == 21 && cell.getValue() === true) {
    let protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
    let isAlreadyProtected = protections.some(protection => {
      return protection.getRange().getRow() === rownum && protection.getRange().getNumRows() === 1;
    });

    if (!isAlreadyProtected) {
      sheet.getRange(rownum, 1, 1, 22).protect()
        .setWarningOnly(true)
        .setDescription(rownum.toString());
    }
  } else if (col == 21 && cell.getValue() === false) {
    let protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
    for (let i = protections.length - 1; i >= 0; i--) {
      if (protections[i].getDescription() === rownum.toString()) {
        protections[i].remove();
      }
    }
  }
}

Sample Output:

Reference:

Class Protection

some()

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật