Skip to content

This is common part for sheet relations #1

@hohlick

Description

@hohlick

Source = Excel.Workbook(File.Contents(FullPath), false, true),
// leave sheets only
FilteredSheets = Table.SelectRows(Source, each ([Kind] = "Sheet")),
// sheets in PQ initially in appearance order, i.e. sheets index (despite visibility)
AddSheetsIndex = Table.AddIndexColumn(FilteredSheets, "Index", 1, 1),
// check SheetNames parameter
SheetNames = if SheetNames is text then {SheetNames} else if SheetNames is list then SheetNames else null,
// filter sheets by name if provided
FilteredByNames = if SheetNames = null or List.IsEmpty(SheetNames) then AddSheetsIndex else Table.SelectRows(AddSheetsIndex, each List.Contains(SheetNames, [Name])),
// UnZip file
UnZipped = Table.Buffer(fnUnZip(File.Contents(FullPath))),
/*
let
Source = Folder.Files(Folder),
file = Source{[Name = FileName, Folder Path = Folder & "\"]}[Content],
UnZippedFile = Table.Buffer(fnUnZip(file))
in
Table.Buffer(UnZippedFile),
*/
// relations id table for sheets
workbook =
let
Source = UnZipped,
Content = Source{[FileName ="xl/workbook.xml"]}[Content],
ImportedXML = Xml.Tables(Content,null,TextEncoding.Utf8),
sheetsTable = ImportedXML{[Name = "sheets"]}[Table],
sheetTable = sheetsTable{[Name = "sheet"]}[Table],
ExpandedRel = Table.ExpandTableColumn(sheetTable, "http://schemas.openxmlformats.org/officeDocument/2006/relationships", {"Attribute:id"}, {"Attribute:id"}),
typed = Table.TransformColumnTypes(ExpandedRel,{{"Attribute:name", type text}, {"Attribute:sheetId", Int64.Type}, {"Attribute:id", type text}})
in
typed,
// sheets relations id to XML target files
workbook_rels =
let
Source = UnZipped,
Filtered = Table.SelectRows(Source, each [FileName]="xl/_rels/workbook.xml.rels"),
GetXML = Table.TransformColumns(Filtered, {"Content", each Xml.Tables(_,null,65001)}),
XMLContent = GetXML{0}[Content]{[Name="Relationship"]}[Table],
FilteredSheetsRel = Table.SelectRows(XMLContent, each [#"Attribute:Type"] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet"),
Removed = Table.RemoveColumns(FilteredSheetsRel,{"Attribute:Type"})
in
Removed,
// merge relations id (via sheets name)
MergedRelationsID = Table.Join(FilteredByNames, {"Name"}, workbook, {"Attribute:name"}),
// join workbook relations
MergedRelationsTarget = Table.Join(MergedRelationsID,{"Attribute:id"},workbook_rels,{"Attribute:Id"}),

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions