# Add table columns via script

**URL:** <https://forum.inductiveautomation.com/t/add-table-columns-via-script/87215>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [March 12, 2024, 7:32pm UTC](https://forum.inductiveautomation.com/t/add-table-columns-via-script/87215 "2024-03-12T19:32:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [March 12, 2024, 7:32pm UTC](https://forum.inductiveautomation.com/t/add-table-columns-via-script/87215/1 "2024-03-12T19:32:25Z")

</div>

Probably I need to use Python Class and _init_??  
My Python powers cannot make it. Need help.

For the life of me, cannot figure this out.

Step 1: I copied column Object to columnObject Variable:

```auto
	columnObject = {
	  "field": "",
	  "visible": True,
	  "editable": False,
	  "render": "auto",
	  "justify": "auto",
	  "align": "center",
	  "resizable": True,
	  "sortable": True,
	  "sort": "none",
	  "filter": {
	    "enabled": False,
	    "visible": "on-hover",
	    "string": {
	      "condition": "",
	      "value": ""
	    },
	    "number": {
	      "condition": "",
	      "value": ""
	    },
	    "boolean": {
	      "condition": ""
	    },
	    "date": {
	      "condition": "",
	      "value": ""
	    }
	  },
	  "viewPath": "",
	  "viewParams": {},
	  "boolean": "checkbox",
	  "number": "value",
	  "progressBar": {
	    "max": 100,
	    "min": 0,
	    "bar": {
	      "color": "",
	      "style": {
	        "classes": ""
	      }
	    },
	    "track": {
	      "color": "",
	      "style": {
	        "classes": ""
	      }
	    },
	    "value": {
	      "enabled": True,
	      "format": "0,0.##",
	      "justify": "center",
	      "style": {
	        "classes": ""
	      }
	    }
	  },
	  "toggleSwitch": {
	    "color": {
	      "selected": "",
	      "unselected": ""
	    }
	  },
	  "numberFormat": "0,0.##",
	  "dateFormat": "MM/DD/YYYY",
	  "width": "",
	  "strictWidth": False,
	  "style": {
	    "classes": ""
	  },
	  "header": {
	    "title": "",
	    "justify": "left",
	    "align": "center",
	    "style": {
	      "classes": ""
	    }
	  },
	  "footer": {
	    "title": "",
	    "justify": "left",
	    "align": "center",
	    "style": {
	      "classes": ""
	    }
	  }
	}

```

step 2: Create empty list

```auto
columnList = []

```

step 3: using for loop, append columnObject to columnList and edit "field" member

```auto
for colName in ds.getColumnNames():
		colObject = {}
		colObject = columnObject
		colObject['field'] = colName
		columnList.append(colObject)

```

For the life of me.. can't make it work..  
The resulting list will have same name on "Field".

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 12, 2024, 7:37pm UTC](https://forum.inductiveautomation.com/t/add-table-columns-via-script/87215/2 "2024-03-12T19:37:29Z")

</div>

When you assign an object to something in python, you are **pointing** at it. You need to construct a **new** column object **within** the loop. Making "deep" copy of a sample is one approach, but I tend to just make the dictionary fresh inside the loop.

(Keep in mind that some of the keys are optional. Omitting the ones you don't need will greatly simplify your life.)

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [March 12, 2024, 7:56pm UTC](https://forum.inductiveautomation.com/t/add-table-columns-via-script/87215/3 "2024-03-12T19:56:58Z")

</div>

> [@pturmel](#):
>
> You need to construct a **new** column object **within** the loop.

How do you assign a new object to a variable?

> [@pturmel](#):
>
> (Keep in mind that some of the keys are optional. Omitting the ones you don't need will greatly simplify your life.)

I only need to amend field and render.  
Does this mean my template object should only have field and render keys?

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [March 12, 2024, 8:10pm UTC](https://forum.inductiveautomation.com/t/add-table-columns-via-script/87215/4 "2024-03-12T20:10:22Z")

</div>

funny i did it.

```auto
colObject = columnObject.copy()

```

But not clear to me how to only involve the key needed.

```auto
	columnObject1 = {
	  "field": ""
	}

```

this would work I guess.

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [March 12, 2024, 8:10pm UTC](https://forum.inductiveautomation.com/t/add-table-columns-via-script/87215/5 "2024-03-12T20:10:55Z")

</div>

```python
	return [
		{
			"field": columnName,
			"visible": True,
			"editable": False,
			"render": "auto",
			"justify": "auto",
 			"align": "center",
			"resizable": True,
			"sortable": True,
			"sort": "none",
			"filter": {
				"enabled": False,
				"visible": "on-hover",
				"string": {
					"condition": "",
					"value": ""
				},
				"number": {
					"condition": "",
					"value": ""
				},
				"boolean": {
					"condition": ""
				},
				"date": {
					"condition": "",
					"value": ""
				}
			},
			"viewPath": "",
			"viewParams": {},
			"boolean": "checkbox",
			"number": "value",
			"progressBar": {
				"max": 100,
				"min": 0,
				"bar": {
					"color": "",
					"style": {
						"classes": ""
					}
				},
				"track": {
					"color": "",
					"style": {
						"classes": ""
					}
				},
				"value": {
					"enabled": True,
					"format": "0,0.##",
					"justify": "center",
					"style": {
						"classes": ""
					}
				}
			},
			"toggleSwitch": {
				"color": {
					"selected": "",
					"unselected": ""
				}
			},
			"nullFormat": {
				"includeNullStrings": False,
				"strict": False,
				"nullFormatValue": ""
			},
			"numberFormat": "0,0.##",
			"dateFormat": "MM/DD/YYYY",
			"width": "",
			"strictWidth": False,
			"style": {
			"classes": ""
			},
			"header": {
				"title": columnName,
				"justify": "left",
				"align": "center",
				"style": {
					"classes": ""
				}
			},
			"footer": {
				"title": "",
				"justify": "left",
				"align": "center",
				"style": {
					"classes": ""
				}
			}
		} for columnName in value.getColumnNames()]

```
