Selasa, 07 Maret 2017

what is google apps

what is google apps

carrie rowe: cameraadds 10 pounds. arun nagarajan: cameradoes add 10 pounds. kalyan reddy: perspective. you are closer. arun nagarajan: that too. we'll let you do anotherone where you can have your own shot. [music playing] arun nagarajan: welcome to"developers live." my name is

arun nagarajan, and we'rehere live from the new york studios. today's topic is "apps scriptcrash course and the script editor." we're going to focusa lot on the script editor. but before i get started, iwant to introduce other presenters. kalyan reddy: hey, everyone. my name is kalyan reddy. i usually do the apps unscriptedseries, but today,

i'm going to give you some neattips and tricks on using the script editor and maximizingproductivity there. and as you might know, most ofus on the apps script team work out of the newyork office here. but today, we have a specialguest on our team from the mountain view office. i'll let her introduceherself. carrie rowe: thanks, kalyan. i'm carrie rowe.

i'm a program manager on theapps dev rel team, and i work with these guys. i'm really excited to sit outhere with them and hear more about what they do. and my job is to help makedevelopers successful, so implementing programs to helpreach out to you guys and communicate to you aboutour products and get your feedback. so if you guys have any ideasabout how we can better

interact with you, please poston the community page or reach out to me on google+. i'd love to hearyour feedback. arun nagarajan: that's right. that's really importantfeedback. "google developers live" itselfis a program, so-- carrie rowe: yes. arun nagarajan: --that's whyshe's on it, promoting it. so let's get started.

so the apps crash course seriesis all about taking one aspect of writing apps scriptapps and just going really deep into it. so even if you're a programmerthat's been working with apps script for a couple of yearsnow, we hope that you'll have something that you can takeaway from this series, but it's more focused around justfrom the ground up explain a lot of the capabilities ofa particular feature. and today, it's all aboutthe script editor.

should we get started? all right. so the very first thing thatwe'll talk about is how do you get to the script editor. so obviously, i think theeasiest to remember url in the world is script.google.com, realeasy to remember, real easy to type. it brings you the scripteditor directly. so when i say script editor,just so everybody is on the

same page, it's the ide, theintegrated development environment where you'll writeand run your scripts from. so that's one of theeasiest ways to get to the script editor. we also have the ability toinject the create script option within google drive. so this actually is a prettynice thing that you can do. so right now, if you click onthe create menu, you have the native google apps formatsavailable and then any

third-party apps youcan install. we have google apps scriptavailable as an app which you can install as well. so what you can do is sayconnect apps to drive and search for apps script,and search for that. and that should-- hopefully, thewi-fi catches up here, and that should bring back an appsscript app that i can hit connect on. and that will install a newoption under your create menu

for script. so i like to use this, becauseit's actually a really nice way to put a script ina particular folder. so if you're within a folderalready where you keep your scripts, and you create a scriptwithin that, it'll be managed properly. so let's go with this route, andthat, again, brought us to the script editor. and in fact, you can see in theurl that the folder id is

specified, so you knowit's working. that's great. so let me talk a littlebit about the script editor itself. so the very first thing that yousee when the script editor comes up is the welcomescreen. i know a lot of you probablyturn this off, but i do find this useful in some scenarioswhere if i have a recent project that i want toimmediately open up, or if i

like to use a template. so on the left-hand side, is theset of templates that we have to help you get started. so this doesn't change the typeof script or create any property changes aboutthe code itself. all it does is it seeds yourscript editor with a bunch of useful snippets that we thinkwill help you get started. so if i, for instance, selectthe calendar template, all that's doing is it's going tocreate the project that we

already have and populate itwith a bunch of code that we think will be useful for you. kalyan reddy: right. like every script isn't likethis is a calendar script. this is a drive script. it's not like that. you could have permissionsgranted based on what services you're using on a script basis,but this just seeds the code into the script editor.

yeah. so as you probably know, most ofthe impressive scripts that are out there oftentimesuse calendar along with spreadsheet data, along with afile in drive or something along those lines. so in this case, we're justgiving you a bunch of code that if you're not interested inreading the documentation, you can start looking at somesamples and get going. excuse me.

so let me show one last thing,and then i'll talk to you about the next set ofthings we'll show. so in drive itself, one ofthings you can do is once you've created a script, you cango into the settings, go to manage apps, and you're ableto actually filter all your scripts by going to googleapps script from the manage app option and then say,view all the files this app created. this allows you to see all thescripts very quickly, so it's

