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

后端 未结 2 1011
佛祖请我去吃肉
佛祖请我去吃肉 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.
    0 讨论(0)
  • 2021-01-28 02:22

    If you don't want to capitalize if the cell contains a formula, you can use the method getFormula() and check if the cell contains a formula.

    Returns the formula (A1 notation) for the top-left cell of the range, or an empty string if the cell is empty or doesn't contain a formula.

    The code should look like this:

    if (ind === 0 && e.range.rowStart > 1 && e.range.columnStart >= 1  && e.range.getFormula() == '') {
      e.range.setValue(e.value.toUpperCase());
    }
    

    EDIT:

    If I've understood you correctly, you're typing exactly the same value, example: if the value in the cell is México, and you delete all or some characters and inmediately type México again, in that scenario the old value and the new value are the same and the OnEdit() won't be fired. Another example is if you change the format of the value, that's another type of event.

    If you want know how the event is considered, you can use an installable on change trigger:

    function triggerOnChange(e) {
      MailApp.sendEmail('john.doe@gmail.com', 'Testing triggerOnChange', JSON.stringify(e));
    }
    

    Then in the Script Editor menu: Resources -> Current Project Triggers -> Add a new trigger -> ['triggerOnChange', 'From spreadsheet', 'On change']

    On how to change the case of the formula's result, I think @Rubén has the right idea, but it will only work if the formula contains UPPER() in the first characters, and also since you're using the formula IMPORTHTML() using UPPER() will break it and maybe some other functions like array formulas, unless you use INDEX():

    =INDEX(UPPER(IMPORTHTML(url, query, index)))
    

    Another option could be Regular expressions, but I think it's a little risky considering all the combinations.

    0 讨论(0)
提交回复
热议问题