Getting started with google apps script
javaScript quick start.
Declaring variables
Convention for variable names
In VBA, the typical convention (although I dislike it and don’t follow it – I actually use the javaScript convention in VBA too) is to use the first 3 letters as the type, and the rest to describe with mixed case. In javaScript, the convention is to use case to divide the word into its components, and for the first component to be in lower case.
1 |
strFirstname |
1 |
firstName |
The semi colon
1 |
x= y; |
Blocks
1 2 3 4 |
if bTrue then ..xx ..yy endif |
1 2 3 4 |
if (bTrue) { ..xx; ..yy; } |
Operators
The equals sign.
1 2 3 |
a=b if (a=b) then .. do something x=(a=b) |
1 2 3 |
a=b; if (a==b) {..do something ..} x = (a==b); |
Note on order of evaluation
1 2 3 |
x=1; a. if (--x) { .. this is false ..} b. if (x--) {.. this is true..} |
Conditional assignment
1 2 3 4 5 |
if (a) then x=y else x=z endif |
1 2 3 4 |
if(a) x=y; else x=z; |
1 |
x = a ? y : z; |
1 2 3 |
ks = isUndefined(k) ? self.generateKey() : (pCleanKey ? makeKey(k.toString()) : k.toString()); |
1 |
var x = isTypeNumber(k) ? k - pBase : pKeys[pCleanKey ? makeKey(k) : k]; |
1 2 3 4 5 6 7 8 9 10 11 |
cCache.prototype.getKey = function (ob) { return DebugAssert ( isTypeString(ob) ? ob : ob.getSheet ? ob.getSheet().getName() : ob.getName ? ob.getName() : null ) ; }; |
Looping
1 2 3 4 |
Dim i as long do i = 0 to 9 ' do something next i |
1 2 3 |
for ( var i=0; i<10 ; i++) { 'do something; } |
Arrays
1 2 3 4 5 6 |
Dim i as long dim a(0 to 9) do i = lbound(a) to ubound(a) msgbox(cstr(a(i))) next i |
1 2 3 4 |
var = new array(10); for ( var i=0; i<a.length ; i++) { alert(a[i].toString()); } |
Functions and Procedures
For more like this see From VBA to Google Apps Script In the meantime why not join our forum, follow the blog or follow me on Twitter to ensure you get updates when they are available.