Forum Discussion
Trim the spreadsheet name in "transform file" code
- 9 months ago
Hii nok
You can fix this by trimming the sheet name before filtering it. Excel.Workbook returns a table with a [Name] column, so instead of matching "BAN" exactly, match the trimmed name. Just change your step to:
BAN_Sheet = Source{[Item = Text.Trim("BAN"), Kind = "Sheet"]}[Data]This ensures that "BAN", "BAN " or " BAN" all resolve to the same sheet, and your append process won’t break due to extra spaces.
hi nok
You can handle this situation by searching dynamically for the sheet whose name trims down to “BAN”, instead of directly referencing Item="BAN".
Your current code expects the sheet name to be exactly “BAN”, so any name like “BAN ” or “ BAN” will break. The idea is to look through all sheets in the workbook, trim their names, and select the one whose trimmed name equals “BAN”.
Below is an updated version of your function using this approach:
(Parâmetro3 as binary) =>
let
Source = Excel.Workbook(Parâmetro3, null, true),
// Find the sheet where the trimmed name equals "BAN"
BAN_Entry =
List.First(
List.Select(
Source,
each Text.Trim([Item]) = "BAN" and [Kind] = "Sheet"
)
),
BAN_Sheet = BAN_Entry[Data],
#"Promoted Headers" = Table.PromoteHeaders(BAN_Sheet, [PromoteAllScalars = true])
in
#"Promoted Headers"Explanation of what this code does:
- Loads the Excel workbook.
- Loops through all sheets.
- Applies Text.Trim() to the sheet names.
- Selects the one where the trimmed name equals "BAN".
- Returns its data and promotes headers.
This way, it will work for:
- “BAN”
- “BAN ”
- “ BAN”
- any variation with extra spaces
If the file ever contains more than one sheet that trims to “BAN”, it will take the first one, so be sure your files follow the expected pattern.
If you need to handle multiple sheets with “BAN” inside the same file or add error handling, feel free to share a sample file and I can help adjust the function further.
Regards,
Nadeem Salam
If this answers your question, please mark it as a solution so it can help others.