How to convert strings to Proper case in Apps Script?

If you've worked with spreadsheets at a school or a company, you know that data can often be messy and not formatted correctly. One common issue that I've run into is that names are not capitalized correctly.

Google Sheets has a function called PROPER() that converts a string in your spreadsheet to Proper case, but unfortunately, there is no such function in Apps Script.

In this tutorial, you'll learn how to write a custom function to convert a string to Proper case.

/**
 * Convert a string to Proper case.
 
 * @param {string} str The string value to be converted.
 * @return The string value in Proper case.
 * @customfunction
*/
function PROPER_CASE(str) {
  if (typeof str != "string")
    throw `Expected string but got a ${typeof str} value.`;
  
  str = str.toLowerCase();

  var arr = str.split(/.-:?—/ );
  
  return arr.reduce(function(val, current) {
    return val += (current.charAt(0).toUpperCase() + current.slice(1));
  }, "");
}

Conclusion

In this tutorial, you learned how to write a custom function to convert a string to Proper case.

Thanks for reading!


Have feedback for me?

I'd appreciate any feedback you can give me regarding this post.

Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!