How to prevent Google Forms from converting form input to scientific notation format -


i have simple script set sends emails based on google form entries using script-based vlookup contact emails. in cases, google forms converts longer numbers entered form field scientific notation. workaround have been using enter apostrophe before number - reason keeps cell formatted plaintext. find solution not require step.

the sheet has form single field, egcs. egcs field can contain combination of letters , numbers , may multi-line string. script sends email user onformsubmit egcs field entry in body of email. problem arises when try submit long string numbers , form entry variable converted scientific notation.

i need whatever user enters in egcs field appear entered on both responses 1 sheet , in body of email sent. here code:

function onformsubmit(e) {     var egcs = e.values[1];    var email = session.getactiveuser().getemail();  //replace google sheets formatted line breaks html line breaks display in email:    egcs = egcs.replace(/\n/g, '<br>');  //send email:       var subject = "this test";    var body = egcs;    mailapp.sendemail(email, subject, body, {htmlbody: body})    return    } 

if submit

6110523527643880  

...into form, number changed scientific notation format , appears 6.11052e+15 both on sheet , in email sent. if submit multi-line string such as:

6110523527643880 6110523527643880 6110523527643880 

...then script works fine , form field entry not converted (probably because google not consider number more). need appear entered whether or not form entry single line or multiple lines.

here example sheet / script / form. should public, please feel free test it.

form responses in forms (as opposed spreadsheets) store responses strings. trigger function grab response form string entered respondent.

function onformsubmit(e) {   // response sheet. first version works in contained script,   // second works in stand-alone scripts. //  var sheet = spreadsheetapp.getactivesheet();   var sheet = e.range.getsheet();    // url of associated form & open   var formurl = sheet.getparent().getformurl();   var form = formapp.openbyurl(formurl);    // response matching timestamp in event   var timestamp = new date(e.namedvalues.timestamp);   // note: there race condition between updates in forms , sheets.   // (often!) spreadsheet form submission trigger function invoked   // before forms database has completed persisting new responses.   // result, might no results when asking recent response.    // work around that, wait , try again.   var timetogiveup = 0;   {     if (timetogiveup > 0) utilities.sleep(1000); // sleep 1s on subsequent tries     timetogiveup++;     var responses = form.getresponses(timestamp);   } while (responses.length == 0 && (timetogiveup < 3));   logger.log("time give "+timetogiveup);   var response = responses[0]; // assume 1 response matches timestamp   var itemresponses = response.getitemresponses();   var egcsitemnumber = 1;  // indicates question appears in form   var egcs = itemresponses[egcsitemnumber-1].getresponse().tostring();    // have respondent typed, string.   // can used as-is in email, example.   var body = "the user entered: "+egcs;   mailapp.sendemail(      session.getactiveuser().getemail(),      "this test",      body    );    // preserve value in spreadsheet, must   // force remain string prepending tick (')   var egcscol = 2;   e.range.offset(0,egcscol-1,1,1).setvalue("'"+egcs); } 

note wrt race condition comment: actual work needed in area of code single line:

var responses = form.getresponses(timestamp); 

while playing this, found receiving exception, same noted in comments below answer...

cannot find method getresponses(object)

it turned out happened when function triggered form submission event, not when running editor/debugger simulated events. implies that, short period of time, response we're trying handle not returned call getresponses().

because of way shared documents implemented, there propagation delay change... that's time takes change in 1 view of asset propagate other views.

in situation, our trigger function has launched spreadsheet event, , opens view of form , tries read newest responses before view contains them.

a simple work-around sleep() period of time allow propagation complete.

utilities.sleep(5000);   // 5s pause var responses = form.getresponses(timestamp); 

simple, yes - inefficient, because we'd waiting when didn't need to. second problem determining how long long enough... , if changed tomorrow?

the chosen work-around retry getting responses if not successful first time. wait when doing retry. , won't wait forever - there's limiting condition applied, via timetogiveup. (we have added additional check success after loop, since next statement through exception if we've blown through our time limit, can let dirty work.)

var timetogiveup = 0; {   if (timetogiveup > 0) utilities.sleep(1000); // sleep 1s on subsequent tries   timetogiveup++;   var responses = form.getresponses(timestamp); } while (responses.length == 0 && (timetogiveup < 3)); 

lots more 1 line of code, more robust.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -