# Easy Chart exportExcel rename headers?

**URL:** <https://forum.inductiveautomation.com/t/easy-chart-exportexcel-rename-headers/83223>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [December 16, 2023, 8:46am UTC](https://forum.inductiveautomation.com/t/easy-chart-exportexcel-rename-headers/83223 "2023-12-16T08:46:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![taohongyu88](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@taohongyu88](https://forum.inductiveautomation.com/u/taohongyu88)\
**Post date:** [December 16, 2023, 8:46am UTC](https://forum.inductiveautomation.com/t/easy-chart-exportexcel-rename-headers/83223/1 "2023-12-16T08:46:36Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/f/4ff2bc026e12fe397f2e8d807198db976952b210.png)  
button script:

```auto
easyChart = event.source.parent.getComponent('ChartContainer').getComponent('Easy Chart')
easyChart.exportExcel('myFileName.xls')

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/9/595f388766f5d4b99f62142adbe7c2af08cf27f9.png)  
I want to change the header from 'Cut/jobpress' to 'jobpress'?  
How to change?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 16, 2023, 8:57am UTC](https://forum.inductiveautomation.com/t/easy-chart-exportexcel-rename-headers/83223/2 "2023-12-16T08:57:24Z")

</div>

Tip: [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [December 16, 2023, 1:00pm UTC](https://forum.inductiveautomation.com/t/easy-chart-exportexcel-rename-headers/83223/3 "2023-12-16T13:00:38Z")

</div>

Rather than use exportExcel directly, you will need to use exportDatasets from [Vision - Easy Chart Scripting Functions - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/Vision+-+Easy+Chart+Scripting+Functions).

Once you have your dataset(s) then using this example to update the column headers: [Rename the column of a dataset - #2 by lrose](https://forum.inductiveautomation.com/t/rename-the-column-of-a-dataset/56043/2)

Then you can use [system.dataset.exportExcel - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/system.dataset.exportExcel) to export the data to excel.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [December 16, 2023, 1:16pm UTC](https://forum.inductiveautomation.com/t/easy-chart-exportexcel-rename-headers/83223/4 "2023-12-16T13:16:24Z")

</div>

> [@taohongyu88](#):
>
> `easyChart.exportExcel('myFileName.xls')`

[One approach that I've used for this](https://forum.inductiveautomation.com/t/easy-chart-vision/72209/8) involves getting the data from the chart indirectly, and then, changing the header names before using `system.dataset.exportExcel`/`CSV` to save the file. Here is an example that has been adapted for this use case:

```auto
# Get the easyChart component
easyChart = event.source.parent.getComponent('ChartContainer').getComponent('Easy Chart')

# This function extracts the row data from the supplied dataset.
# ...Then, it creates a new dataset using the provided headers
def renameHeaders(dataset, headers):
	values = [[dataset.getValueAt(row,col) for col in range(dataset.columnCount)] for row in range(dataset.rowCount)]
	return system.dataset.toDataSet(headers,values)

# Get the tag paths column as a java list
tagPaths = list(easyChart.tagPens.getColumnAsList(easyChart.tagPens.getColumnIndex('TAG_PATH')))

# Create a list with the desired header names
headers = ['Time Stamp', 'jobpress']

# Get the displayed start date and end date from the chart
startTime = easyChart.zoomedStartDate
endTime = easyChart.zoomedEndDate

# Get the easy chart's dataset indirectly with a tag history query using the easy chart's parameters
tagHistory = system.tag.queryTagHistory(paths=tagPaths, startDate=startTime, endDate=endTime, returnSize=-1, aggregationMode="Maximum", returnFormat='Wide', noInterpolation=True)

# Use the renameHeaders function to replace the headers that were returned by the tag history query
dataset = renameHeaders(tagHistory, headers)

# Create an xls file using the modified dataset
system.dataset.exportExcel('myFileName.xls', True, dataset)

```

**Result:**  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/d/6d04587fead96a86b88732d592d88927936adaeb.png)

**Edit:**  
@bschroeder posted his reply while I was putting mine together, so I didn't notice it. I tested the `exportDatasets` he listed and was pleased to see that it also returns only the displayed _[zoomed]_ values. Here is the above example adapted to use that method:

```auto
# Get the easyChart component
easyChart = event.source.parent.getComponent('ChartContainer').getComponent('Easy Chart')

# This function extracts the row data from the supplied dataset.
# ...Then, it creates a new dataset using the provided headers
def renameHeaders(dataset, headers):
	values = [[dataset.getValueAt(row,col) for col in range(dataset.columnCount)] for row in range(dataset.rowCount)]
	return system.dataset.toDataSet(headers,values)

# Get the tag paths column as a java list
tagPaths = list(easyChart.tagPens.getColumnAsList(easyChart.tagPens.getColumnIndex('TAG_PATH')))

# Create a list with the desired header names
headers = ['Time Stamp', 'jobpress']

# Use the renameHeaders function in conjunction with exportDatasets
# ...to produce a dataset with the desired header names
dataset = renameHeaders(easyChart.exportDatasets()[0], headers)

# Create an xls file using the modified dataset
system.dataset.exportExcel('myFileName.xls', True, dataset)

```

---

<div class="post-metadata">

**Author:** ![taohongyu88](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@taohongyu88](https://forum.inductiveautomation.com/u/taohongyu88)\
**Post date:** [December 16, 2023, 2:03pm UTC](https://forum.inductiveautomation.com/t/easy-chart-exportexcel-rename-headers/83223/5 "2023-12-16T14:03:34Z")

</div>

Thank you very much!  
I used this example in exchange.  
[Ignition Exchange | Inductive Automation](https://inductiveautomation.com/exchange/92/overview)

So in fact, the headers is dynamic.  
I don't konw how to get the dynamic headers in easy chart.

```auto

# Create a list with the desired header names
headers = ['Time Stamp', 'CUT/jobprss'] # I don't konw how to get the dynamic headers in easy chart

# 定义headers映射
headersMapping = {
    'CUT/jobprss': u'jobprss',
    'COOLER/UTLM16/V2': u'冷冻机组/M16冰水出水温度',
    'COOLER/UTLM16/V3': u'冷冻机组/M16冰水回水温度',
    'COOLER/UTLM17/V2': u'冷冻机组/M17冰水出水温度',
    'COOLER/UTLM17/V3': u'冷冻机组/M17冰水回水温度'
}

# 修改headers
newHeaders = [headersMapping.get(name, name) for name in headers]

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/9/3937fb9b23da1c71a7c7b0cb0e2fb051691435d8.png)

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [December 16, 2023, 2:12pm UTC](https://forum.inductiveautomation.com/t/easy-chart-exportexcel-rename-headers/83223/6 "2023-12-16T14:12:01Z")

</div>

It looks like you could get the headers you want dynamically by doing this:

```auto
# Get the pen names, some of which contain '/' this character
penNames = easyChart.tagPens.getColumnAsList(0)

# Set the first header to t_stamp
# ...and get anything after the last '/' character in each pen name
headers = ['t_stamp']
for name in penNames:
	headers.append(name.split('/')[-1])

```

**Edit:** Almost forgot that the column headers have to include the time stamp

Also, if you need to filter out disable pens, the code could be modified in this way:

```auto
for row, name in enumerate(penNames):
	# Filter out disabled pens
	if easyChart.tagPens.getValueAt(row, 'Enabled'):
		headers.append(name.split('/')[-1])

```

---

<div class="post-metadata">

**Author:** ![taohongyu88](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@taohongyu88](https://forum.inductiveautomation.com/u/taohongyu88)\
**Post date:** [December 16, 2023, 3:07pm UTC](https://forum.inductiveautomation.com/t/easy-chart-exportexcel-rename-headers/83223/7 "2023-12-16T15:07:29Z")

</div>

Thank You very much!🤗

```auto
penNames = easyChart.tagPens.getColumnAsList(1)
print(penNames)

# Set the first header to t_stamp
# ...and get anything after the last '/' character in each pen name
headers = ['t_stamp']
for name in penNames:
    headers.append(name.split(']')[-1].upper())

#print(headers)

# Define headers mapping as a list of tuples
headersMapping = [
    (u'CUT/PRESS', u'剪切机/压力'),
    (u'CUT/INTEMP', u'剪切机/进料温度'),
    (u'CUT/OUTEMP', u'剪切机/出料温度'),
    (u'CUT/JOBPRESS', u'剪切机/工作压力')
]

# Modify headers
newHeaders = [next((new_name for old_name, new_name in headersMapping if old_name == header), header) for header in headers]

print(newHeaders)

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/7/f789b2abeb1cdad9cf244070088e59f23f385656.png)
