Is it possible to put a limit to the length of cell content while entering data into a cell using Google Apps Script or any other way?

  • 1
    Not while entering. Upon leaving it is possible. – Jacob Jan Tuinstra Apr 19 '15 at 18:58

The following little script limits the cell input to 5 positions and adds the surplus as a note to the cell.


function onEdit(e) {
  var limit = 5;
  if(e.value.length > limit) {
    e.range.setValue(e.value.substring(0, limit) + "...");
    e.range.setNote("Remaining text was: \n....." + e.value.substring(limit));


On each and every edit, the onEdit(e) trigger is fired and every time it will check whether the cells value (e.value) has a length larger than 5. If so, the active cell will receive a new value of no more than 5 positions (setValue & substring(0,5)). The remainder of the text is added as a note.


enter image description here


I've created an example file for you: Limit cell input
Add the code under Tools > Script editor from the spreadsheet menu. In the script editor, paste the code and press the save button.

| improve this answer | |

Use a Data Validation with a custom formula.

If the cell to limit the value length is A1 and the length limit is 5, then the custom formula is



| improve this answer | |
  • How to combine this with Text Not Containing "XYZ, ABC" ? – shadowz1337 Feb 18 '19 at 7:39

The following formula limits the cell input to 10 positions.

If the cell to limit the value length is A2 and the length limit is 10, then the custom formula is



example 1 formula


example 1 result

Done. But if you wanna add an ellipsis after the result you can edit this following:

=CONCATENATE(""&LEFT(A2,10)&" ...")


example 2 result

| improve this answer | |

We can also use Data Validation with a custom formula to limit the length of a cell by its word count (aka, to set a maximum number of words in a cell).

If A1 is the cell we are limiting by word count, and 10 is the maximum number of words, then the formula is:

=COUNTA(SPLIT(A1, " "))‹=10


| improve this answer | |
  • 1
    Basically is the same as this other answer but with a convuluted formula. – Rubén Nov 22 '16 at 23:52
  • In fact that answer is to limit strings by number of characters, the formula I shared limits by words. But yes, much more convoluted. – GrapesInASalad Dec 4 '16 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.