Script to automatically capitalize contents of a cell in Google Sheets?

后端 未结 2 1013
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-28 01:50

I have a spreadsheet that takes input of stock symbols. I would like them to always be in ALL CAPS regardless of how they are typed in. This appears to require some scripting

2条回答
  •  花落未央
    2021-01-28 02:22

    So, anyone know a way to do exactly what the above code does, but not touch the actual formulas inside the cells, only the text that they output?

    Consider to make a slight change in the OP approach: rather than capitalize all the cells content for any case, capitalize according the following conditions:

    1. If the cell value, including the values of cells that holds constants or formulas, is not a string then do nothing .
    2. If the cell value is a string
      • and the cell content is a constant, then change the case directly from the script.
      • and the cell content is a formula, then nest the original formula inside the built-in function UPPER.

    Example:

    function onEdit(e) {
      var range = e.range;
      var value = range.getValue();
      var sheet = range.getSheet();
      var sheetName = sheet.getName();
      if (sheetName === 'Sheet1' && 
          range.getRow() > 1 && 
          range.getColumn() > 1 && 
          typeof value === 'string') {
        if(!range.getFormula()) {
          range.setValue(value.toUpperCase());
        } else {
          if(range.getFormula().substring(0,6).toUpperCase() == '=UPPER') {
            return;
          } else {
            range.setFormula('=UPPER(' + range.getFormula().substring(1) + ')');
          }
        }
      }
    }
    

    Notes:

    • For simplicity the ind array was not included.
    • typeof e.value always returns 'string', so instead range.getValue(); is used.

提交回复
热议问题