|Getting Started With Google Apps Script|
|Written by Ian Elliot|
|Saturday, 28 October 2017|
Page 3 of 3
Using jQuery & Promises
This section is at a slightly more advanced level
The good news is that you can use jQuery on the client side to make your code much easier to write.
You can include jQuery from the Google Content Distribution Network by adding:
to the end of the HTML file. Notice you can't use jQuery with the server-side script because there is no DOM which is what jQuery works with.
With this addition the onSucess function in the previous Gmail example can be written:
You can also use jQuery's Deferred object to implement Promises. For example, you can write a getThreads function which returns a Promise which resolves when the SuccessHandler is called:
With this defined you can write functions like:
If you are using jQuery 3 then the Deferred and the Promise can be chained and they handle errors correctly.
If you don't want to use jQuery then you can use Promises on the client side, but there is no indication that Apps Script supports them officially whereas jQuery is not only supported but recommended.
A Promise implementation of getThreads is:
It works and can be used in exactly the same way as the jQuery version.
There is a lot more to find out about Apps Script. You need to discover "triggers", which let you run apps at predefined times or in response to events that occur in containers. You will need to find out about templates and how to use them because only the smallest and simplest of projects don't need them. You can use it to build add-ins to existing containers such as the Spreadsheet, you can use it to access other Google services, you can even use it to build web apps that use other services that have nothing to do with Google.
A Programmers Guide To Languages
or email your comment to: firstname.lastname@example.org
|Last Updated ( Saturday, 28 October 2017 )|