a really nice way to kind offilter and look at all your scripts in a simple search. and then i'll also do a quickwalk through of all the different options withinthe script editor. so we've actually done a reallynice job, i think, to make the script editor feelfamiliar to other editors you may have worked in. obviously, it's still runningin a browser, so it's very portable, and it's meant to belightweight and quick, but

we're still trying to makeit very familiar. so you have your standard file,edit menu where you can kind of do basic things aroundthe file in the project you have the list offiles on the left. you cannot have more than onefile with the same name, so we take care of that kindof validation here. and then you can actually havemultiple tabs open which is great when you're writing afairly sophisticated program. and then these are your standardformatting, and

deployment, and debuggingoptions, so being able to indent, unindent,save the file. this is a deployment optionto the web as a web app. these are triggers. we'll get to that in a second. and then these are the debuggingand the running icons, so you'll end upusing that a lot. and this is the list offunctions that we have parsed out from your code thatyou can execute.

so that's sort of thevery quick overview of the script editor. now, i think kalyan is goingto take over and talk about one of his favorite features. kalyan reddy: yeah. so if you're anything like me,you hate taking your hands away from a keyword, because itjust wastes so much time. carrie rowe: spoken likea true engineer. exactly.

it wastes so much time-- grabbing the mouse and trying touse the ui to do something that you know is there, and youjust want to get to it. so i have a couple of handyshortcuts that i use on a day-to-day basis wheni'm writing scripts. so maybe if you don't know, youmight find these useful. the first thing is actually autocomplete, which is the most handy shortcut, i think,anyone likes here. so basically, you cansee it in here.

it's content assist, but inaction you can see it. let's say we have calendar app,get calendar by id here. if you want to do something elsewith calendar app, you can actually just get to itby doing that shortcut combination, control-space, andthen you have all of these choices, which are our top-levelservices that you can interact with usingapp script. arun nagarajan: this is a greatway to just keep up with what are our availableservices.

i haven't typed anything yet,but you already get this list. so then let's go type c forcalendar right there, and then you can complete that when youpress the dot, you get a sub menu drilling into thattop-level service. and you can say-- let's say i want to create acalendar, and then you can give it some name. so very easily without doinganything, without remembering, without going to ourdeveloper pages and

looking at the function. arun nagarajan: even though ourdeveloper pages are great i think this is-- kalyan reddy: oh, yeah. right. arun nagarajan: --i thinkthis is a really nice way to write code. kalyan reddy: exactly. let's see.

after that, let's get into somekeyboard shortcuts that you might not know. so one shortcut is-- let's name it something. i love calling thingstest script. arun nagarajan: you justdefined those, right? kalyan reddy: i have a billiontest scripts in my drive right now, and i have no idea whatany of the old ones do. ok.

so let's see, what aresome of my favorites? the one thing that i loveis, i write some code. i save it. i just want to seewhat it does. it's the run. all right? arun nagarajan: there's anotherone here with some code that you can run quickly. kalyan reddy: there we go.

so this is good codethat we can show. let's say we want to run aprocessinbox function. i select it. i make some edits here,whatever, save it. so if you're using a mac, it'scommand-r. if you're for using anything else, it's control-r.so that does not reload the page like the behavior is injust a regular web browser for other websites, but it actuallyruns the function that's currently selected withinthis function box here.

and as you can see, the yellowbar came up saying the function was run. in conjunction with this,another useful chain command is command-enter orcontrol-enter, if you use other ides. and this brings up thelogging output. so what i usually do is i docontrol-r, control-enter and then quickly see what'sthere to log. so actually, if you want tochain that even more,

command-s to save it. as you know, this is thesame for other things. arun nagarajan: so you'resaying, you'll write code. you'll use auto completeto write your code. arun nagarajan: command-s.command-r. command-enter. kalyan reddy: command-enter. carrie rowe: all of this is partof the quest to become an app script jedi master. arun nagarajan: well, justin general doing more--

carrie rowe: slash impressyour friends. arun nagarajan: yes. kalyan reddy: yes. i talk about this at parties. so anyway, you see thislogging output here. we'll get into explicit detailsof logging later. but this is generallyhow you can get to the log very quickly. another feature that we have,you might have noticed this,

