0

For the most part of this code I have down. The only issue I am having is formatting a phone number that has been inputted to the Google Form. On the Spreadsheet the values come in looking like 1234567890, just a string of numbers. I am using body.replaceText("%PHONE1%", row[2]); to replace %PHONE1% in the template document. Issue is it numbers come in the same as the spreadsheet 1234567890 and I want the phone number to look like (123) 456-7890. It may possibly something like

Utilities.formatString('%11.6f', 123.456);

Please help, im a super newb at any type of scripting any help would be appreciated. Thank you

  • I just realized that Utilities.formatString is completely wrong. But there may be another option to add script to the form so that as they input the phone number it automatically formats... – Good Vibe DJs Nov 13 '17 at 22:39
  • 1
    Good answer with regexes: https://stackoverflow.com/questions/17651207/mask-us-phone-number-string-with-javascript – Dean Ransevycz Nov 13 '17 at 22:45

1 Answers1

0

Here's a simple example:

function formatPhoneNumber()
{
  var s='1234567890';
  if(s)
  {
    var mA=s.match(/(\d{3})(\d{3})(\d{4})/);
    var s=Utilities.formatString('(%s) %s-%s', mA[1],mA[2],mA[3]);
    var ui=HtmlService.createHtmlOutput(s);
    SpreadsheetApp.getUi().showModelessDialog(ui, 'Phone Number')
  }
}
Cooper
  • 59,616
  • 6
  • 23
  • 54