Skip to main content

Writing a Google Apps Script.

I have already explained in a previous post what Google Apps Script or GAS is. Will use this post to explain the steps needed to write a script and walk you through a simple script that creates a new Google Document and email the link of the created document.


There are about 3 ways of creating a Google Apps Script from Google Drive. 
  1. Go to drive.google.com.  You will have to log in with your Google Apps / Gmail account. From Google Drive, choose Create -> More -> Script
  2. Go to script.google.com
  3. You can create a script from an existing spreadsheet. Once you have spreadsheet open, go to Tools -> Script Editor. All 3 options will open the same window for you to create a script for; Blank Project, Script as Web App, Drive, Spreadsheet, Document, Gmail, Calendar and Sites. 

Select Blank Project from the opened window and delete the placeholder code in your new script. Copy and paste the code below into the Script Editor.

function createAndSendDocument() {
 // Create a new document with the title 'Hello World'
 var doc = DocumentApp.create('Hello World');


 // Add a paragraph to the document

 doc.appendParagraph('This document was created by my first Google Apps Script.');

 // Save and close the document

 doc.saveAndClose();

 // Get the URL of the document

 var url = doc.getUrl();

 // Get the email address of the active user - that's you

 var emailAddress = Session.getActiveUser().getEmail();

 // Send yourself an email with a link to the document
 GmailApp.sendEmail(emailAddress,
                    'Hello from my first Google Apps Script!',
                    'Here is a link to a document created by my ' +
                    'first Google Apps Script: ' + url);
}


After you paste the code into the Script Editor, click the Save icon. You'll be prompted to rename your project. Enter a name and then click OK. Now that you've created the script, execute the script! 

On your first run it will request for access to run the script, just normal routine security stuff, read the message and continue. More details about Google’s apps scripts security policy can be found here. Now go ahead and run the script again.

Next we will write some generic scripts which will do the following:

Comments

Popular posts from this blog

EC isn't prepared to take technology to the next level in Elections

In Election 2012, Ghana's EC introduces a new process to verify voters before they are allowed to cast their vote. This was in the form of biometric verification, a good idea but poorly not so well implemented.

How does tonaton make money?

Ever wondered how the Swedish company Tonaton makes money? Typical of most classifieds they make or intend making money from premium promotions and advertisements. With extra paid options, posters who want to stand out and reach more interested buyers quickly and easily can have their ad At the top of every ad listing page, where they have up to 2 spots reserved for Top Ads. They currently provide two options for promotions via Airtel, Tigo Cash and Vouchers; 1. Top Ad   This promotion gives your ad a chance to be displayed as a Top Ad over the course of 7 days. 2.  Daily Bump .   This promotion will move your ad up to the top of the listings once every day for 7 days. Meaning your Ad will be on top of the listings 7 times!

Be a part of the experience, and make sure your Polling Station is represented on Google Maps!

Excited to see the number of polling stations that have been mapped by Ghanaian voters. It is validation of the belief in the internet’s role in helping to ensure that the 2012 elections will be the best ever held in Ghana.