but arun mentioned itbriefly just before. it's the auto indent feature. so if you have it turned on,then you might notice that you can't press the tab key toindent things like normal, because it's handlingthis for you. and if you add something else,it'll automatically indent it starting at the same spot. if you turn thatoff, you can-- well, let me show someother behavior here.

well, let's try somereal code here. var x equals 2, let's say. if you want to fix that quickly,you just make a selection and press tab. and then it'll makeyour code pretty. arun nagarajan: and i find thatwhen i'm copy/pasting samples or code from anotherplace, i end up needing that a lot. kalyan reddy: it's all messedup, so you do command-a or

control-a, select all, and thenjust do tab to fix that. now, if you don't use this autoindent, if you have that turned off, the behavioris slightly different. so when you do have somethinglike this that you want to fix, you have to do shift-tabto fix it. so instead of just tab, you haveto do shift-tab, because otherwise, you can use tab justto do regular indents. arun nagarajan: sowhat do you have? do you have--

kalyan reddy: i don't usethe auto indenter. arun nagarajan: you don't? kalyan reddy: no. arun nagarajan: wow. kalyan reddy: i just like, ifi need to indent something, control-a, shift-tab. it's quick enough. the next thing is for variablesthat you've defined earlier in your code.

a useful way to complete thishere, here we have logger.log, thread.count. let's say we want to do thateasily, we went and defined thread count earlier on. you can do thread and then-- so alt forward slash is ashortcut to do completions from your code. so as i keep clicking it, itkeeps iterating through everything you've defined.

you've defined threadid earlier. you've defined threadcount, threads. its iterating though all ofthese that you've designed, so you can quickly do it. arun nagarajan: so you no longerneed to decide between short names thatare easier type versus descriptive names-- arun nagarajan: --thatare easy to read. kalyan reddy: so it's actuallygood programming practice to

not name everything xyz. you can actually give themdescriptive names. and so even if your name haslike four words camel cased together, you can quickly justget to that and have people who are sharing the script withyou and managing it in the future, have themunderstand it. arun nagarajan: and whatwas the shortcut again? it was alt? kalyan reddy: so altforward slash.

arun nagarajan: altforward slash. and is this one of the oneswe list in the menu? kalyan reddy: this is. so if you go to the edit menu,if you ever forget-- arun nagarajan: this is-- kalyan reddy: word completion. arun nagarajan: wordcompletion. kalyan reddy: is whatit is here. arun nagarajan: all right.

and content assist is ourcontent, our apis. and another really, really handyone is let's say you want to quickly comment a lineof code, you can do control forward slash, and then you canautomatically toggle the comments on and off. and this is also listedin the edit menu here in toggle comment. arun nagarajan: and where doesyour cursor have to be, anywhere in that line?

kalyan reddy: anywhereon the line is fine. and if you have a multiple line,selection like i have three lines here, you can dothe same comment all of the lines are-- arun nagarajan: that'sa good trick. kalyan reddy: --done. arun nagarajan: that's evenfaster than using the multi-line comments, i think. because you have tomove your mouse.

you have to create new line upthere, down there slash star. arun nagarajan: no wonderyou're always typing faster than me. you've got to know allthese things, man. let's see, another featurethat's very common to other ides is the undo/redofunctionality. so if you do command-z onthe mac, you can undo. command-y is redo. control-z, control-yi believe--

arun nagarajan: all of thisworks on a chromebook. it works on everything. the actual shortcut,the modifier key might be different. it's command on a mac, but ifyou go to the edit menu, you can see what-- arun nagarajan: will showthe correct one. kalyan reddy: --the actualmodifier is. so it'll be adjustedfor your system.

and also, this, like arunsaid, is meant to be lightweight so we canwrite some great scripts really quickly. but we do want to add somefunctionality that's missing from other ides, like eclipse,that you might use, or even more older ones like vim. i don't want to get there. i love using vim. so you know, if there's somefunctionality that you want us

to add like some new keyboardshortcuts, then please put a feature request on our issuetracker, and we'll enter that in the system. and if enough people likeit, we'll definitely be introducing that intothe script editor. arun nagarajan: great. thank you very much for that. so now, what we've done so faris given you tips, tricks to allow you to write yourcode really fast.

now, the time has come to runthe script and find out what you've screwed up, right? so that's where this featurecalled the debugger comes in. so debugger is a concept thata lot of you that have programmed on other platformswould be very familiar with. it's where you could run yourcode and pause the execution, and then inspect the stateand variables, and then potentially run it a little bitmore, pause it again, and compare things.

