Getting Started With Google Apps Script
Written by Ian Elliot   
Saturday, 28 October 2017
Article Index
Getting Started With Google Apps Script
Using Files And Calling Functions
Using jQuery & Promises

Using jQuery & Promises

This section is at a slightly more advanced level 

One problem is that Apps Script mostly used classic JavaScript 1.8 or ES5 with some extras from later editions. There is no support for Promises, for example, and given that you are mostly writing asynchronous code this is a shame.

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:

<script src="https://ajax.googleapis.com/ajax/libs/
                      jquery/3.2.1/jquery.min.js">
</script>

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:

function onSuccess(noThreads){
 $("#text1").text(noThreads.toString());
}

 

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: 

function getThreads(){
 var d=$.Deferred();
 google.script.run.withSuccessHandler(
             function (noThreads){
                d.resolve(noThreads);
             }).myOnClickHandler();
return d.promise();
}

 

With this defined you can write functions like: 

button1.addEventListener('click',
  function(event){
    getThreads().then(function(noThreads){
               
                 $("#text1").text(noThreads.toString());

                      });

  });

 

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:

function getThreads(){
 var p=new Promise(function(resolve,reject){
                google.script.run.withSuccessHandler(
                        function (noThreads){
                          resolve(noThreads);
                        }).myOnClickHandler();
                });
return p;
}

 

It works and can be used in exactly the same way as the jQuery version.  

 

Where Next?

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.  

 

Related Articles

Gmail, Spreadsheets and Google Apps Script

Gmail Add-ons Open To All

Google Apps Script Gets Eclipse Support

Google Dumps GUI Builder and more       

New Google Apps Script Features

 

A Programmers Guide To Languages

languagecover

Contents

  1. Octave
  2. Scratch 3 *revised
  3. Node.js *revised
  4. App Inventor 2 *revised
  5. Getting Started With .NET IL
  6. NetLogo *revised
  7. Small Basic  *revised
  8. TypeScript *revised
  9. Google Apps Script
  10. Guide to F#
  11. Python
  12. Google's Go
    1. A Programmer's Guide To Go With Visual Studio Code *revised
    2. A Programmer's Guide To Go Part 2 - Objects And Interfaces *revised
    3. A Programmer's Guide To Go Part 3 - Goroutines And Concurrency *revised
  13. Guide to F#
  14. The Prolog Way ***NEW!
  15. Ruby
    1. Object-Oriented Approach
    2.    A Functional Language
 

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

 

espbook

 

Comments




or email your comment to: comments@i-programmer.info

 

Banner


JavaScript Canvas - Fetch API

Working with lower-level data is very much part of graphics. This extract from Ian Elliot's book on JavaScript Graphics looks at how to use typed arrays to access graphic data.



JavaScript Jems - The Inheritance Tax

JavaScript should not be judged as if it was a poor version of the other popular languages - it isn't a Java or a C++ clone. It does things its own way.  In particular, it doesn't do inheritance  [ ... ]


Other Articles

 



Last Updated ( Saturday, 28 October 2017 )