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
How to insert a row in a ZenTable at a specific row index?
-
- Posts: 16
- Joined: Mon May 25, 2020 9:53 am
-
- 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
Hello Fredrik Olsson,I have a ZenTable and I want to insert this row at a certain row index.
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)
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.
-
- Posts: 16
- Joined: Mon May 25, 2020 9:53 am
Re: How to insert a row in a ZenTable at a specific row index?
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
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
-
- Posts: 180
- Joined: Wed May 20, 2020 10:10 am
Re: How to insert a row in a ZenTable at a specific row index?
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:
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)
-
- Posts: 1
- Joined: Sat May 27, 2023 11:12 am
Re: How to insert a row in a ZenTable at a specific row index?
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
Regards
mapquest driving directions
Re: How to insert a row in a ZenTable at a specific row index?
It worked, I followed this guide and it worked.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)
-
- Posts: 23
- Joined: Tue Jun 20, 2023 10:20 pm
Re: How to insert a row in a ZenTable at a specific row index?
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/
If anyone want to know about epoxy flake flooring tampa visit here https://www.epoxyfloorsoftampa.com/