1

There's another question addressing how to protect the sheet but doesn't show how to unlock individual cells.

I can't just start recording & create a script (which usually show how it's done) because apparently, excel online still doesn't support protecting sheets from the web UI.

rrh
  • 808
  • 7
  • 12

1 Answers1

3

This took me a while to find out, so I'm answering my own question here for the benefit of others with the same need.

function main(workbook: ExcelScript.Workbook) {
  let sheet = workbook.getWorksheet("Sheet1");
  
  // will not let you lock/unlock cells if protected
  sheet.getProtection().unprotect();

  // just checking
  let locked = sheet.getRange("B9").getFormat().getProtection().getLocked()
  console.log('value of locked: ', locked)

  // unlock
  sheet.getRange("B9").getFormat().getProtection().setLocked(false)

  // sanity check
  locked = sheet.getRange("B9").getFormat().getProtection().getLocked()
  console.log('locked value after set to false: ',locked)
  
  sheet.getProtection().protect({
    allowFormatCells: false
    //allowFormatCells: true
  });
}
rrh
  • 808
  • 7
  • 12
  • +1; thanks for posting the answer. As part of the product team, may I ask if we could have done anything to find the answer faster/efficiently? Did you find the answer from the product documentation online or through trial and error? Thanks for help in advance. – Sudhi Ramamurthy Nov 16 '20 at 23:15
  • In this case, I found it with a combination of reading example scripts in the documentation plus trial and error. I guess, more examples for different scenarios could help. – rrh Nov 17 '20 at 17:52