Introduction
Google Apps Script allows you to automate Google Workspace tools like Sheets, Docs, or Gmail without an external server. This tutorial guides you step by step to create your first scripts. You will learn to read and write data in Sheets, add custom menus, and send automatic emails. Ideal for beginners, each step includes complete and ready-to-use code. The goal is to transform repetitive manual tasks into automated processes in just a few minutes.
Prerequisites
- A free Google account
- Access to Google Sheets
- Basic knowledge of JavaScript
- Modern web browser
Create the Apps Script Project
function helloWorld() {
console.log('Bonjour depuis Google Apps Script !');
}
helloWorld();This simple function displays a message in the console. Open the script editor from Extensions > Apps Script in your Sheet.
First Execution
Save the script then click Run. Authorize permissions the first time. The message appears in the execution log.
Read Sheets Data
function lireDonnees() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const range = sheet.getRange('A1:B5');
const values = range.getValues();
console.log(values);
}This code retrieves the values from cells A1 to B5 and displays them. Adjust the range according to your needs.
Write Data
function ecrireDonnees() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange('C1').setValue('Total');
sheet.getRange('C2').setValue(150);
}This function writes text and a number into specific cells. Very useful for generating automatic reports.
Add a Custom Menu
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Mon Menu')
.addItem('Lire données', 'lireDonnees')
.addItem('Écrire données', 'ecrireDonnees')
.addToUi();
}The onOpen function runs automatically when the file opens. It adds a custom menu to the Sheets interface.
Send an Automatic Email
function envoyerEmail() {
const email = 'destinataire@example.com';
const sujet = 'Rapport automatique';
const corps = 'Voici les données du jour.';
GmailApp.sendEmail(email, sujet, corps);
}This script sends an email via Gmail. Test with your own address before automating it.
Best Practices
- Always test scripts on a copy of the file
- Use clear and descriptive function names
- Add comments in the code
- Handle errors with try/catch
- Limit API calls to avoid quotas
Common Errors
- Forgetting to authorize OAuth permissions
- Using incorrect ranges that cause errors
- Not triggering onOpen correctly
- Exceeding daily execution quotas
Go Further
Discover our complete training on Google Workspace automation: https://learni-group.com/formations