How to programmatically change cells that have been marked read-only?

Call SetLockReadOnly(FALSE) before changing read-only cells and after the changes are done call SetLockReadOnly(TRUE) again.

See also the FAQ “An exception-safe way for changing read-only cells” in the miscellaneous section.