Apache OpenOffice (AOO) Bugzilla – Issue 126950
Cannot change URL textfield in Calc cell when referenced via paragraph text portions enumeration
Last modified: 2017-05-20 09:32:10 UTC
Created attachment 85519 [details] Contains macros to show the bug changing URL textfield values A Calc table cell contains a hyperlink as a URL textfield. I want to change its URL and Representation elements. So I enumerate the cell's text paragraph text portions and test if the actual textfield supports the service com.sun.star.text.TextField.URL: Sub WriteCellFieldURL() Dim oCell Dim oParEnum, oPar Dim oPortionsEnum, oPortion, oField oCell = ThisComponent.Sheets(0).getCellByPosition(0, 0) oParEnum = oCell.Text.createEnumeration() Do While oParEnum.hasMoreElements() oPar = oParEnum.nextElement() oPortionsEnum = oPar.createEnumeration() Do While oPortionsEnum.hasMoreElements() oPortion = oPortionsEnum.nextElement() If oPortion.TextPortionType = "TextField" Then oField = oPortion.TextField If oField.supportsService("com.sun.star.text.TextField.URL") Then Print oField.Representation & ": " & oField.URL oField.Representation = "New file" oField.URL = "file:///home/volker/newfile.pdf" Print oField.Representation & ": " & oField.URL 'New URL content Exit Do End If End If Loop Loop Print oCell.String 'Old cell content End Sub But when I try to change the contents of the URL textfield, these new values are not saved in the cell. If I rather enumerate the cell's textfields directly, I find that the actual field does not seem to support the service com.sun.star.text.TextField.URL. Sub WriteCellFieldURL() Dim oCell Dim oFieldsEnum, oField oCell = ThisComponent.Sheets(0).getCellByPosition(0, 0) oFieldsEnum = oCell.TextFields.createEnumeration() Do While oFieldsEnum.hasMoreElements() oField = oFieldsEnum.nextElement() Print "Field found" If oField.supportsService("com.sun.star.text.TextField.URL") Then Print oField.Representation & ": " & oField.URL oField.Representation = "New file" oField.URL = "file:///home/volker/newfile.pdf" Print oField.Representation & ": " & oField.URL Exit Do End If Loop End Sub So the field is not found. My workaround is to enumerate the textfields as above and use an error handler when reading the actual field's URL element. In this case the changes are saved! Sub WriteCellFieldURL() Dim oCell Dim oFieldsEnum, oField Dim sURL As String oCell = ThisComponent.Sheets(0).getCellByPosition(0, 0) oFieldsEnum = oCell.TextFields.createEnumeration() On Local Error Goto NoURL Do While oFieldsEnum.hasMoreElements() oField = oFieldsEnum.nextElement() sURL = oField.URL Print oField.Representation & ": " & oField.URL oField.Representation = "New file" oField.URL = "file:///home/volker/newfile.pdf" Print oField.Representation & ": " & oField.URL NoURL: Loop Print oCell.String End Sub Is it one bug or two bugs? I have attached a file showing the effect. The behaviors explained above are produced by starting the Subs WriteCellFieldURL1 through ...3 in its macro module. I tested with AOO 4.1.2 under Linux 64-Bit openSUSE 13.2 as well as under Windows 7. Volker
Sub WriteCellFieldURL() Dim oCell, oFields, oField Dim i As Integer oCell = ThisComponent.Sheets(0).getCellByPosition(0, 0) oFields = oCell.getTextFields() For i = 0 To oFields.getCount() - 1 oField = oFields.getByIndex( i ) MsgBox oField.getPropertyValue( "Representation" ) & ": " & oField.getPropertyValue( "URL" ) oField.setPropertyValue( "Representation", "New file" ) oField.setPropertyValue( "URL", "file:///home/volker/newfile.pdf" ) MsgBox oField.getPropertyValue( "Representation" ) & ": " & oField.getPropertyValue( "URL" ) Next i End Sub
Is there actually a difference between enumerating textfields and referencing them by index one after the other? In any case I have to ensure that the actual field is a URL textfield. If I read the URL property and there isn't any, an exception is thrown. But at this moment it leaps into my mind that there possibly doesn't exist any other textfield type beside URL in Calc cell texts. Is this right? If so then there really is no problem at all. Volker
I don't think that this bug is resolved fixed. When I reference a textfield either via enumeration or via indexing, the URL field neglects supporting the service com.sun.star.text.TextField.URL. What is this other than a bug? On the other hand when I enumerate the paragraph text portions to reference the URL field it correctly agrees to support the URL service, but changes to the URL and Representation properties are not saved in the cell. I consider this a bug. Volker
Please have a look at https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/TextPortion.html ----------- TextField [ readonly ] XTextField TextField; Usage Restrictions optional Description contains the text field of a text portion of type TextField. ----------- With the Enumeration you get a "SvxUnoTextField". Readonly, see above. With oCell.getTextFields().getByIndex( 0 ) you get a "ScCellFieldObj". https://www.openoffice.org/api/docs/common/ref/com/sun/star/text/XTextFieldsSupplier.html#getTextFields You should discuss this behaviour in an user forum.
Excuse me for being cumbersome. I did discuss the topic in the German openoffice forum. Well, I (we) overlooked that the textfield as a text portion is readonly. But you didn't respond to my complaint that the field as referenced by index or by cell textfields enumeration (I think both yield the same ScCellFieldObj) denies to support the URL service (oField.supportsService("com.sun.star.text.TextField.URL" = False)). But in fact it does, as this service is not optional. In the forum there was no explanation for this behavior. Volker
I'm not an AOO developer. But it seems that SvxUnoTextField is the visual representation (View) of ScCellFieldObj (Model). SvxUnoTextField supports "com.sun.star.text.TextField.URL" (and "com.sun.star.text.textfield.URL") whereas ScCellFieldObj it doesn't. ScCellFieldObj simply stores the needed values in /Representation/, /TargetFrame/ and /URL/. It doesn't know something about its visual representation. SvxUnoTextField implements com.sun.star.text.TextField.URL which reads the stored Values and uses /Format/ for the type of representation (text/button).
Thank you very much indeed for this clarifying explanation. It was important for me to understand the background. So I can correctly describe in a documentation how to handle cell URL textfields.