ASS03: Create Your First Apps Script Project
Introduction
This lesson will guide you through creating your first Google Apps Script project using either the online script editor or the CLASP terminal command. This follows the previous lesson, which covered setting up your Google Apps Script development environment.
Presentation
Create New Project
A new Apps Script project can be created using the Apps Script online editor or the CLASP (Command Line Apps Script Project) command line tool.
- Container-bound projects can be created from Google Sheets, Docs, Slides, and Forms.
- Standalone projects can be created from here (https://script.google.com/home) or Google Drive.
- Create new project with CLASP command:
clasp create –title “project name” –type “sheets” # sheets-bound project
clasp create –title “project name” –type “docs” # docs-bound project
clasp create –title “project name” –type “slides” # forms-bound project
clasp create –title “project name” –type “forms” # forms-bound project
clasp create –title “project name” –type “standalone” # standalone project
My First Apps Script Project
My first Apps Script project involved automating a daily task for my boss. He needed timestamps in several tracking files for his daily standup meetings. Team members were previously responsible for updating these timestamps manually, which was prone to errors and forgetfulness. My script automated this process, ensuring that the filenames were always updated with the correct timestamp.
My Solution
To address this issue, I'm utilizing the "onOpen" trigger function, which activates whenever the Spreadsheet is opened. Subsequently, a custom function is created to fetch the current timestamp and update the Spreadsheet's name by separately appending or updating the timestamp. This custom function is then invoked within the onOpen trigger.
My Sample Code
The lines of my sample code are explained in the comments below.
/**
* Special trigger in Apps Script which will be called when a document opens
*/
function onOpen() {
// call function here to update the file name
updateFileName_();
}
/**
* The hidden function to update the current Spreadsheet name by adding current
* timestamp
*/
function updateFileName_() {
// get the active Spreadsheet
const ss = SpreadsheetApp.getActive();
// get the name of the active Spreadsheet,
// use let here because we'll need to mutate the name later
let name = ss.getName();
// the timestamp format: M - month, m: minute
const datetimeFormat = "ddMMyy hh:mm:ss";
// get the current datetime value
const now = new Date();
// get the script timezone which can be defined in the project settings or
// appsscript.json file
const timezone = Session.getScriptTimeZone();
// get the formatted timestamp for using the Utilities API
const timestamp = Utilities.formatDate(now, timezone, datetimeFormat);
// Check if the filename ends with a timestamp format
if (/\d{6}\s\d{2}:\d{2}:\d{2}/.test(name)) {
// If yes, replace the old one with the new one to create the new file name
name = name.slice(0, name.length - datetimeFormat.length) + timestamp;
} else {
// If no, append the timestamp to the name to create new file name
name = `${name} ${timestamp}`;
}
// finally, rename the Spreadsheet with the new file name
ss.rename(name);
}
You First Project
Start by identifying small issues in your daily workflow. Then, try to find a solution and create an Apps Script project to implement it. It's always good practice to start with small projects that address your own problems and continue learning from there. You'll gradually improve your skills and become more proficient.
Apps Script Simplified
Links
Join Memberships Make a copy Github Hire Me Buy Me a Coffee
Previous Read
ASS02: Setting Up Your Apps Script Development Environment
Next Read
Comments
Post a Comment