Saturday, October 20, 2012

Google Spreadsheet Script to accept a maximum number of characters



Question:


I am trying to create a spreadsheet to be used in scheduling tweets, and I want to condition the cells to accept 120 characters or less so that I don't have to open Twitter/hootsuite to make sure they are the right length.  How can I do this? Either a warning or an error message would work. Thanks!

Solution:


Following is the script that will show you warning ( as comment ), that is an error message, in the current cell if you exceed the limit of 120 characters.


function onEdit(e)
{
  var activeCell = SpreadsheetApp.getActiveSpreadsheet().getActiveCell();
  var activeCellValue = activeCell.getValue();
  var length = String(activeCellValue).length;
  
  if(length>120)
  {
    activeCell.setComment("Length is greater than 120 characters, Length is: " + length );
    Browser.msgBox("Length is greater than 120 characters, Length is: " + length );
  }
}


And If you are not much familiar with scripts then check out the following link:


I hope the above solution will help you, and if you need more help then please do comment below on this blog itself, I will try to help you out.


I also take up private or confidential projects:

If this blog post was helpful to you, and if you think you want to help me too and make my this blog survive then please donate here: http://igoogledrive.blogspot.com/2012/09/donate.html 


Thanks,
Kishan,

2 comments:

  1. Hi! Is there a way to limit the number of characters typed into a text field of a Google Form?

    ReplyDelete
  2. Hey - this is great, but (and I apologise for being a dunce, abuse kindly accepted ;-) how do I set this up to just affect certain parts of a specific spreadsheet, i.e only one column? Thank you

    ReplyDelete