1

I'm currently working on this thread. The code seems to work with the active sheet, but how can I make it work if the button for lock and unlock are located on another sheet?

This is the sheet where the button is located. So, column B is the sheet name.

picture

Is it possible for the button in column C to only work with the sheet in line with the button?

Here's the code:

function Unlock() { var sheet = SpreadsheetApp.getActiveSheet(); var protection = sheet.protect().setDescription('Sample protected range'); var unprotected = protection.getUnprotectedRanges(); unprotected.push(sheet.getRange('F9:O52')); unprotected.push(sheet.getRange('S9:AB52')); protection.setUnprotectedRanges(unprotected); var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); } } function Lock() { var sheet = SpreadsheetApp.getActiveSheet(); var protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0]; if (protection && protection.canEdit()) { protection.remove(); } LockSheet(); } function LockSheet() { var sheet = SpreadsheetApp.getActiveSheet(); var protection = sheet.protect().setDescription('Sample protected sheet'); var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); } } 
6
  • In your situation, can you provide the scripts for "LOCK" and "UNLOCK" buttons for each row? Commented Nov 27, 2020 at 0:15
  • @Tanaike I have already edited it. Kindly see. Thank you. Commented Nov 27, 2020 at 0:22
  • Thank you for replying and adding the script. For example, in your image, what functions are you using for "LOCK" and "UNLOCK" buttons of the rows 10 and 11? Commented Nov 27, 2020 at 0:26
  • @Tanaike I'm thinking of putting the Unlock() and Lock(). I still don't know how to execute it. Commented Nov 27, 2020 at 0:30
  • Thank you for replying. From your replying and question, I proposed a modified flow for your situation as an answer. Could you please confirm it? If that was not the direction you expect, I apologize. Commented Nov 27, 2020 at 0:47

1 Answer 1

2

Modification points:

  • From your question and replyings, I understood that you might be using the functions of Lock() and Unlock() for all "LOCK" and "UNLOCK" buttons, respectively. In this case, your script uses the active sheet. By this, even when each button is clicked, the active sheet is used. I think that this is the reason of your issue.
  • When you want to run the script for each sheet like "MTB_Q1", "MTB_Q2",,, by clicking each button, it is required to prepare each function for each button. Because in the current stage, when the button is clicked, the information of clicked button cannot be retrieved by the event object and so on.

When above points are reflected to your situation, I would like to propose the following flow.

Modified flow:

1. Prepare script.

In this case, it is required to prepare each function for each button. So when your script is modified, it becomes as follows. Please copy and paste the following script.

// These are used for the buttons of "LOCK" and "UNLOCK" at the row 10 in your image. const lock_row10 = () => Lock("MTB_Q1"); const unlock_row10 = () => Unlock("MTB_Q1"); // These are used for the buttons of "LOCK" and "UNLOCK" at the row 11 in your image. const lock_row11 = () => Lock("MTB_Q2"); const unlock_row11 = () => Unlock("MTB_Q2"); // These are used for the buttons of "LOCK" and "UNLOCK" at the row 12 in your image. const lock_row12 = () => Lock("MTB_Q3"); const unlock_row12 = () => Unlock("MTB_Q3"); // These are used for the buttons of "LOCK" and "UNLOCK" at the row 13 in your image. const lock_row13 = () => Lock("MTB_Q4"); const unlock_row13 = () => Unlock("MTB_Q4"); // IMPORTANT: If you have more buttons, please add the functions like above. function Unlock(sheetName) { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); var protection = sheet.protect().setDescription('Sample protected range'); var unprotected = protection.getUnprotectedRanges(); unprotected.push(sheet.getRange('F9:O52')); unprotected.push(sheet.getRange('S9:AB52')); protection.setUnprotectedRanges(unprotected); var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); } } function Lock(sheetName) { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); var protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0]; if (protection && protection.canEdit()) { protection.remove(); } LockSheet(sheet); } function LockSheet(sheet) { var protection = sheet.protect().setDescription('Sample protected sheet'); var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); } } 

2. Set functions to buttons.

In your image, please set each function name to each button as follows.

  • Set lock_row10 and unlock_row10 to the buttons of "LOCK" and "UNLOCK" at the row 10 in your image.
  • Set lock_row11 and unlock_row11 to the buttons of "LOCK" and "UNLOCK" at the row 11 in your image.
  • Set lock_row12 and unlock_row12 to the buttons of "LOCK" and "UNLOCK" at the row 12 in your image.
  • Set lock_row13 and unlock_row13 to the buttons of "LOCK" and "UNLOCK" at the row 13 in your image.

If you have more buttons, please add the functions like above.

3. Testing

After above settings were done, when you click a button, the sheet name corresponding to each row is used and the script is run with the sheet name.

Sign up to request clarification or add additional context in comments.

6 Comments

@cjvdg Thank you for replying. I'm glad your issue was resolved. From your question and additional information, I could correctly understand about your question. Thank you, too.
Hello! I can't make it work on the editor side, it only works when I'm the owner. Can you help me with this? It says that Exception: You are trying to edit a protected cell or object. Please contact the spreadsheet owner to remove protection if you need to edit.
I remember I asked that before, so I tried your solution: stackoverflow.com/a/64689629/6729785 but it gives me another problem that says Exception: Request failed for https://script.google.com returned code 500. Truncated server response: <!DOCTYPE html><html lang="en"><head><meta name="description" content="Web word processing, presentations and spreadsheets"><meta name="viewport" c... (use muteHttpExceptions option to examine full response)
@cjvdg About your new question, I have to apologize for my poor English skill. Unfortunately, I cannot understand about your new question. But I would like to support you. So can you post it as new question by including more information? By this, I would like to confirm it, and it will help users think of the solution. If you can cooperate to resolve your issue, I'm glad.
Here's the link to my new question: stackoverflow.com/q/65031013/6729785
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.