Files
spqr/power-query/fGetSheet.m
2025-10-24 14:22:52 +03:00

40 lines
1.8 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

let
GetSheetTable = (tblSource as table, anySheetName as any, optional intSheetNum as nullable number) as table =>
let
intSheet = if intSheetNum = null then 0 else intSheetNum,
// Преобразуем список объектов в список имен листов
// ToDo: добавить сортировку для листов/именованных таблиц или сделать обработку именованных таблиц отдельно
SheetNames = Table.Column(tblSource, "Name"),
// Функция для поиска первого совпадения по имени
FindSheetByName =
if Value.Is(anySheetName, type text) then
if List.Contains(SheetNames, anySheetName) then
Table.SelectRows(tblSource, each [Name] = anySheetName){0}[Data]
else
null
else if Value.Is(anySheetName, type list) then
let
ExistingNames = List.Intersect({anySheetName, SheetNames}),
SheetData = if List.Count(ExistingNames) > 0 then
Table.SelectRows(tblSource, each [Name] = ExistingNames{0}){0}[Data]
else
null
in
SheetData
else
null,
// Если по имени не найдено, возвращаем по номеру
Result =
if FindSheetByName <> null then
FindSheetByName
else if intSheet >= 0 and intSheet < Table.RowCount(tblSource) then
tblSource{intSheet}[Data]
else
error "Не удалось подобрать лист"
in
Result
in
GetSheetTable