I want to add a unique ID to a TextTable object (this is a requirement). I chose to use the Name property for this purpose. The problem is that when the document is in DOCX format, I assign a unique ID to the table’s Name property, save and close the document, and then reopen it, I find that the Name property of all tables has been reset, and the ID I set is lost. Is there any other way to add an ID to a table that will not be lost?
hii @gulixiang can you please explain a bit more what is your requirement to set UID for table?
I want to add custom labels to tables, but these labels should not be visible to users. The labels are used to identify differences between tables. For example, when a user edits within a table, different data validation can be performed based on the specific table. So I want to write the label data to the Name property of the TextTable. In reality, it can’t be written, and it’s lost when the document is reopened. I’ve actually tested other properties of TextTable as well, but none of them work, such as: TableInteropGrabBag, TableTemplateName, RowDescriptions, PageDescName.
Here is a Python code snippet:
from com.sun.star.beans import PropertyValue
textTables = XSCRIPTCONTEXT.getDocument().getTextTables()
table = textTables.getByIndex(0)
props = table.TableInteropGrabBag
new_property = PropertyValue(Name="id", Value="1")
props += (new_property,)
table.TableInteropGrabBag= props
hi @darshan Do you need me to add anything else?
I don’t think there is an easy way you can add random key-value pairs on a Writer table in a way that survives a DOCX edit in Word.
The TableInteropGrabBag is specifically for preserving info from DOCX (or other foreign formats) till you re-export to the same foreign format; so it’s expected that the content of that is not written to ODT or DOCX.
If you only need an ODT-only solution, you could perhaps do this with RDF metadata: LibreOffice Developer's Guide: Chapter 6 - Office Development - The Document Foundation Wiki although that doesn’t support tables, so you would annotate the first paragraph in the A1 cell or something like that.
Alternatively, if you find a DOCX markup that does what you need, it would be possible to check if Writer supports that (and if not, fix it). Hope this helps.
@vmiklos Hi I still can’t find a way to locate the unique identifier for tables, but I have a new idea. I noticed that the order of tables in the navigator is correct. It’s sorted according to the order of tables in the document. Is there a way to get the table array object from the navigator? I tried using TextDocument.TextTables but found that the order of tables here is random. I also tried iterating through the entire document to sort the tables, but this is too costly.
Good question. I’m not sure it’s intentional that the order of tables in the TextTables container is not sorted. Possibly you could take a look at the code yourself at core.git sw/source/core/unocore/unocoll.cxx (SwXTextTables) and/or submit a bugreport for that at Log in to Bugzilla
As you say, scanning the entire document is not cheap.