Google Apps Script
Retrouvez nos trucs et astuces Google Workspace classés par application.
Débutant Manage Script Configurations with ScriptProperties
Easily store and access configuration settings for your Google Apps Script scripts without modifying the code, using the ScriptProperties service.
PropertiesService. This makes it easier to maintain and deploy your scripts in different environments.Set a property:
function setMyConfig() {
const scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty('ADMIN_EMAIL', 'admin@example.com');
scriptProperties.setProperty('SHEET_ID_DATA', '1ABC...XYZ');
Logger.log('Properties set.');
}Get a property:
function getMyConfig() {
const scriptProperties = PropertiesService.getScriptProperties();
const adminEmail = scriptProperties.getProperty('ADMIN_EMAIL');
const sheetId = scriptProperties.getProperty('SHEET_ID_DATA');
Logger.log('Admin Email: ' + adminEmail);
Logger.log('Sheet ID: ' + sheetId);
}You can also manage these properties via the Apps Script user interface: open your script, click the gear icon "Project Settings", then scroll down to "Script properties". You can add, edit, or delete properties manually there.
Intermédiaire Implement Robust Error Handling with Notifications
Ensure the reliability of your Apps Script scripts by implementing error handling with automatic email notifications in case of issues.
try...catch blocks and send email notifications to be alerted immediately.Example:
function myMonitoredFunction() {
try {
// Your code that could potentially fail
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data');
if (!sheet) {
throw new Error('The sheet "Data" was not found.');
}
// ... rest of your logic ...
} catch (e) {
const scriptName = ScriptApp.getProjectKey();
const functionName = arguments.callee.name; // Or simply 'myMonitoredFunction'
const adminEmail = 'your.email@example.com'; // Or stored in ScriptProperties
const subject = `[GAS ERROR] ${scriptName} - ${functionName}`;
const body = `An error occurred in script "${scriptName}", function "${functionName}" :
Message : ${e.message}
Name : ${e.name}
File : ${e.fileName}
Line : ${e.lineNumber}
Stack : ${e.stack}`;
MailApp.sendEmail(adminEmail, subject, body);
Logger.log(body); // For execution log
}
}Remember to authorize the script to send emails on first execution.
Intermédiaire Optimize Performance with Batch Operations
Drastically reduce the execution time of your Google Apps Script scripts by using batch operations to interact with Google services.
Bad practice (slow):
function slowUpdate() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
for (let i = 1; i <= 100; i++) {
sheet.getRange(i, 1).setValue('Value ' + i);
}
}Good practice (fast):
function fastUpdate() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const data = [];
for (let i = 0; i < 100; i++) {
data.push(['Value ' + (i + 1)]);
}
sheet.getRange(1, 1, data.length, data[0].length).setValues(data);
}This tip is applicable to almost all Google Workspace services. Collect your data, then perform a single bulk read or write operation.