How to insert a row in a ZenTable at a specific row index?

In this subforum specific Topics concerning ZEN Core, e.g workbenches, are discussed
Post Reply
Fredrik Olsson
Posts: 16
Joined: Mon May 25, 2020 9:53 am

How to insert a row in a ZenTable at a specific row index?

Post by Fredrik Olsson »

Hi,

I have a ZenTable and I want to add an extra row to this table with the same columns but I want to insert this row at a certain row index.
I know how to add a new row to a ZenTable but how to make a Datarow and insert that with the command table.Rows.InsertAt(Datarow, index)? When I manage to have a separate Datarow it complaints that it already belongs to a DataTable and I can not insert it.
Any advice?

Kind regards
Fredrik Olsson
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: How to insert a row in a ZenTable at a specific row index?GGG

Post by CarlZeissMicroscopy3 »

I have a ZenTable and I want to insert this row at a certain row index.
Hello Fredrik Olsson,

yes, you are right! The function table.NewRow() to generate a row that is not already inserted into the table is missing in the ZenTable class. That is the reason why table.InsertAt cannot be used ;-(

But I can offer you two workarounds:

The first one is very easy, but it only works if you are not dependent on the ZenTable class.
Using the function ToDataTable() transfers the content and structure of the ZenTable as a DataTable.
DataTable is a standard .Net class with a lot of capabilities, see contributions in the internet!

Here comes he code snippet to change from ZenTable to DataTable:

Code: Select all

zenTable = ZenTable("MyTable")
zenTable.Columns.Add('Key',str)
zenTable.Columns.Add('Value',int)

row = 0
zenTable.Rows.Add()
zenTable.SetValue(row,0,"V1")
zenTable.SetValue(row,1,1)

#Changing from ZenTable to DataTable

dataTable = zenTable.ToDataTable()
newRow = dataTable.[b]NewRow[/b]()
    
newRow[0] = "v2"
newRow[1] = 2
        
dataTable.Rows.InsertAt(newRow, 0)
If you want or have to stay with the ZenTable the second workaround is using the funtion InsertAt(zenTable, position, arguments):

Code: Select all


def InsertAt(zenTable, arguments, position):

    zenTable.Rows.Add()
    
    for row in reversed(range(position, zenTable.RowCount - 1)):
        for column in range(0, zenTable.ColumnCount):
            print column
            zenTable.SetValue(row+1,column,zenTable.GetValue(row, column))
    
    column = 0
    for item in arguments:
        zenTable.SetValue(position,column,item)
        column = column + 1



Zen.Application.Documents.RemoveAll(False)

zenTable = ZenTable("MyTable")
zenTable.Columns.Add('Key',str)
zenTable.Columns.Add('Value',int)

Zen.Application.Documents.Add(zenTable)

row = 0
zenTable.Rows.Add()
zenTable.SetValue(row,0,"V1")
zenTable.SetValue(row,1,1)


InsertAt(zenTable, ["V2", 2], 0)
InsertAt(zenTable, ["V3", 3], 0)
InsertAt(zenTable, ["V4", 4], 0)
InsertAt(zenTable, ["V5", 5], 2)

I hope that one of the workarounds is suited for your needs.

Thank you for your contribution!
This bug will be fixed so that NewRow is also available for ZenTable in future releases of Zen.
Fredrik Olsson
Posts: 16
Joined: Mon May 25, 2020 9:53 am

Re: How to insert a row in a ZenTable at a specific row index?

Post by Fredrik Olsson »

Thank you!!

I actually found a way to stay with the ZenTable.
Kind regards
Fredrik Olsson

zenTable = ZenTable("MyTable")
zenTable.Columns.Add('Key',str)
zenTable.Columns.Add('Value',int)

row = 0
zenTable.Rows.Add()
zenTable.SetValue(row,0,"V1")
zenTable.SetValue(row,1,1)


newRow = zenTable.Rows[0].Table.NewRow() #Can be any existent row number in ZenTable


newRow[0] = "v2"
newRow[1] = 2

zenTable.Rows.InsertAt(newRow, 0) #Can be inserted without complaining about already belonging to a Table.

Zen.Application.ActiveDocument = zenTable
CarlZeissMicroscopy3
Posts: 180
Joined: Wed May 20, 2020 10:10 am

Re: How to insert a row in a ZenTable at a specific row index?

Post by CarlZeissMicroscopy3 »

Hello Fredrik Olsson,

this is a very good workaround!

Just for someone reading the post in the future, this is what will come with the next releases:

Code: Select all

zenTable = ZenTable("MyTable")
zenTable.Columns.Add('Key',str)
zenTable.Columns.Add('Value',int)

newRow = zenTable.NewRow()
newRow[0] = "V1";
newRow[1] = 1;
zenTable.Rows.Add(newRow)

newRow = zenTable.NewRow()
newRow[0] = "V2";
newRow[1] = 2;
zenTable.Rows.InsertAt(newRow,0)
Celine Dion
Posts: 1
Joined: Sat May 27, 2023 11:12 am

Re: How to insert a row in a ZenTable at a specific row index?

Post by Celine Dion »

Thank you for your quick response. I already know how to insert a row into ZenTable at a specific row index.

Regards
mapquest driving directions
Cho Tuan
Posts: 1
Joined: Fri Aug 25, 2023 8:08 pm

Re: How to insert a row in a ZenTable at a specific row index?

Post by Cho Tuan »

CarlZeissMicroscopy3 wrote: Thu May 19, 2022 2:19 pm Hello Fredrik Olsson,

this is a very good workaround!

Just for someone reading the post in the future,wordle game, this is what will come with the next releases:

Code: Select all

zenTable = ZenTable("MyTable")
zenTable.Columns.Add('Key',str)
zenTable.Columns.Add('Value',int)

newRow = zenTable.NewRow()
newRow[0] = "V1";
newRow[1] = 1;
zenTable.Rows.Add(newRow)

newRow = zenTable.NewRow()
newRow[0] = "V2";
newRow[1] = 2;
zenTable.Rows.InsertAt(newRow,0)
It worked, I followed this guide and it worked.
Rose J Lever
Posts: 30
Joined: Tue Jun 20, 2023 10:20 pm

Re: How to insert a row in a ZenTable at a specific row index?

Post by Rose J Lever »

To insert a row at a specific index in a ZenTable, use the relevant programming or scripting language and the appropriate method or function provided by the ZenTable library. Typically, this involves specifying the desired index and the data for the new row, then invoking the insertion method. The exact syntax may vary based on the programming language and the ZenTable library in use. Refer to the library's documentation for specific guidance and examples tailored to the technology you're working with.
If anyone want to know about epoxy flake flooring tampa visit here https://www.epoxyfloorsoftampa.com/
Post Reply