conditional protection : this +that = lock

Viewed 20

I have a script (below) that will protect a cell from alteration, by another user, that is cell background color dependent. However, it is too broad a solution. Can it be altered to be a combination of events? ie: "Lock cell from change if "cell is this color + contains any (*) characters" ?

function protectColor() {
  const spreadsheet = SpreadsheetApp.getActive();
  const sheets = spreadsheet.getSheets();
  sheets.forEach(sheet => { // Loop through sheets in spreadsheet
    const maxRow = sheet.getMaxRows(); // Get last row in sheet
    const maxColumn = sheet.getMaxColumns(); // Get last column in sheet
    const backgrounds = sheet.getRange(10,10,maxRow,maxColumn).getBackgrounds(); // Get cell colors
    for (let i = 0; i < backgrounds.length; i++) { // Loop through rows in sheet
      for (let j = 0; j < backgrounds[i].length; j++) { // Loop through current row
        if (backgrounds[i][j] === "#a4c2f4") { // Change if necessary
          const ltblueCell = sheet.getRange(i+1, j+1); // Get light blue cell
          const protection = ltbluCell.protect(); // Protect cell
          const me = Session.getEffectiveUser();
          protection.addEditor(me);
          protection.removeEditors(protection.getEditors());
          if (protection.canDomainEdit()) {
            protection.setDomainEdit(false);
          }
        }
      }
    }
  });
}
0 Answers
Related