so the debugger gives you thisdeep insight into the execution of the program. and even though google appsscript is a cloud-based, scripting language, we'vegiven you the debugger capability that runswithin the browser. so that's actually prettyincredible to see. so let's see a quickdemo of this. so i'll use the same functionhere called the processinbox, which is on a test inbox.

it's just going to iteratethrough all my inbox threads and log the messages, thefirst message subject. so let's say what if i wantto ensure that this-- let's say something is breakingat about the 10th thread or somethingalong those lines. what i can do instead of hittingthe run command, which kalyan was using command-r, icould hit this little bug symbol, the universalsymbol for bug. i'm sure like in the next 10years, this is going to get

archaic, but we're still usingfloppy disks and bugs for important metaphors,so that's great. so i'll hit the debug button. and what happens now is, insteadof the function just running in the regular mode, itruns it in a special mode, which is the debug mode. so what happened here, and youcan see that the line that i clicked on was highlighted,and it pauses execution. i forgot to show you.

you can just hit the gutter onthe left-hand panel, and it turns on a red dot next to theline you want to break on. that's the word, break point. and there we have it. so what it essentially didhere is, it paused the execution of the code youwrote on the google data centers and returned back thestate your application is in. so you can see that thevariables that you have here are threads, and msg, and i,which is the incrementer for

the loop itself. so let's just take a look. i'm going to hit thestep over function, which is the continue. when i hit that, it's going togo back and start running the loop again, because we arenow in the second item. so you can actually kind of walkthrough and you watch the line of code beinghighlighted. this is actually prettyimpressive when you think

about what kind of capabilitythis gives you. so you're able to essentiallyrun the code interactively from the script editor itself. and it's really nice to beable to say, ok now, i is incrementing appropriately, andfor a complex app, that may have multiple call stackitems on the left. this becomes really powerfulto observe and inspect. kalyan reddy: now, i don't knowif you all caught that at home, but you can see here inthe fourth line here that i is

of type number and then valueis in the third column. it was 0 in the first iterationof the loop, because you started it at 0, and then itbecame 1 when arun went to the next generationof the loop. and if you don't want to walkthrough every line, you could just hit continue debugging, andthat will go to the next time the break point get hit. so it will come back and now youcan see that my message is now it says, add aprofile photo.

so that's the messageit's picking up. and you could also lookat a collection. in this case an array has gmailthread objects in it. you can see that's not veryuseful in that hover we have here, but those are the thingsthat you're able to really see pretty easily. and i end up using this quitefrequently because i think this is a lot more powerfulthan just logging things. this can allow you to take alook at all the different

objects in it as well. so if you have a string valuethat you can really inspect that pretty quickly. cool. so that's the basicsof the debugger. i definitely urge you guys totake a look at these options to stop, and you can actuallydive into a function as well. so if you're just making afunction call that you've written, you can use this optionwhich is step in, to go

into your custom function. and there are some ways to dothe inheritance and all the expanded objects in thecollection as well. the stopping and pausing,stopping really just stops execution, so now we're done. so we just iterated throughtwo items, and we decided to stop. so it's a nice way to-- you can figure it out.

if you know where the issueis, you don't need to wait till the app finishes for youto start fixing the bug. you can just stop it andwrite it right there. and sometimes later onyou, might be like gmailapp.sendmail. if you don't want to runall of those, you can just exit early. arun nagarajan: yeah. absolutely.

and so it's kind of a powerfultool that we end up using quite a bit to troubleshootour own code. so we hope to haveyou guys use it. carrie rowe: you guysdon't write code perfectly the first time? arun nagarajan: unfortunately,not. kalyan reddy: i do. i don't know about everyone. arun nagarajan: it's all thekeyboard shortcuts, i assume.

now, this is great when, infact, you've written an app that you are launching andrunning for the script editor. however, there are severalcases where your code is invoked asynchronously,either from an event or from a web app. so in those scenarios, for thetime being, you can't attach to that process and break aparticular line at this point. so what you end up needing todo is you create a little wrapper function where youessentially mock the

invocation. so these mock objects allow youto control the debugging experience like youwould normally. so let's take a look atwhy you'd want that. so for instance, let's say ihave published this web app. go to manage versions. i already have one, i guess. and then i'm goingto say, deploy. wi-fi is being a littleslow here.

