Home > Enterprise >  How to know whether a cell contains Gujarati or English letters?
How to know whether a cell contains Gujarati or English letters?

Time:05-31

If the contents of the cells in Google sheet are as per the below image:

enter image description here

Then in Apps Script, how can we check whether it includes Gujarati letters or not.

function containsGujaratFont() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("SheetNameToWriteHere");
  var vals = sheet.getRange("B1:B3").getValues();
  vals.forEach(r => {
    Logger.log(includesGujaratiFont(r[0]));
  });
}

function includesGujaratiFont(str){
  // some coding to identify if str contains Gujarati characters or not
  if(itContainsGujaratiFont){
    return true;
  }else{
    return false;
  }
}

The log should be:

true
false
true

CodePudding user response:

For example, when this information is used, it is considered that the range of Unicode is \u0A80-\u0AFF. When this range is used in your script, it becomes as follows.

Modified script:

In this case, your function of includesGujaratiFont is modified.

function includesGujaratiFont(str){
  return /^[\u0A80-\u0AFF] $/.test(str);
}
  • In this modification, when all characters in a cell value are Gujarati, true is returned.

  • If you want to return true, at least when one of the characters in a cell value is Gujarati, please modify as follows.

    • From

        return /^[\u0A80-\u0AFF] $/.test(str);
      
    • To

        return /[\u0A80-\u0AFF]/.test(str);
      

References:

CodePudding user response:

You do not need to use app script to do this. You can use the DETECTLANGUAGE funtion within google sheets.

If you enter the following formula into cell B2, where your text is in A2, the formula will then output GU for Gujarati.

=detectlanguage(A2)    

The only limitation on this function is if the cell contains two lanagaues i.e. Gujarati and English it will only work for the first text element

  • Related