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

后端 未结 2 1010
佛祖请我去吃肉
佛祖请我去吃肉 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

    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.

提交回复
热议问题