Tuesday, March 11, 2025

Power Query - funcao limpa replace texto numero

//fnc_limpa_texto

(TextValue as nullable text) =>

let

    SafeText = if TextValue = null then "" else TextValue,

    DigitsOnly = Text.Combine(

        List.Select(Text.ToList(SafeText), each Text.Contains("0123456789", _))

    )

in

    DigitsOnly

    

//fnc_limpa_numeros

(TextValue as nullable text) =>

let

    SafeText = if TextValue = null then "" else TextValue,


    // Tabela com caracteres acentuados e suas substituições

    AccentsList = {

        {"à","a"},{"á","a"},{"â","a"},{"ã","a"},{"ä","a"},

        {"è","e"},{"é","e"},{"ê","e"},{"ë","e"},

        {"ì","i"},{"í","i"},{"î","i"},{"ï","i"},

        {"ò","o"},{"ó","o"},{"ô","o"},{"õ","o"},{"ö","o"},

        {"ù","u"},{"ú","u"},{"û","u"},{"ü","u"},

        {"À","A"},{"Á","A"},{"Â","A"},{"Ã","A"},{"Ä","A"},

        {"È","E"},{"É","E"},{"Ê","E"},{"Ë","E"},

        {"Ì","I"},{"Í","I"},{"Î","I"},{"Ï","I"},

        {"Ò","O"},{"Ó","O"},{"Ô","O"},{"Õ","O"},{"Ö","O"},

        {"Ù","U"},{"Ú","U"},{"Û","U"},{"Ü","U"},

        {"ç","c"},{"Ç","C"},{"ñ","n"},{"Ñ","N"}

    },


    // Primeiro remove os acentos explicitamente

    WithoutAccents = List.Accumulate(

        AccentsList, 

        SafeText, 

        (text, pair) => Text.Replace(text, pair{0}, pair{1})

    ),


    // Remove todos os caracteres não alfabéticos (pontuação, especiais, números)

    CleanedText = Text.Combine(

        List.Select(

            Text.ToList(WithoutAccents), 

            each Text.Contains("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", _)

        ), ""

    )

in

    CleanedText   

No comments:

Post a Comment

Power Query - funcao limpa replace texto numero

//fnc_limpa_texto (TextValue as nullable text) => let     SafeText = if TextValue = null then "" else TextValue,     DigitsOnly...