Is it possible to detect an error when attempting to use the removeRange method? For example, the method will fail if part of the range is within a merged cell. Can the caller detect that the operation failed?
The method doesn’t throw. My idea would be maybe use XUndoManagerListener around the call, to somehow detect, is the Undo action was actually added or not (I don’t know if it would actually work).
If cells cannot be deleted, nothing is written to Undo (see attachment).
If B1 is replaced with A1, the deletion is performed.
TestRemoveRange.ods (10.0 KB)
That is exactly the idea: to check if Undo was created (=success), or not (=the call failed), which is what @Discloop wanted to detect.
I don’t even think about such winding detours.
A more straightforward way is to file an enhancement request for a throwing variant, or a variant returning a boolean (even simpler), or otherwise signaling failure (of course, a compelling use case is required in the request description). That is trivially doable (creating a derived interface, with the new function); but indeed, that would only work later, when it’s implemented and released… @Discloop could even implement it themself, with our guidance (to not depend on someone who finds it interesting enough to implement, maybe in ten years from now…)
Thanks for the replies. I did solve this by saving data in the cell(s) to be deleted, then re-checking afterwards to see if they have changed. Not fool-proof, but works for now.
@mikekaganski 's solution is simple:
bSuccess = (ThisComponent.UndoManager.CurrentUndoActionTitle = "Delete")
Perfect! I verified that this solves the problem. Many thanks!
@Villeroy that would depend on previous undo action being different. Besides, it would be localization dependent, and also would break on the UI string change.
Test this sample, which shows at some curious case of “succeeded with errors” removing A1 in a sequence:
TestRemoveRange_listener.ods (10.9 KB)
But this approach (generally with the Undo) also depends on the Undo being enabled (not guaranteed).
Let’s try another way with the same idea.
' lang:en
' Deletes cells from a rectangular range and returns True on success.
' - oRange cell range (SheetCellRange object).
' - nMode describes how to move following cells.
' - see [CellDeleteMode](https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1sheet.html#af2bbbff47b7abe36f258e59b1351e422).
Function RemoveRange(ByVal oRange As Object, ByVal nMode As Long) As Boolean
Dim oDoc As Object, oSheet As Object, isMod As Boolean
oSheet=oRange.SpreadSheet
oDoc=oSheet.DrawPage.Forms.Parent
isMod=oDoc.isModified()
If isMod Then oDoc.setModified False
On Error GoTo ErrLabel
oSheet.removeRange oRange.RangeAddress, nMode
On Error GoTo 0
If oDoc.isModified() Then
RemoveRange=True
Exit Function
End If
ErrLabel:
If isMod Then oDoc.setModified True
RemoveRange=False
End Function
Nother idea: If all the cells have been deleted, the range does no longer exist, and rg.AbsoluteName raises an error.
Sub Main
Dim sTest As String
rg = thiscomponent.currentselection
sh = rg.getspreadsheet()
a = rg.getrangeaddress()
c = com.sun.star.sheet.CellDeleteMode.UP
sh.removeRange(a, c)
on error resume next
sTest = rg.AbsoluteName
on error goto 0
bSuccess = (len(sTest) = 0)
End Sub