Introduction To Google App Script Programming 20220405
🚀 Introduction to Google Apps Script
Unlock the power of automation. Learn how to use JavaScript to supercharge your Google Workspace, automate tedious tasks, and build custom workflows—all in the cloud!
- A Google Account (Gmail, Google Drive, etc.)
- Basic knowledge of JavaScript (variables, functions, objects)
- Familiarity with Google Sheets or Google Docs
What is Google Apps Script?
Google Apps Script (GAS) is a cloud-based scripting language developed by Google. It uses JavaScript as its foundation, making it incredibly accessible if you already know web development.
Unlike traditional JavaScript that runs in a web browser, Apps Script runs on Google's servers. This means it can interact directly with Google's ecosystem of products.
⚡ Automate Tasks
Automatically send emails, format spreadsheets, or organize your Drive files on a schedule.
📊 Custom Functions
Write your own custom formulas for Google Sheets that go beyond the standard built-in functions.
🔗 Connect APIs
Fetch data from external APIs (like weather, currency, or AI) and push it directly into your Docs or Sheets.
🧩 Build Add-ons
Create custom menus, sidebars, and web apps that integrate seamlessly into Google Workspace.
Step 1: Accessing the Script Editor
There are two ways to write Apps Script:
- Standalone Script: Go to
script.google.comand click "New project". This is independent of any specific file. - Bound Script: Open a Google Sheet, Doc, or Form, go to Extensions > Apps Script. This script is "bound" to that specific file and can easily access its data.
Step 2: Your First Script (Hello World)
Let's write a simple script that logs a message to the console. In your Apps Script editor, delete any existing code and paste this:
function myFirstScript() {
// This logs a message to the Apps Script execution log
Logger.log("Hello, Google Apps Script!");
// This shows a pop-up alert in the Google Sheet UI
SpreadsheetApp.getUi().alert("Hello from Apps Script!");
}How to run it:
- Click the Save icon (or press
Ctrl+S/Cmd+S). - At the top, ensure
myFirstScriptis selected in the function dropdown. - Click the Run button (▶️).
- Google will ask for permissions. Click "Review Permissions", choose your account, and click "Advanced" -> "Go to Untitled project (unsafe)" -> "Allow". (This is normal for new scripts!)
You will see a pop-up alert in your Sheet, and you can view the Logger.log output by clicking Execution log at the bottom.
Step 3: Automating Google Sheets
The real power of GAS is manipulating data. Let's write a script that automatically highlights all cells in Column A that contain the word "Pending".
function highlightPending() {
// 1. Get the active spreadsheet and the first sheet
const sheet = SpreadsheetApp.getActive().getActiveSheet();
// 2. Get the data range (assuming data is in column A, rows 1 to 100)
const range = sheet.getRange("A1:A100");
const values = range.getValues();
// 3. Loop through the data and change background color
for (let i = 0; i < values.length; i++) {
if (values[i][0] === "Pending") {
// Set background to yellow
range.getCell(i + 1, 1).setBackground("#ffff00");
}
}
}Step 4: Understanding "Services"
Apps Script provides built-in "Services" that act as bridges to Google products. You don't need to install any external libraries to use them.
SpreadsheetApp: Interact with Google Sheets (read/write cells, format, create charts).DocumentApp: Interact with Google Docs (create text, insert images, format paragraphs).GmailApp/MailApp: Send emails, search inboxes, manage labels.DriveApp: Manage files and folders in Google Drive.CalendarApp: Create events, check availability, send invites.
Step 5: Triggers (Running Scripts Automatically)
You don't always have to click "Run" manually. Apps Script supports Triggers:
- Simple Triggers: Built-in functions like
onOpen()(runs when a Sheet is opened) oronEdit(e)(runs when a cell is edited). - Installable Triggers: Set up via the UI to run on a schedule (e.g., every day at 8 AM), or when a form is submitted.
Example of a Simple Trigger:
// This runs automatically every time the Google Sheet is opened!
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Scripts')
.addItem('Highlight Pending', 'highlightPending')
.addToUi();
}If you add this to your script and reload your Google Sheet, you'll see a new custom menu called "Custom Scripts" at the top!
Ready to automate your workflow?
Google Apps Script is a game-changer for productivity. Start experimenting, break things, and have fun coding in the cloud! 💻✨
If you found this guide helpful, please share it with your colleagues and leave a comment below!