Add table columns via script

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:

	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

columnList = []

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

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".

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.)

How do you assign a new object to a variable?

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

funny i did it.

colObject = columnObject.copy()

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

	columnObject1 = {
	  "field": ""
	}

this would work I guess.

	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()]
3 Likes