In Libreoffice BASIC, if we type Sub LockThisSheet LockSheet("Sheet1") End Sub Function LockSheet(passedSheetname) Dim oDoc As Object : oDoc = ThisComponent Dim oSheet As Object : oSheet = oDoc.Sheets.getByName(passedSheetname) Dim pPassword As String If IsMissing(pPassword) Then pPassword = "SomePassword" oSheet.UnProtect(pPassword) oSheet.Protect(pPassword) End Function Sheet is protected without the password. Version: 7.2.0.0.alpha0+ (x64) / LibreOffice Community Build ID: 6b15a8658f369e4144251854bcdb736acb595f47 CPU threads: 4; OS: Windows 10.0 Build 19042; UI render: default; VCL: win Locale: en-IN (en_IN); UI: en-US Calc: threaded
The problem is with > If IsMissing(pPassword) Then pPassword = "SomePassword" which does not set the password pPassword to "SomePassword". Runtime function "IsMissing" tests if a function is called with an _optional_ parameter and function "LockSheet" has no optional parameter called pPassword. Therefore pPassword stays empty and protection is activated with an "empty" password. Change the code to something like: >Sub LockThisSheet > LockSheet("Sheet1") >End Sub > >Function LockSheet(passedSheetname, optional pPassword ) > > Dim oDoc As Object : oDoc = ThisComponent > Dim oSheet As Object : oSheet = oDoc.Sheets.getByName(passedSheetname) > > If IsMissing(pPassword) Then pPassword = "SomePassword" > oSheet.UnProtect(pPassword) > oSheet.Protect(pPassword) > >End Function From my perspective not a bug
(In reply to Uwe Auer from comment #1) > The problem is with > > > If IsMissing(pPassword) Then pPassword = "SomePassword" > > which does not set the password pPassword to "SomePassword". Runtime > function "IsMissing" tests if a function is called with an _optional_ > parameter and function "LockSheet" has no optional parameter. called > pPassword. Therefore pPassword stays empty and protection is activated with > an "empty" password. Change the code to something like: > > >Sub LockThisSheet > > LockSheet("Sheet1") > >End Sub > > > >Function LockSheet(passedSheetname, optional pPassword ) > > > > Dim oDoc As Object : oDoc = ThisComponent > > Dim oSheet As Object : oSheet = oDoc.Sheets.getByName(passedSheetname) > > > > If IsMissing(pPassword) Then pPassword = "SomePassword" I think that in this line it says that if the pPassword is missing set the pPassword to "SomePassword". Since here pPassword is missing, pPassword is set to "SomePassword". > > oSheet.UnProtect(pPassword) > > oSheet.Protect(pPassword) Here it protects the sheet with pPassword > > > >End Function > > From my perspective not a bug
(In reply to An-Kh from comment #2) I did understand your code, but probably you did not understand my answer. To be precise and clear: Your code is wrong and I explained why. > If IsMissing(pPassword) Then pPassword = "SomePassword" is not doing what you understand / assume it is doing. It doesn *not* test whether variable is set. Runtime function IsMissing() tests for *optional* parameter in your function definition (i.e. it tests, whether *pPassword* has been passed to function LockSheet(passedSheetname)) - but your function doesn't have that optional parameter at all. Therefore provided a solution (which you obviously did not check) to change: - Function LockSheet(passedSheetname) + Function LockSheet(passedSheetname, optional pPassword ) In other words: The declaration of your function *LockSheet* is incorrect with respect of using runtime function *IsMissing()* Explanation - means "remove" from your code + means "add" to your code
(In reply to An-Kh from comment #2) > > Therefore provided a solution (which you obviously did not check) to change: > Change to my previous comment: - Dim pPassword As String - Function LockSheet(passedSheetname) + Function LockSheet(passedSheetname, optional pPassword )
Hi I think that in this case, some kind of dialog box should pop up telling that Dim pPassword As String If IsMissing(pPassword) is not allowed. I saw this code in the Bug 133257
note: https://opengrok.libreoffice.org/xref/core/basic/source/runtime/methods.cxx?r=3482f590#2500
(In reply to An-Kh from comment #5) > I saw this code in the Bug 133257 You saw it in a bug, which indicates that it did not work
(In reply to Uwe Auer from comment #7) > (In reply to An-Kh from comment #5) > > > I saw this code in the Bug 133257 > > You saw it in a bug, which indicates that it did not work That bug was unrelated to this one. It was related to UI in Calc during protection
(In reply to himajin100000 from comment #6) > note: > > https://opengrok.libreoffice.org/xref/core/basic/source/runtime/methods. > cxx?r=3482f590#2500 Hii I saw the code in the above link. It should give an error message if the parameters are entered wrongly in IfMissing(). But here it is neither giving the error message nor the protection is applied.
IMHO, It looks at least to me that in order to give compile-time error "IsMissing" has to be a language construct rather than a function.
Imho, the behaviour is correct, since, if IsMissing is used on a non optional parameter it always gives false.
But we could throw a runtime error ...
(In reply to Andreas Heinisch from comment #12) > But we could throw a runtime error ... Yess.. I think some kind of indication should be there
(In reply to Andreas Heinisch from comment #12) > But we could throw a runtime error ... that's ok, but then it is not a bug but an enhancement request. I stick with the statements that the code is wrong and documentation is clear about correct usage of IsMissing - it tests whether a optional function parameter has been passed to the function or not and that. And the function as defined in the report has no optional parameter which could be tested.
Could be a valid enhancement request, but it may break exisiting (but wrong) macros. However, the function does what the documentation states.
(In reply to Andreas Heinisch from comment #15) > Could be a valid enhancement request, but it may break exisiting (but wrong) > macros. However, the function does what the documentation states. Moving to NEW then