Table of Contents

reading time: 30 min

  • Introduction

  • Basic structure in Google Apps Script

  • Types and Variables in Google Apps Script

  • Constants, Variables, and Letters in Google Apps Script

  • Operations and Operators in Google Apps Script

  • Var, Let and Const in Google Apps Script

  • Control Structures in Google Apps Script

  • Loops in Google Apps Script

  • Objects in Google Apps Script

  • Functions in Google Apps Script

  • Parameters and Scopes in Google Apps Script

  • Rest and Propagation Operators in Google Apps Script

  • [Assignment by Decomposition in Google Apps Script]

  • Collections and Declarative Programming in Google Apps Script

Introduction

Google Apps Script (GAS) is a scripting development platform associated with various Google services such as Google Sheets, Google Docs, and Google Forms. It allows for the automation of tasks, the addition of custom features, and the integration of Google services.

In this memo, we will explore the primary concepts of Google Apps Script using concrete examples. We will cover notions such as variables, loops, functions, and many others, adapting them to the specific context of GAS.

Basic structure in Google Apps Script

To begin, let’s examine the basic structure of a Google Apps script.

/**
 * Structure de base en Google Apps Script
 */
function exempleStructureGAS() {
    // Votre code ici
    Logger.log("Hello, Google Apps Script!");
}

Types and Variables in Google Apps Script

Google Apps Script uses JavaScript as its programming language. Data types and variables work similarly to standard JavaScript.

/**
 * Types et Variables en Google Apps Script
 */
function exempleTypesVariablesGAS() {
    // Votre code ici
    var nombre = 42;
    var texte = "Bonjour, Google Apps Script!";
    Logger.log(nombre);
    Logger.log(texte);
}

Operations and Operators in Google Apps Script

The operations and operators in Google Apps Script are the same as in JavaScript.

/**
 * Opérations et Opérateurs en Google Apps Script
 */
function exempleOperationsGAS() {
    // Votre code ici
    var x = 10;
    var y = 5;
    var resultat = x + y;
    Logger.log(resultat);
}

Var, Let and Const in Google Apps Script

In GAS, the concepts of`var`, let, et `const`are used to declare variables. However, it is essential to understand their differences and implications.

/**
 * Var, Let et Const en Google Apps Script
 */
function exempleVarLetConstGAS() {
    // Votre code ici
    // Utilisation de var
    var x = 10;
    if (true) {
        var x = 20; // La variable x est modifiée globalement
    }
    Logger.log(x);

    // Utilisation de let
    let y = 30;
    if (true) {
        let y = 40; // La variable y est limitée au bloc if
    }
    Logger.log(y);

    // Utilisation de const
    const z = 50;
    // z = 60; // Impossible de réassigner une constante
    Logger.log(z);
}

Control Structures in Google Apps Script

Control structures such as`if`, else, while et `for`are used to manage the execution flow in Google Apps Script.

/**
 * Structures de Contrôle en Google Apps Script
 */
function exempleStructuresControleGAS() {
    // Votre code ici
    var condition = true;

    if (condition) {
        Logger.log("La condition est vraie.");
    } else {
        Logger.log("La condition est fausse.");
    }

    var compteur = 0;
    while (compteur < 5) {
        Logger.log(compteur);
        compteur++;
    }

    for (var i = 0; i < 3; i++) {
        Logger.log(i);
    }
}

Parameters and Scopes in Google Apps Script

Parameters and scopes play a crucial role in developing GAS scripts. Understand how they work to avoid unexpected behavior.

/**
 * Paramètres et Scopes en Google Apps Script
 */
function exempleParametresScopesGAS(parametre) {
    // Votre code ici
    var variableGlobale = "Je suis global";

    function afficherParametre() {
        Logger.log(parametre);
    }

    afficherParametre();
    Logger.log(variableGlobale);
}

Constants, Variables, and Letters in Google Apps Script

Understanding the difference between`const`, var, et `let`is essential for the effective use of variables in Google Apps Script.

/**
 * Constantes, Variables, et Lettres en Google Apps Script
 */
function exempleConstantesVariablesLettresGAS() {
    // Votre code ici
    const constante = "Je ne change pas";
    Logger.log(constante);

    var variable = "Je peux changer";
    Logger.log(variable);

    let lettre = "Je peux aussi changer, mais seulement dans mon bloc";
    Logger.log(lettre);
}

