-1

I have a call log DB in google spreadsheets that is generated by a IFTTT Applet. This DB is then sent to a private web API by means of GAS with url.fetch commands(if your interested you can find a Post Here with the code). For relevance. Using a onChange trigger for automation, I'm reformatting the data and then fetching the lastROW to API every time a new Row is inserted by IFTTT.

It's working, but i have a problem with the phone number format. The format I'm getting from IFTTT is not consistent, for example ~80% of phone numbers have a format like this "40727000000"(11 digits no.) or "727000000"(9 digits no.), rest of them are international phone no. or robot phone no. formatted like this "393280000000" (12 digits no.) or less then 9 digits no. for robot. Now the format I send to API needs to be a 10 digits no. like 0727000000 (but only for those ~80% of numbers, the international and robot numbers need's to remain the same).

So in essence i need to write a function in GAS with 3 scenarios:

  1. If it has 12 digits or less then 9 digits, do nothing.

  2. If it has 11 digits delete the first digit, in my case is always "4".

  3. If it has 9 digits add a "0" in front of "7", it's always "7".

Thank you!

Community
  • 1
  • 1
  • I'm sorry because i asked such a stupid question. But my skills with coding, of any kind are null, I manage to did this script with a lot of help from different users from this forum and ton of searching on net. So again sorry for insulting anyone!!! – Lucian Medisan Apr 26 '17 at 21:15

1 Answers1

0

Here is a solution:

function formatDigits(number) {

  var num = number.toString();
  var length = num.length;

  if(num.length == 11){

    num = num.substring(1,11);

    return num;

  }
  if(num.length == 9){

    num = "0" + num;

    return num;

  }

  return 0; // or return num; Depending how you want to deal with other case

}
Pierre-Marie Richard
  • 1,914
  • 2
  • 19
  • 25
  • Thank you, i'l mark as accepted answer but you did some mistakes. Here is the working 'code'function reformatDigits(number) { var num = number.toString(); var length = num.length; if(length == 11){ num = num.substring(1,11); return num; } if(length == 9){ num = "0" + num; return num; } if(length > 12 && length < 9); num = num; return num; }'code' – Lucian Medisan Apr 26 '17 at 21:02
  • For the case where you've got less than 9 digits or more than 12, you specify to do nothing, so I just return 0. Otherwise, in your code, you don't specify how you're dealing with a 10 digits number: you can replace by function reformatDigits(number) { var num = number.toString(); var length = num.length; if(length == 11){ num = num.substring(1,11); return num; } if(length == 9){ num = "0" + num; return num; } return num; } – Pierre-Marie Richard Apr 27 '17 at 08:19