tableau / document-api-python

Create and modify Tableau workbook and datasource files
https://tableau.github.io/document-api-python/
MIT License
331 stars 177 forks source link

Data Source does not save new xml tree when changing fields properties #163

Closed CooperData closed 2 years ago

CooperData commented 5 years ago

My apologies if I am doing something wrong; I am using the development branch and it seems the xml tree is not getting updated after setting some of the properties. The field properties are correctly set but I believe the Datasource.save() method is not really updating the tree with the new properties of the fields.

This what I did:

sourceTDS = Datasource.from_file('datasource_file.tdsx')
for count, field in enumerate(sourceTDS.fields.values()):
    if field.caption == None:
        print('changing field {} to caption {}'.format(field.name, field.name.replace('_',' ').title()))
        field.caption = field.name.replace('_',' ').title()

for count, field in enumerate(sourceTDS.fields.values()):
    if field.caption == None:
        print('field {} still with empty caption'.format(field.name))
        # returns nothing

sourceTDS.save()

However, after loading the data source again. The captions were not saved. #103

CooperData commented 5 years ago

checking more on this, it seems that what this might be missing is adding an element to the xml tree similar to this:

<column caption="Search Total Children" datatype="string" name="[SEARCH_TOTAL_CHILDREN]" role="dimension" type="nominal" />

Which is missing from the xml when updating alias and caption. Instead the values that get added are in the metadata-record element:

      <metadata-record alias="Search Total Children" caption="Search Total Children" class="column">
        <remote-name>SEARCH_TOTAL_CHILDREN</remote-name>
        <remote-type>129</remote-type>
        <local-name>[SEARCH_TOTAL_CHILDREN]</local-name>
        <parent-name>[V_IMPRESSION_ANALYZER_MAIN_MINI]</parent-name>
        <remote-alias>SEARCH_TOTAL_CHILDREN</remote-alias>
        <ordinal>147</ordinal>
        <local-type>string</local-type>
        <aggregation>Count</aggregation>
        <width>16777216</width>
        <contains-null>true</contains-null>
        <collation flag="0" name="binary" />
        <attributes>
          <attribute datatype="string" name="DebugRemoteType">"SQL_VARCHAR"</attribute>
          <attribute datatype="string" name="DebugWireType">"SQL_C_CHAR"</attribute>
          <attribute datatype="string" name="TypeIsVarchar">"true"</attribute>
        </attributes>
      </metadata-record>
jacalata commented 2 years ago

I'm sure you have long since moved on, but I believe this was actually implemented back in 2016, not sure why it wasn't working for you at the time. When I tried just now, if I add a line to the show_fields sample of field.caption = "NEW LINE", this caption then shows up in the saved file.