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.
strFirstname
firstName
The semi colon
x= y;
Blocks
if bTrue then ..xx ..yy endif
javaScript
if (bTrue) { ..xx; ..yy; }
Operators
The equals sign.
a=b if (a=b) then .. do something x=(a=b)
a=b; if (a==b) {..do something ..} x = (a==b);
Note on order of evaluation
x=1; a. if (--x) { .. this is false ..} b. if (x--) {.. this is true..}
In case a. the result of –x is tested for non-zero i.e. after being decremented, but in case b, x is tested for non-zero before being decremented. In both cases, x is 0 after the if statement is executed.
Conditional assignment
if (a) then x=y else x=z endif
if(a) x=y; else x=z;
x = a ? y : z;
ks = isUndefined(k) ? self.generateKey() : (pCleanKey ? makeKey(k.toString()) : k.toString());
var x = isTypeNumber(k) ? k - pBase : pKeys[pCleanKey ? makeKey(k) : k];
or even build a whole function out of it
cCache.prototype.getKey = function (ob) { return DebugAssert ( isTypeString(ob) ? ob : ob.getSheet ? ob.getSheet().getName() : ob.getName ? ob.getName() : null ) ; };
Of course you dont have to use it- a series of ifs and elses will do the job, but isn’t it so much more close to how you tackle the problem?
Looping
Dim i as long do i = 0 to 9 ' do something next i
for ( var i=0; i<10 ; i++) { 'do something; }
Arrays
Dim i as long dim a(0 to 9) do i = lbound(a) to ubound(a) msgbox(cstr(a(i))) next i
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.