Convert Macro excel to Google Sheets script

Asked

Viewed 247 times

-1

I would like a help to transform this macro code from excel to the Google Sheets script.

I don’t know the resources so much, I’ve cracked my head moving but it didn’t work.

Basically he removes the accents, the idea and that if he finds any accented word, he replaces it for the letter without that accent.

Function faxineiro(caract)
    codiA = " àáâãäèéêëìíîïòóôõöoùúûüÀÁÂÃÄÈÉÊËÌÍÎOÒÓÔÕÖÙÚÛÜçÇñÑ."
    codiB = "_aaaaaeeeeiiiioooooouuuuAAAAAEEEEIIIOOOOOOUUUUcCnN-"
    temp = caract
        For i = 1 To Len(temp)
        p = InStr(codiA, Mid(temp, i, 1))
            If p > 0 Then Mid(temp, i, 1) = Mid(codiB, p, 1)
        Next
        faxineiro = temp
    End Function

1 answer

0


This script will do this on all worksheets, if you want to do it only on a specific worksheet you will need to make some changes.

function onEdit(e){
  const ss = e.source;
    let value = e.value;
    let range = e.range;
    value = value.replace(new RegExp("[[áàâäã]", 'g'),"a");
    value = value.replace(new RegExp("[[ÁÀÂÄÃ]", 'g'),"A");
    value = value.replace(new RegExp("[[éèêë]", 'g'),"e");
    value = value.replace(new RegExp("[[ÉÈÊË]", 'g'),"E");
    value = value.replace(new RegExp("[[ìíîï]", 'g'),"i");
    value = value.replace(new RegExp("[[ÌÍÎ]", 'g'),"I");
    value = value.replace(new RegExp("[[òóôõöo]", 'g'),"o");
    value = value.replace(new RegExp("[[OÒÓÔÕÖ]", 'g'),"O");
    value = value.replace(new RegExp("[[ùúûü]", 'g'),"u");
    value = value.replace(new RegExp("[[ÙÚÛÜ]", 'g'),"U");
    value = value.replace(new RegExp("[[çÇ]", 'g'),"c");
    value = value.replace(new RegExp("[[Ç]", 'g'),"C");
    value = value.replace(new RegExp("[[ñÑ]", 'g'),"n");
    value = value.replace(new RegExp("[[Ñ]", 'g'),"N");
    range.setValue(value);

}

EDITED: The code below works too.

function onEdit(e){
    const ss = e.source;
    let range = e.range;
    let value = e.value;
    let changeCase = {'a': '[àáâãäå]', 'ae': 'æ', 'c': 'ç', 'e': '[èéêë]', 'i': '[ìíîï]', 'n': 'ñ', 'o': '[òóôõö]', 'oe': 'œ', 'u': '[ùúûűü]', 'y': '[ýÿ]'};
    for (i in changeCase) { value = value.replace(new RegExp(changeCase[i], 'g'), i); }
    range.setValue(value);
};
  • Thank you very much. I’ll try

Browser other questions tagged

You are not signed in. Login or sign up in order to post.