Advertisement
Guest User

Untitled

a guest
Mar 31st, 2025
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | Source Code | 0 0
  1. function ProtectAndUnprotect(e) {
  2. // Allow the function to run only for authorized users
  3. var userEmail = Session.getActiveUser().getEmail();
  4. if (!authorizedEmails.includes(userEmail)) return;
  5.  
  6. var sheet = e.source.getActiveSheet();
  7. var sheetName = sheet.getName();
  8. // Skip processing for specific sheets
  9. if (sheetName === "Settings" || sheetName.endsWith("-M") ||
  10. sheetName === "Shop Template" || sheetName === "Monthwise Template" ||
  11. sheetName === "Summary") return;
  12.  
  13. var range = e.range;
  14. var row = range.getRow();
  15. var col = range.getColumn();
  16. var value = range.getValue();
  17. var numRows = range.getNumRows();
  18.  
  19. // Only process edits in column 5 ("Issued") or column 7 ("Passed")
  20. if (col !== 5 && col !== 7) return;
  21.  
  22. try {
  23. if (col === 5) { // "Issued" checkbox: protect columns 1-4 and column 8
  24. var primaryRange = sheet.getRange(row, 1, numRows, 4);
  25. var additionalRange = sheet.getRange(row, 8, numRows, 1);
  26.  
  27. if (value === true) {
  28. protectRanges([primaryRange, additionalRange]);
  29. range.setBackground('lightgreen');
  30. } else if (value === false) {
  31. unprotectRanges([primaryRange, additionalRange]);
  32. range.setBackground(null);
  33. }
  34. } else if (col === 7) { // "Passed" checkbox: protect column 6
  35. var targetRange = sheet.getRange(row, 6, numRows, 1);
  36.  
  37. if (value === true) {
  38. protectRanges([targetRange]);
  39. range.setBackground('lightgreen');
  40. } else if (value === false) {
  41. unprotectRanges([targetRange]);
  42. range.setBackground(null);
  43. }
  44. }
  45. } catch (error) {
  46. Logger.log("Error processing edit: " + error.message);
  47. }
  48. }
  49.  
  50. function protectRanges(ranges) {
  51. ranges.forEach(function(range) {
  52. // Protect the range and remove all editors for maximum security
  53. var protection = range.protect().setDescription('Protected by script');
  54. protection.removeEditors(protection.getEditors());
  55. range.setBackground('lightgreen');
  56. });
  57. }
  58.  
  59. function unprotectRanges(ranges) {
  60. // Assume all ranges are on the same sheet
  61. var sheet = ranges[0].getSheet();
  62. var protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
  63. var protectionMap = {};
  64.  
  65. // Build a hash map keyed by A1 notation for quick lookup
  66. protections.forEach(function(protection) {
  67. var key = protection.getRange().getA1Notation();
  68. if (protectionMap[key]) {
  69. protectionMap[key].push(protection);
  70. } else {
  71. protectionMap[key] = [protection];
  72. }
  73. });
  74.  
  75. // Remove protections for the specified ranges
  76. ranges.forEach(function(range) {
  77. var key = range.getA1Notation();
  78. if (protectionMap[key]) {
  79. protectionMap[key].forEach(function(protection) {
  80. protection.remove();
  81. });
  82. }
  83. range.setBackground(null);
  84. });
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement