Issue 126950 - Cannot change URL textfield in Calc cell when referenced via paragraph text portions enumeration
Summary: Cannot change URL textfield in Calc cell when referenced via paragraph text p...
Status: CLOSED FIXED
Alias: None
Product: App Dev
Classification: Unclassified
Component: api (show other issues)
Version: 4.1.2
Hardware: PC Linux 64-bit
: P5 (lowest) Normal
Target Milestone: ---
Assignee: AOO issues mailing list
QA Contact:
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-05-03 17:53 UTC by Volker Lenhardt
Modified: 2017-05-20 09:32 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Latest Confirmation in: ---
Developer Difficulty: ---


Attachments
Contains macros to show the bug changing URL textfield values (9.41 KB, application/vnd.oasis.opendocument.spreadsheet)
2016-05-03 17:53 UTC, Volker Lenhardt
no flags Details

Note You need to log in before you can comment on or make changes to this issue.
Description Volker Lenhardt 2016-05-03 17:53:23 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
Comment 1 mroe 2016-05-03 19:33:18 UTC
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
Comment 2 Volker Lenhardt 2016-05-03 21:37:34 UTC
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
Comment 3 Volker Lenhardt 2016-05-04 17:34:57 UTC
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
Comment 4 mroe 2016-05-04 19:06:16 UTC
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.
Comment 5 Volker Lenhardt 2016-05-05 09:31:57 UTC
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
Comment 6 mroe 2016-05-07 13:50:46 UTC
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).
Comment 7 Volker Lenhardt 2016-05-08 08:59:29 UTC
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.