Loops in Google Apps Script

Loops, such as`for` et while, are essential for iterating over elements and performing repetitive operations.

/**
 * Boucles en Google Apps Script
 */
function exempleBouclesGAS() {
    // Votre code ici
    for (var i = 0; i < 3; i++) {
        Logger.log(i);
    }

    var condition = true;
    var compteur = 0;

    while (condition) {
        Logger.log("Tour de boucle");
        compteur++;
        if (compteur === 3) {
            condition = false;
        }
    }
}

Objects in Google Apps Script

Objects are used to structure data. In GAS, many built-in objects facilitate interaction with Google services.

/**
 * Objets en Google Apps Script
 */
function exempleObjetsGAS() {
    // Votre code ici
    var feuille = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var cellule = feuille.getRange("A1");
    cellule.setValue("Nouvelle valeur");
}

Constants, Variables, and Letters in Google Apps Script

Understanding the difference between`const`, var, et `let`is essential for the effective use of variables in Google Apps Script.

/**
 * Constantes, Variables, et Lettres en Google Apps Script
 */
function exempleConstantesVariablesLettresGAS() {
    // Votre code ici
    const constante = "Je ne change pas";
    Logger.log(constante);

    var variable = "Je peux changer";
    Logger.log(variable);

    let lettre = "Je peux aussi changer, mais seulement dans mon bloc";
    Logger.log(lettre);
}

Functions in Google Apps Script

Functions are fundamental elements in programming. In GAS, they can be declared and called in different ways.

/**
 * Fonctions en Google Apps Script
 */
function exempleFonctionsGAS() {
    // Votre code ici
    function additionner(a, b) {
        return a + b;
    }

    var resultat = additionner(2, 3);
    Logger.log(resultat);

    // Fonction anonyme
    var multiplier = function (x, y) {
        return x * y;
    };

    Logger.log(multiplier(4, 5));
}

Pause and Propagation Operators in Google Apps Script

The rest operators (…​) and propagation (…​) are useful for manipulating arrays and objects concisely.

/**
 * Opérateurs de Repos et de Propagation en Google Apps Script
 */
function exempleOperateursReposPropagationGAS() {
    // Votre code ici
    // Opérateur de repos (...) pour les tableaux
    var nombres = [1, 2, 3, 4, 5];
    var [...copieNombres] = nombres;
    Logger.log(copieNombres);

    // Opérateur de propagation (...) pour les objets
    var objOriginal = { x: 1, y: 2 };
    var objClone = { ...objOriginal, z: 3 };
    Logger.log(objClone);
}

Destructuring Assignment in Google Apps Script

Destructuring assignment is a powerful feature that allows for the concise extraction of values from objects and arrays.

/**
 * Affectation par Décomposition en Google Apps Script
 */
function exempleAffectationDecompositionGAS() {
    // Votre code ici
    var coordonnees = [3, 4];
    var [x, y] = coordonnees;
    Logger.log(x);
    Logger.log(y);

    var utilisateur = { nom: "John", age: 30 };
    var { nom, age } = utilisateur;
    Logger.log(nom);
    Logger.log(age);
}

Collections and Declarative Programming in Google Apps Script

Google Apps Script supports data structures such as arrays, as well as functional programming concepts and declarative (vs. imperative) iteration, such as`map`, reduce et forEach.

/**
 * Collections et Programmation Déclarative en Google Apps Script
 */
function exempleCollectionsFonctionnelleGAS() {
    // Votre code ici
    // Exemple de tableau (collection)
    var nombres = [1, 2, 3, 4, 5];

    // Utilisation de forEach
    nombres.forEach(function (nombre) {
        Logger.log(nombre);
    });

    // Utilisation de map
    var carresNombres = nombres.map(function (nombre) {
        return nombre * nombre;
    });
    Logger.log(carresNombres);

    // Utilisation de reduce
    var somme = nombres.reduce(function (acc, nombre) {
        return acc + nombre;
    }, 0);
    Logger.log(somme);
}

Conclusion

This memo covered the fundamental concepts of Google Apps Script, illustrating them with concrete code examples. Use this knowledge to automate your daily tasks, customize your documents, and take full advantage of the features of Google Apps Script.

Related articles