This cheat sheet provides a quick reference to commonly used JavaScript syntax and constructs tailored for Google Apps Script development.
Variables and Data Types
Declaring Variables:
var name = ‘John’; // Old-school JavaScript variable (function-scoped)
let age = 25; // Preferred for block-scoped variables
const isStudent = true; // Constant value
Data Types:
let string = ‘Hello, world!’; // String
let number = 123; // Number
let boolean = true; // Boolean
let array = [1, 2, 3]; // Array
let object = {key: ‘value’}; // Object
let func = function() { return ‘Hello!’; }; // Function
let nullValue = null; // Null
let undefinedValue; // Undefined
Control Structures
If-Else Statement:
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
Switch Statement:
switch(expression) {
case value1:
// Code to execute when result of expression matches value1
break;
case value2:
// Code to execute when result of expression matches value2
break;
default:
// Code to execute if expression doesn’t match any case
}
For Loop:
for (let i = 0; i < 10; i++) {
// Code to execute 10 times
}
While Loop:
let i = 0;
while (i < 10) {
// Code to execute
i++;
}
Functions
Function Declaration:
function greet(name) {
return ‘Hello ‘ + name + ‘!’;
}
Function Expression:
const greet = function(name) {
return ‘Hello ‘ + name + ‘!’;
};
Arrow Function:
const greet = (name) => ‘Hello ‘ + name + ‘!’;
Arrays
Creating Arrays:
let fruits = [‘Apple’, ‘Banana’, ‘Cherry’];
Accessing Array Items:
let firstFruit = fruits[0]; // ‘Apple’
Array Methods:
fruits.push(‘Date’); // Add an item
let lastFruit = fruits.pop(); // Remove the last item
let firstFruit = fruits.shift(); // Remove the first item
fruits.unshift(‘Apricot’); // Add an item to the start
Objects
Creating Objects:
let person = {
firstName: ‘John’,
lastName: ‘Doe’,
age: 30
};
Accessing Object Properties:
let name = person.firstName; // Dot notation
let age = person[‘age’]; // Bracket notation
Iterating Over Objects:
for (let key in person) {
if (person.hasOwnProperty(key)) {
console.log(key + “: ” + person[key]);
}
}
String Manipulation
Concatenation:
let fullName = ‘John’ + ‘ ‘ + ‘Doe’;
Template Literals:
let fullName = `${person.firstName} ${person.lastName}`;
Common String Methods:
let length = fullName.length; // Get string length
let upper = fullName.toUpperCase(); // Convert to upper case
let lower = fullName.toLowerCase(); // Convert to lower case
Working with Dates
Creating Date Objects:
let now = new Date();
let specificDate = new Date(‘2024-03-01T12:00:00’);
Accessing Date Components:
let year = now.getFullYear();
let month = now.getMonth(); // Note: Months are 0-indexed (0 = January, 11 = December)
let day = now.getDate();
Error Handling
Try-Catch Block:
try {
// Code that may throw an error
} catch (error) {
// Code to execute if an error occurs
console.error(error);
}
Remember, while Google Apps Script is based on JavaScript, there are specific Apps Script services and methods that may not follow standard JavaScript syntax. Always refer to the official Google Apps Script documentation for detailed information on these services.
JavaScript Cheat Sheet for Google Apps Script
This cheat sheet provides a quick reference to commonly used JavaScript syntax and constructs tailored for Google Apps Script development.
Variables and Data Types
Declaring Variables:
var name = ‘John’; // Old-school JavaScript variable (function-scoped)
let age = 25; // Preferred for block-scoped variables
const isStudent = true; // Constant value
Data Types:
let string = ‘Hello, world!’; // String
let number = 123; // Number
let boolean = true; // Boolean
let array = [1, 2, 3]; // Array
let object = {key: ‘value’}; // Object
let func = function() { return ‘Hello!’; }; // Function
let nullValue = null; // Null
let undefinedValue; // Undefined
Control Structures
If-Else Statement:
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
Switch Statement:
switch(expression) {
case value1:
// Code to execute when result of expression matches value1
break;
case value2:
// Code to execute when result of expression matches value2
break;
default:
// Code to execute if expression doesn’t match any case
}
For Loop:
for (let i = 0; i < 10; i++) {
// Code to execute 10 times
}
While Loop:
let i = 0;
while (i < 10) {
// Code to execute
i++;
}
Functions
Function Declaration:
function greet(name) {
return ‘Hello ‘ + name + ‘!’;
}
Function Expression:
const greet = function(name) {
return ‘Hello ‘ + name + ‘!’;
};
Arrow Function:
const greet = (name) => ‘Hello ‘ + name + ‘!’;
Arrays
Creating Arrays:
let fruits = [‘Apple’, ‘Banana’, ‘Cherry’];
Accessing Array Items:
let firstFruit = fruits[0]; // ‘Apple’
Array Methods:
fruits.push(‘Date’); // Add an item
let lastFruit = fruits.pop(); // Remove the last item
let firstFruit = fruits.shift(); // Remove the first item
fruits.unshift(‘Apricot’); // Add an item to the start
Objects
Creating Objects:
let person = {
firstName: ‘John’,
lastName: ‘Doe’,
age: 30
};
Accessing Object Properties:
let name = person.firstName; // Dot notation
let age = person[‘age’]; // Bracket notation
Iterating Over Objects:
for (let key in person) {
if (person.hasOwnProperty(key)) {
console.log(key + “: ” + person[key]);
}
}
String Manipulation
Concatenation:
let fullName = ‘John’ + ‘ ‘ + ‘Doe’;
Template Literals:
let fullName = `${person.firstName} ${person.lastName}`;
Common String Methods:
let length = fullName.length; // Get string length
let upper = fullName.toUpperCase(); // Convert to upper case
let lower = fullName.toLowerCase(); // Convert to lower case
Working with Dates
Creating Date Objects:
let now = new Date();
let specificDate = new Date(‘2024-03-01T12:00:00’);
Accessing Date Components:
let year = now.getFullYear();
let month = now.getMonth(); // Note: Months are 0-indexed (0 = January, 11 = December)
let day = now.getDate();
Error Handling
Try-Catch Block:
try {
// Code that may throw an error
} catch (error) {
// Code to execute if an error occurs
console.error(error);
}
Remember, while Google Apps Script is based on JavaScript, there are specific Apps Script services and methods that may not follow standard JavaScript syntax. Always refer to the official Google Apps Script documentation for detailed information on these services.