+ 1

How to keep Google apps script js clean?

Pretty fresh, building my 2nd app with Google Apps Script. This one of a bit of a beast. My question is general, & may even cross into basic JS best practices territory. I am looping through a ton of emails, and need to get a similar set of variables (many) for ea email. It's like 20 lines of code just for these vars. Don't want all that to be in my loop, but the only other option I know is to make all the variables global… Is there any other way to keep this separated, call vars from outside my loop?

28th Apr 2017, 4:46 AM
Timothy
Timothy - avatar
4 odpowiedzi
+ 6
Show us your code to get appropriate help ;) Anyway, you can avoid global variables but having your global scope of variables anyway with closures ( namespaced or anonymized ): var global = 42; // global scope var (function() { var fakeglobal = 24; // global for this anonymized function self running // your code using your 'fakeglobal' var })(); Look carrefully the syntax to declare and call the anonymized function ( she's executed as soon as she was parsed, as global scope code )... However, you can name your variable scope using objects: var myPersonnalScope = {}; // a global scope unique var to hold your scope myPersonnalScope.myVar = 'fake global var, accessible from anywhere';
28th Apr 2017, 6:30 AM
visph
visph - avatar
+ 6
Instead put your variables into a function object, use a simple object: var doIt = { orderEmail: 'test', dateBlock: 42 } alert(doIt.orderEmail+'\n'+doIt.dateBlock); ... and you can even put functions inside: var nameSpace = { myFunc: function() { /* do something */ } } nameSpace.myFunc();
28th Apr 2017, 3:21 PM
visph
visph - avatar
0
Thanks visph! Below are all the variables I'm pulling from each email. These variables need to be pulled for each email I'm looping through. Currently I removed them from this function, and put all this at the start of what is now a massive loop... Ideally, I'd think I could have this function somewhere in my file, but call it within the loop and get the variables I need there... function doIt() { var orderEmail = GmailApp.getStarredThreads(e, 1)[0].getMessages()[0]; var emailBody = orderEmail.getPlainBody(); // lowercased string of fan's full name from email body var dateBlockReg = /.*delivered on:\s*([^\n\r]*)/; var dateBlock = dateBlockReg.exec(emailBody); var dueDate = dateBlock.slice(Math.max(dateBlock.length - 2, 1)).toString(); // lowercased string of fan's full name from email body var nameBlockReg = /.*delivered to:\s*([^\n\r]*)/; var nameBlock = nameBlockReg.exec(emailBody); var fanName = nameBlock.slice(Math.max(nameBlock.length - 2, 1)).toString().toLowerCase(); // fan's email from order email body var emailReg = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/; var extractedEmail = emailReg.exec(emailBody); var fanEmail = extractedEmail[0].toString().toLowerCase(); // restaurant name from order email body var subtotalLineReg = /.*Subtotal*([^\n]*)/; var subtotalLine = subtotalLineReg.exec(emailBody); var restaurantName = subtotalLine.toString().replace(' Subtotal', '').replace(',', ''); // order total as float from order email body var totalBlockReg = /.*Order Total\s*([^\n\r]*)/; var totalBlock = totalBlockReg.exec(emailBody); var totalString = totalBlock.slice(Math.max(totalBlock.length - 1, 1)).toString(); var orderTotal = Number(totalString.replace(/[^0-9\.]+/g,"")); }
28th Apr 2017, 3:12 PM
Timothy
Timothy - avatar
0
awesome, somehow between all the little js tutorials I did, I somehow managed to miss/forget function objects… +1 for sololearn and big thanks, again, visph!
28th Apr 2017, 4:20 PM
Timothy
Timothy - avatar