we're going to deploythis version. and as you can imagine, there'sa doget function that's just returning a verysimple output that's the message for the given index. so let's open up a new tabhere, and go there. it's probably going to throwan error of undefined. so i'll say, ?indexequal to 2. kalyan reddy: so this isyou passing parameters. so think of this as a verysimple web app, and i'm

passing in a parameter. and now let's say that, forinstance, something breaks when index is 10 again. something is not workingcorrectly, and we want to debug it. so in order to debug this whenyou come back to the script editor, the way you were able todebug doget function is you have to sort of write a wrapperfunction from which you're launching doget with theparameters packaged up as

an object yourself. so it's a bit of a hand-heldprocess to create that object, but it's something that i thinkwill really help you when you understand that, ok,so it's the same object that comes in from the invocation andthe web app in a browser or another device. so this is what we'll doinstead, and let's say we want to debug this. i can put a break point now herefor message, and i will

use testdoget asmy invocation. and when i hit thedebug sign-- zoom out a little bit. click on the bug again. resize it appropriately. and you can see that now i havemy break point in doget with the appropriateparameter. so that's one of the really niceways that you can debug even asynchronous invocations bycreating these test harness

mock functions that allow youto pretend as if your application is being invoked. now, it's not just web apps. there's also tons of triggersand events we expose, things like onopen, onedit,even google forms. we launch a lot moreevents now. kalyan reddy: onformsubmit. arun nagarajan: onformsubmit. yep.

and you can see that thislist is quite large. so there's a page in ourdocumentation for understanding events,and you can see that under events and triggers. and you can see quite a numberof items that we've listed there, and we actually documentto you all the different object parametersthat will come to you. so you can see that if it's aclock event, you'll get e dot x, y, and z, these differentproperties.

so with this knowledge, youcan actually mock your own objects just likei've done here. for a web app, it's allparameters dot x, y, and z. for events, it's a bit moreinvolved, because there's a lot more objects of propertiesthat we give you. so that's somethingto keep in mind. it allows you to debug muchmore effectively. i think there's a couple ofother things that are interesting aboutthe debugger.

so the way debuggers work,essentially, is we paused the execution on our side, and wesend down the state of the application downto you, right? so that's prettypowerful stuff. there's certain things wherewe're unable to just pause execution and save state. there's a couple of thingsthat we've documented. one of them is if you'reasking for input. so if you have browser.msgboxor browser dot--

i forget the other-- inform or whatever theequivalent is. i'll use auto complete. browser.msgbox and inputbox,very useful. carrie rowe: very useful. really saves a lot ofroom in my head memorizing these things. so when you have these sortof user, input-grabbing invocations, we're unable toserialized that state, because

we can't pick up where weleft off if the user is in middle of typing. so you will see a messagealong the lines of continuation is not allowed. i forget the exact message. kalyan reddy: unable toserialize continuations. arun nagarajan: and every nowand then there's someone in the issue tracker bringing thisissue back up, but for jdbc connections, which arethese persistent database

connections we make on behalfof you and for browser ui interaction like msgbox andinputbox, we do not allow debugging around thosefunctions. kalyan reddy: and in additionto that, there are certain things that you should be ableto debug, and you might still be getting this error. and that's a bug on our part,so definitely report those kind of things. if it's not around the twothat arun mentioned but

something different, and you'restill getting that unable to serializecontinuation, that weird error, then report that tous, and then we can fix that as they come. arun nagarajan: sounds good. so hopefully, you got a littlesense for what the debugger is all about. and now kalyan is going to takeover and talk about some other features.

kalyan reddy: all right. so logging. i briefly touched on thisbefore, but we'll go into a little more detail here. so logger.log is how youcan get to the log. and if you want to just try torandom string into your log, this is how you do it. so you get the string just bydoing-- you're iterating on the threads object here whichyou get from gmailapp by doing

inbox threads. and you're extracting the firstmessage subject of this particular thread, and thatstring you can easily log by just going logger.logand then message. now, when i run thisprocessinbox function here, you can see that the log hasa timestamp of when each particular entry was written tothe log in addition to the actual message. so these--

more information about, blah,blah, blah, all of this stuff, i think this is arun'stest account. nothing awesome here, but theseare the subjects for all of the threads in arun'sinbox here. very quick way when you'redebugging your script. i mean, if you're going tohave users running your script, they're notgoing to see this. no one in the public sideis going to see this, but this is for you.

