help to convert a macro to office script

Suzanne Cadden 1 Reputation point
2022-07-20T14:54:23.343+00:00

From my reading, it seems that my macros will still work (when opened in desktop) in an excel file that is used by others in excel online. If that is the case, I have one macro that I need to convert to script. The file is very large and performs a lot of functions. Because of the number of lines, I have certain areas grouped but I want the formulas locked so no one can accidentally delete them but they need to view results. This script allows users to easily get to and ungroup the set of data they need to review without having to scroll through 8,000+ lines. Can anyone help me with this? Thanks in advance!!

Sub EnableOutliningWithProtection()

'Password Protect Current Sheet
ActiveSheet.Protect Password:="MDM258!", UserInterfaceOnly:=True

'Enable Group Collapse/Expand Capabilities
ActiveSheet.EnableOutlining = True

End Sub

Microsoft 365 and Office Development Other
{count} votes

1 answer

Sort by: Most helpful
  1. Brian Gonzalez 76 Reputation points
    2022-08-07T20:55:12.833+00:00

    You can try the code below. You'll have to specify the protection options in the protect json object. You can bring up the options available to you by using ctrl + space within the object. The options will tell you what value they expect. Here's an example of the code using protection selection mode. It expects an enum value of which the options are none, normal, and unlocked:

    function main(workbook: ExcelScript.Workbook) {  
      let selectedSheet = workbook.getActiveWorksheet();  
      if (selectedSheet.getProtection().getProtected() === false) {  
        selectedSheet.getProtection().protect({selectionMode:ExcelScript.ProtectionSelectionMode.none}, "MDM258!")  
      }  
    }  
    

    You may have to play around with the options and find an option which works for you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.