Welcome to the exciting world of Google Apps Script! This chapter is your launchpad into automating your Google Workspace. Before we dive into powerful scripts, let's get acquainted with the fundamental building blocks: the basic syntax and core concepts that make Apps Script tick. Think of this as learning the alphabet and grammar before you can write a novel. We'll cover variables, data types, functions, and how Apps Script interacts with Google services.
Variables are like labeled containers that hold information. You declare a variable using the var, let, or const keywords. var is the older way, let is for variables that might change, and const is for values that will never change. You then assign a value to it using the equals sign (=).
var greeting = 'Hello';
let count = 0;
const PI = 3.14;Apps Script, like JavaScript, supports various data types. These are the different kinds of values your variables can hold. Common ones include:
- Strings: Text, enclosed in single or double quotes (e.g., "My Name").
- Numbers: Integers (whole numbers) and floating-point numbers (numbers with decimals) (e.g., 10, 25.5).
- Booleans: Represent truth values, either
trueorfalse. - Arrays: Ordered lists of values (e.g., ["apple", "banana", "cherry"]).
- Objects: Collections of key-value pairs (e.g., { name: "Alice", age: 30 }).
var userName = "Alice";
var age = 30;
var isStudent = false;
var colors = ["red", "green", "blue"];
var person = {
firstName: "John",
lastName: "Doe"
};Functions are reusable blocks of code that perform a specific task. They are essential for organizing your scripts and avoiding repetition. You define a function using the function keyword, followed by the function name, parentheses () (which can hold parameters), and curly braces {} containing the code to be executed.