Monday, December 31, 2018

Google Apps Script - Script Examples

This post contains examples / snippets of google apps script.  I'll be updating this post periodically so check back regularly.

My past programming experience is in VB and VBA.  Some of the built-in functions I got used to were not available in JavaScript so I wrote a few of my own.

Some of the scripts listed are ones I wrote and some are scripts I found on the Internet.  This post serves as a reference.

Information Functions


isArray()

1
2
3
function isArray(myArray) {
    return myArray.constructor.toString().indexOf("Array") > -1;
}

isDate()

1
2
3
function isDate(date) {
    if(typeof(date) == 'object'){
    return (date.constructor.name == 'Date')};
}

isString()

1
2
3
function isString(str) {
    return (typeof(str) == 'string');
}

isBoolean()

1
2
3
function isBoolean(boolvar) {
    return (typeof(boolvar) == 'boolean');
}

isObject()


1
2
3
function isObject(obj) {
    return (typeof(obj) == 'object');
}

Application

Include the function with the variable within the function's parenthesis as part of an if statement.  For example:

if(isDate(vblname)){...insert code if true...}else{...insert code if false...}

LookUp Functions


LookUpTbl()


1
2
3
4
5
6
7
8
9
function LookUpTbl(table,item, searchCol, resultsCol){
  for(var count = 1; count < table.length; count++){
    if (table[count][searchCol] == item)    
    {var result = table[count][resultsCol]; 
    table = null;
    return result;}
  } 
return 'none'  
}

Finds a match for item in table in searchCol and returns the value form resultsCol.  The arguments:
  • table - an array
  • item - any type
  • searchCol - integer that represents the column number in the array to find a match for item.  
  • resultsCol - integer that represents the column number in the array that contains the value to be returned.
The first match is returned.  Note that in Javascript arrays always starts at zero, so if you're searching the first column in an array that column number is zero.

LookUpRow()


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function LookUpRow(table,item){
  var result
  var count
  for(var count = 1; count < table.length; count++){
    if (table[count][0] == item)    
    {result = count 
    table = null
    return result;}
  }   
}

Finds a match for item in table in the first column and returns the first row number the item matches. The arguments:
  • table - an array
  • item - any type