so if you're developing yourscript, you can quickly say, hey, what is this returning? let me just log inand find out. arun nagarajan: so in a webapp, this is console.log. kalyan reddy: yep. arun nagarajan: and since thisis the server side, we have to expose our own logger, which iswhat capital l logger is. so actually, it's kind ofhidden, i guess, but you can actually log within the dogetand dopost as well.

if you're the person that'sexecuting the web app and you're signed in, when youexecute those functions and you go back to your scripteditor and you go into your logger, you'll see the loggingoutput from there as well. you won't see it when otherusers run your script, but you do when you're developing it. it's very handy for likedeveloping like web apps just without going throughall that pain. but there's another way tocapture log output, which is

once you have accumulated somethings within the log, within a script execution,in the logger, there's another method. you can do getlog, which returnsto you a string of everything that you'velogged to logger. arun nagarajan: that'sreally cool. kalyan reddy: so basically, whatyou can do is you can do gmailapp.sendmail, and you canjust send that as the body, the logger.getlog.

arun nagarajan: so at theend of an execution-- arun nagarajan: or maybe anexception try catch all the logs so far and send it out? so a lot of people do thingslike open a document and then append logs to thedocument instead. you know that kind of adds alittle bit more latency to your script than ismaybe necessary. this is a very lightweight wayto, if you're running in the script editor, you can justexamine the logger.

if you're not, then you cango check your email. it gives you moreoptions there. arun nagarajan: cool. kalyan reddy: and so anotherthing that you can do is you can do formatting. instead of just having thesimple method where you do logger.log and then pass inone string, there's an overloaded log method. and i have that as anexample down here.

so i have three variables herethat i've poorly named. arun nagarajan: allright, man. kalyan reddy: so here we go. so one is just a number, and theother one is a string, and the other one is an object,which has first and second [? keys ?] and thenappropriately has values. so i want to log all of these. i don't want to do the valueof a, close the quotes plus the object plus open quotesand continue that.

if you're familiar with otherprogramming languages, you can do syntax like this where youhave one string that's unbroken but where you want toreplace certain values of the string with variables, you cando this %s syntax where it actually takes that variable'svalue and puts it into the string when it's aboutto write the string. so here we have the samelogger.log as before, but we've defined this string, andwe're giving the parameters of the string.

so i have three %s's here. and these correspondsequentially to a, b, and c. you can provide a variablenumber of arguments here, and you can likewise add in more%s's into your string. a %s means formattedas a string. so it will convertthis object. arun nagarajan: it'slike printf in c++. kalyan reddy: i'ts like printf,yeah, or sprintf or all the other variants.

arun nagarajan: in fact, we havea utility style format string as well that we'verecently added-- arun nagarajan: --whichhas very similar-- kalyan reddy: --which mirrorsthe same syntax. arun nagarajan: very cool. kalyan reddy: so yeah. so let me just run this realquick and show you an example of that as well. so saving code.

arun nagarajan: can you useyour keyboard shortcuts? kalyan reddy: i should. so anyway, this is theline that we logged. the value of a is 1, b is 2,and then c, as you can see, was an object. but it turned that object intoa string and then enumerated all of its keys andprinted it nicely. oh, something i almostforgot to mention. the execution transcript.

so this is a feature that weadded relatively recently. but people have been askingabout how do i know how long a particular script takes becausei might be getting time out errors. i want to know how i can makemy code more efficient. where are the problems? where are the bottlenecks? so a great way to find thatout is to just look at the execution transcript.

before you would have to dolike, i don't know, log a timestamp there, a timestampthere, something more complex. but here let's say we'rerunning something like processinbox. we've run the function, andwe want to know what's taking a long time? what did my code actually do? you can go to view and executiontranscript here, and so this shows you exactlyall of the calls

that your code did. and as you can see here, you dogetinboxthreads, and this is within the loop, right? so you iterate through thefirst thread and call getfirstmessagesubject, andthen you log something. and then you get it for thesecond thread, and then you log something else. and you can see that it actuallyshows what would be actually logged insteadof saying logger

has been called again. arun nagarajan: so you can seethat the most expensive call here was the very first call. so just calling thatapi and getting it back took one second. and then from that point on,you have the object, and you're not making any morecalls, so it's nearly instantaneous. and you don't have toworry about it.

