Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-macros

Google Sheets Function Converting a Parameter to Upper Case


If I pass a variable in Google Sheets Macros into a function, how do I convert that variable to upper case? Examples below do not work (BTW: these are just a subsets of a bigger function)

function testit(textComingIn){

var a = textComingIn.toUpperCase();

return a;

}

or 

function testit(textComingIn){

var a = textComingIn.value.toUpperCase();

return a;

}

Solution

  • If you are looking for a simple UpperCase converting function, not a function for use in spreadsheet cells. Try using this myUpper() function:

    function testNow() {
      var valorCelda = SpreadsheetApp.getActiveRange().getValue();
      Logger.log(valorCelda);
      Logger.log(myUpper(valorCelda));
      Logger.log(valorCelda.toUpperCase());  
    }
    function myUpper(datopar){
      return datopar.toUpperCase();
    }
    

    The Log for running this code (uses active cell)... to a text containing cell

    [20-03-27 20:28:41:614 CST] Dirección de correo electrónico
    [20-03-27 20:28:41:619 CST] DIRECCIÓN DE CORREO ELECTRÓNICO
    [20-03-27 20:28:41:621 CST] DIRECCIÓN DE CORREO ELECTRÓNICO
    

    On a number typed cell throws... TypeError: datopar.toUpperCase is not a function

    demonstrating auto typing in JS.

    So... use .toString() firt to stringify other values. Working code is:

    function testNow() {
      var valorCelda = SpreadsheetApp.getActiveRange().getValue();
      Logger.log(valorCelda);
      Logger.log(myUpper(valorCelda)); // 
      // Logger.log(valorCelda.toUpperCase());
    }
    function myUpper(datopar){
      return datopar.toString().toUpperCase();
    }