Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Google Apps Script for Beginners

You're reading from  Google Apps Script for Beginners

Product type Book
Published in Feb 2014
Publisher
ISBN-13 9781783552177
Pages 178 pages
Edition 1st Edition
Languages
Author (1):
Serge Gabet Serge Gabet
Profile icon Serge Gabet

Table of Contents (16) Chapters

Google Apps Script for Beginners
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Enhancing Spreadsheets 2. Create and Manipulate Forms 3. Managing an E-mail Account 4. Embedding Scripts in Text Documents 5. Embedding Scripts in Google Sites 6. Standalone Web Applications / User Interfaces 7. Using User Interfaces in Spreadsheets and Documents 8. How to Expand your Knowledge 9. Conclusion Index

Reading and writing data from spreadsheets – best practices and performances


You probably noticed when playing with the preceding script example that it takes some time to execute. You must remember how these spreadsheets and scripts actually work.

The sheet we see in our browser does not exist as a file in our computer; all its data is stored on a Google server somewhere and it is rendered as HTML content that can be understood by any web browser.

Each time we make a change manually or using a script, the information is sent to the server, interpreted, processed, and sent back to us using our Internet connection.

This can be a time-consuming journey for all those bytes; some parameters are predictable (our average connection speed, for example) and some are not, such as the distance from our computer to the Google server and its load at that moment. Remember that you are not alone in using this drive app! There can be thousands of users sending requests simultaneously.

To make things better, or at least the best we can achieve with the parameters we have control over, we should take care to use as few separate requests as possible to any distant service.

For example, if we want to fill 100 cells in a sheet column, it would be a bad idea to fill each cell one by one. We should try to use the batch method to set the values in all 100 cells in one single step.

Example

The following are two scripts that do what we suggested in the preceding section: filling a column of 100 cells with some text. Try both versions successively and compare the execution times of both versions, which will be shown in a browser pop up. The following code snippet fills a column of 100 cells with some text:

function fill100Cells(){
  var start = new Date().getTime();
  var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  for(var n=1 ; n<=100 ;n++){
    sh.getRange(n,1).setValue('This cell is filled');
  }
  Browser.msgBox('Execution time : '+(new Date().getTime()-start)+' milliseconds');
}

function fill100CellsatOnce(){
  var start = new Date().getTime();
  var sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sh.getRange(1,1,100,1).setValue('This cell is filled');
  Browser.msgBox('Execution time : '+(new Date().getTime()-start)+' milliseconds');
}

The following screenshot displays the execution time for filling a column of 100 cells with some text:

I'll let you guess which one is the most efficient.

This simple example illustrates the very few annoying aspects of cloud computing and Google Drive applications, in particular, the execution speed of code.

Knowing that, we should always be very careful when writing scripts, do it as efficiently as possible, and using as few service calls as possible. These recommendations and a few others that we shall examine later on are clearly explained in Google Drive's documentation. I suggest you read it twice rather than once and keep it in mind when writing your future applications (https://developers.google.com/apps-script/best_practices).

There have been a couple of very interesting posts on that subject on the Stack Overflow Help forum and I suggest that you read this as well, as it goes quite deeper in the speed testing and optimization process (among others: http://stackoverflow.com/questions/15145918/what-is-faster-scriptdb-or-spreadsheetapp/15149959#15149959).

You have been reading a chapter from
Google Apps Script for Beginners
Published in: Feb 2014 Publisher: ISBN-13: 9781783552177
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}