Hello,

I need your help with a solution to making a function trigger automatically.

What i need to do is format input into a range to a certain mask as 
follows: 0xxx-xxx-xxx (phone number that has to start with 0 and its 
separated every 3 digits - not counting the leading 0).

In excel i can set this mask \0#00-000-000.

Whats the easiest way to do it in spreadsheets?

I found and edited the following function and it works as a pull down 
function:

function SCHANGE(array) {
  if(typeof(array) == 'number') {
    var number = array.toString();
  } else {
    var number = array.replace(/\D/g,'');
  }

  if(number.length != 12) {
    return "<> 12"
  } else {
    var first = number.substring(0,4);
    var mid = number.substring(4,8);
    var last = number.substring(8,12);
    var formText = first + " - " + mid + " - " + last;
  }
  return formText;
}

I tried with no succes to convert this function to auto change every value 
in a certain range:
function SCHANGE(array) {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getActiveSheet();
  var cell = sheet.getRange("B2:B10");  
  var number = sheet.getRange("B2:B10").getValues();
  for (var i = 0; i < 9 ; ++i) {

     number[i] = number[i].toString();
      var first = number[i].substring(0,3);
      var mid = number[i].substring(3,6);
      var last = number[i].substring(6,9);
      
      var formText = "0"+first + "-" + mid + "-" + last;
     number[i].setValues(formText); 

    
 }
}

I am sure my syntax is wrong.

Can anyone please help with this issue?

Thank you

-- 
You received this message because you are subscribed to the Google Groups 
"Google Spreadsheets API" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to