Monday, August 27, 2012

Validating a Cell having extra blank spaces

Validating a Cell having extra blank spaces


Question:

I am using data validation for a "Day of the week" column using items from a list.  Here is my list:

Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,

However, the people entering information are constantly entering a space after the day, eg "Sunday "  instead of "Sunday."

The data validation doesn't catch this.  What can I do?  Is there a way to force them to choose from a list instead of allowing them to type or choose?

Is there a way to tell data validation to not accept spaces?



Solution:

This can be done with the help of the following script:

I have written this following script for only Cell H2 and only for "Sunday". 
I have also put Data validation on Cell H2 for it should be only from list: 
Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday


function onEdit(e)
{
 myFunction();
}

function myFunction()
{  
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet = ss.getSheetByName("mainsheet"); // your sheet name
  var range = sheet.getDataRange();
  var CellValue = range.offset(1, 7, 1, 1).getValue(); // Cell H2
  
  if (CellValue.match(/.*Sunday/))
  {
    range.offset(1, 7, 1, 1).setValue("Sunday");
  }  
}

The above script will execute on every time when a user will edit this spreadsheet.
If user inputs "Sunday  " it will change it to "Sunday".

I hope this script helps you in validating your data.


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.

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,

No comments:

Post a Comment