Forum Discussion
Get all column headings for all sheets
- 4 years ago
Here is a function that gets the values in Row 1 for all sheets in a workbook. You can adapt it as needed. Create a blank query, open the advanced editor and replace the code there with this. That will create the function you can use in another query that shows 1+ excel file. On the Add Column tab, click on Invoke Custom Function and point this function at the Content column.
// let // Source = File.Contents("C:\Test\GetHeaders.xlsx"), (filecontent) => let Source = filecontent, OpenExcel = Excel.Workbook(Source, null, true), #"Filtered Rows" = Table.SelectRows(OpenExcel, each ([Kind] = "Sheet")), #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"Name", "Data"}), #"Added Custom" = Table.AddColumn(#"Removed Other Columns", "Headers", each Record.ToList([Data]{0})), #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Data"}), #"Expanded Custom" = Table.ExpandListColumn(#"Removed Columns", "Headers"), #"Changed Type" = Table.TransformColumnTypes(#"Expanded Custom",{{"Headers", type text}}) in #"Changed Type"The top lines with // are commented out. You can comment out the //(filecontent) line and uncomment those to adapt it.
Pat
Here is a function that gets the values in Row 1 for all sheets in a workbook. You can adapt it as needed. Create a blank query, open the advanced editor and replace the code there with this. That will create the function you can use in another query that shows 1+ excel file. On the Add Column tab, click on Invoke Custom Function and point this function at the Content column.
// let
// Source = File.Contents("C:\Test\GetHeaders.xlsx"),
(filecontent) => let Source = filecontent,
OpenExcel = Excel.Workbook(Source, null, true),
#"Filtered Rows" = Table.SelectRows(OpenExcel, each ([Kind] = "Sheet")),
#"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"Name", "Data"}),
#"Added Custom" = Table.AddColumn(#"Removed Other Columns", "Headers", each Record.ToList([Data]{0})),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Data"}),
#"Expanded Custom" = Table.ExpandListColumn(#"Removed Columns", "Headers"),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded Custom",{{"Headers", type text}})
in
#"Changed Type"
The top lines with // are commented out. You can comment out the //(filecontent) line and uncomment those to adapt it.
Pat
Brilliant this works perfectly, thank you!!
In the mean time, I also came across this video that works for the adding index part (just in case anyone else is interested):
https://www.youtube.com/watch?v=7CqXdSEN2k4
Your solution is much slicker though 🙂