Add condition to short Office Script

ChristineJ

Well-known Member
Joined
May 18, 2009
Messages
775
Office Version
  1. 365
Platform
  1. Windows
I'm using this code in Office Scripts so that cell E15 is not locked when the sheet is protected.

selectedSheet.getRange("E15").getFormat().getProtection().setLocked(false);

Cell B2 contains a text string that includes numbers, such as CJJS621597.

I'd like to update the Script so that cell E15 is set to unlocked only if the 7th character in the string in B2 is a 1 (possibly incorporating something like MID($B$2,7,1)="1" into the script?)

Can that be done? Thanks!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
it might be easier to put the =MID formula in a cell with an IF statement so that your Office Script code can say something like this (assuming you put this formula on the same sheet in cell E1000:

JavaScript:
function main(workbook: ExcelScript.Workbook) {
    let selectedSheet = workbook.getActiveWorksheet();

    let cellVal = selectedSheet.getRange("E1000").getValue();

if (cellVal === "Yes") {
    return selectedSheet.getRange("E15").getFormat().getProtection().setLocked(false);
}

}

and cell E1000 should say something like:

Excel Formula:
=IF(MID($B$2,7,1)="1","Yes","No")
 
Upvote 0
Solution
Yes, I can use approach you're suggesting. Thanks!

Can I ask you one more thing? (I'm very new to Office Scripts.) I am trying to add a line to turn the protection on by adding the following code before the very last closing curly bracket in your code above:

selectedSheet.getProtection().protect();

Your script works, but the protection is not added to the sheet. (No password is necessary.) Am I putting it in the wrong place?

Thanks for the help!! C
 
Upvote 0
In that case you would get rid of the return statement and do the following:

JavaScript:
function main(workbook: ExcelScript.Workbook) {
    let selectedSheet = workbook.getActiveWorksheet();

    let cellVal = selectedSheet.getRange("E1000").getValue();

    if (cellVal === "Yes") {
        selectedSheet.getRange("E15").getFormat().getProtection().setLocked(false);
        selectedSheet.getProtection().unprotect();
        selectedSheet.getProtection().protect();
    }

}
 
Upvote 0
EXCELLENT! Exactly what I need. I really appreciate your sticking with me on this. C
 
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,227
Members
453,025
Latest member
Hannah_Pham93

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top