and so this loggingtook a fraction of a second, et cetera. but if you had morefunction calls-- i'm not sure if you have a goodexample ready to go here. but if you did have more complexcalls, you can say like driveapp.getallfiles andthen enumerate through each file, might be moreexpensive than-- a good example is when you dogetinboxthreads, you get all the threads in your inbox.

if you were to dothreadsi.getmessage dot and then do operations on that,basically, getmessage per each thread, that takes much longerthan doing getinboxthreads, getmessagesforthreads. there's another method thattakes an array of threads and returns a 2d array arun nagarajan: likea batching option. arun nagarajan: so it's likewith spreadsheets, it's something you shouldlook into.

so execution transcript is agreat way to analyze if a command is taking way too long,and how to improve it. kalyan reddy: oh, one more thingis find and replace. within your script. arun nagarajan: verypopular request. kalyan reddy: a verypopular request. so now i already havethreads ready to go. there's only one thread,so let's look for a var for some reason.

so then you can quickly goin and iterate through. arun nagarajan: so it actuallymoves around this focus. very cool. kalyan reddy: and you canactually drag this dialogue around just to see wherein your script it is. find and replace, obviously,it's very similar to other find and replace functionalitiesin other ides. arun nagarajan: and you havemultiple files, that actually lists those optionshere as well.

so you can see it makesre-factoring and other cleanup options pretty straightforward. so we're running shorton time here. i'll show you a couple of otherquick things that i think are really importantaround libraries. i think libraries are a reallynice way to organize your code, share code acrossdifferent projects. in fact, just recently, ourcolleague eric koleda, he released the library thathe put together based on

moment.js, which is an opensource date parsing library that is very popular withjavascript developers. and so let's just take a lookat his google+ post. now, he talked about here's alibrary that he shared, and he gave us what's calledthe project key. so this is how librariesare brought in. so you can find your own projectkey by going to file, project properties. it's tucked away here, but ifyou want to bring in another

project into your project asa library, you can go to resources, manage libraries. and let's go copy this id, andpaste this in here under find a library, and hit select. and then it brought in momentas the title of the project, and then identifies whatwe want it to auto complete off of. so that's sort of thename space for our particular project.

and i'll just pick the staticversion for the time being, and i'll hit save. the development mode, we'll talkabout at a separate time. for now, we'll just keepit as a static version. i'll hit save. so now, i get a nice messagesaying it's been updated, and what i can do now is type inmoment dot and i get auto complete within the library thateric has provided for me, which is really nice to not justbe using auto complete

from our apis. you can actually getauto complete on your own apis as well. so libraries are a great wayto share and reuse code. a couple of other things thati think are really important to point out are-- you saw this very quickly wheni went into the project properties-- is the concept of userproperties and project

properties. so user properties areessentially name value pairs that are sort of like cookies ina web browser, if you will. you can leave that as anapi for it as well. there's a ui for it. you can just say, favoritecolor, and you could say something like blue. and the next time you come andrun the script for this user, you can say, what didi say for you as

your favorite color? and this gives you thatlittle storage space. and project properties arestored and shared across all users essentially. it's the project-level setting,and user properties are shared acrossall projects. so if you have multipleprojects, and they're using the same user property key,you'll get the same value. so that's pretty useful a lotof times to write sort of

personalized content,so you remember what you did for the user. and here's the apis for it, userproperties dot, and you get the auto complete. so it's pretty powerfulin that regard. so i think i covered everythingthat i want to. let me look at my cheat sheetreally quickly here, and we're running out of time anyway. but hopefully, you've learned alot of really useful tricks.

carrie, did you learnanything? carrie rowe: i definitelylearned a ton. arun nagarajan: you're goingto go back to mountain view and write your firstapp script? carrie rowe: this will be greatfor all of my personal training on app scripts. carrie rowe: and i lovekeyboard shortcuts. kalyan reddy: thanks. arun nagarajan: yeah,that's for kalyan.

so we hope that you've learnedmore tricks around how you can be more productive and usesome of the features that we've had in the product forsome that we released this year, some that's beenaround for a while. and we hope to keep improvingthis experience. let us know your feedback,and thank you very much for joining. kalyan reddy: thank you. carrie rowe: thanks,everybody.

bye.

Tidak ada komentar:

Posting Komentar