简体   繁体   中英

Protect row according to the data in cell (Yes/No)

I have a sheet with a table. When me or one of my editors changes the data of the cell in column F (so that it is not blank), I need the row to become protected for everybody except me (owner).

在此处输入图像描述

I have seen several similar questions here, but no one gives a working script... I will appreciate any help.

I believe your current situation and your goal as follows.

  • Your Spreadsheet is shared with some users.
  • When the user is edited the dropdown list of the column "F", when the value is not empty, you want to protect the row of columns "A" to "F".
  • After the row was protected, you want to edit by only the owner.
  • You want to achieve this using Google Apps Script.

In this case, I would like to propose to run the script with the installable OnEdit trigger. The sample script is as follows.

Sample script:

Please copy and paste the following script to the script editor of Spreadsheet and please install the OnEdit trigger to the function of myFunction .

function myFunction(e) {
  const sheetName = "Sheet1"; // Please set your sheet name.

  const range = e.range;
  const sheet = range.getSheet();
  const value = range.getValue();
  const row = range.getRow();
  if (sheet.getSheetName() != sheetName || range.getColumn() != 6 || row == 1 || value == "") return;
  const p = sheet.getRange(`A${row}:F${row}`).protect();
  const owner = Session.getActiveUser().getEmail();
  p.getEditors().forEach(f => {
    const email = f.getEmail();
    if (email != owner) p.removeEditor(email);
  });
}
  • In this script, it supposes that the dropdown list is put to the cells "F2:F" from your sample image. If you want to change the range, please modify above script.
  • When you use this script, by an user who is not owner , please edit the dropdown list of the column "F" of "Sheet1". By this, when the value of dropdown list is not empty, the script works. And, the row is protected. The editor is only the owner.

References:

Added:

About your following 2nd question in comment ,

thank you very much,. It works? and I know now something about triggers - a new thing for me, Is it possible to extend this function for 3 sheets with the same structure (within one spreadsheet), Sheet1. Sheet2.Sheet 3...

When you want to use above script for the specific sheets like "Sheet1", "Sheet2", "Sheet3", how about the following sample script?

Sample script:

function myFunction(e) {
  const sheetNames = ["Sheet1", "Sheet2", "Sheet3"]; // Please set the sheet names you want to run the script.

  const range = e.range;
  const sheet = range.getSheet();
  const value = range.getValue();
  const row = range.getRow();
  if (!sheetNames.includes(sheet.getSheetName()) || range.getColumn() != 6 || row == 1 || value == "") return;
  const p = sheet.getRange(`A${row}:F${row}`).protect();
  const owner = Session.getActiveUser().getEmail();
  p.getEditors().forEach(f => {
    const email = f.getEmail();
    if (email != owner) p.removeEditor(email);
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM