Best Google Apps Script for Developers

Hello, Guys welcome back with the Best Google Apps Script for Developers. Google Apps Script allows you to easily integrate data and functionality from Gmail, Google Drive, Google Maps, YouTube, and the majority of other Google APIs. Apps Script is JavaScript behind the hood, so you don’t have to learn a new language or maintain any servers because all of your code runs on the Google Cloud, not your browser.

You’ll learn how to create Google Apps Script projects locally on your PC using Visual Studio Code in this video tutorial. You can write your code in modern JavaScript, nicely arranged in modules, and the build environment will utilise Babel and Webpack to transform it into a JavaScript version that is Apps Script compliant.

How to Universal Find and Replace for Google Documents in Drive

Modern Development with Google Apps Script

There are numerous advantages to using a local development environment rather than writing code on the Apps Script Cloud IDE.

  1. You can use ES6 Classes, Arrow Functions, Modules, Destructing, and all of the other current JavaScript capabilities to write code.
  2. The programming experience with VS Code is unrivalled, and tools like ESLint and Prettier help you identify mistakes early in the development process.
  3. With npm scripts and CLASP, Google’s command line utility for Apps Script, the development and deployment processes may be totally automated.
  4. VS Code includes Git support and connects with source control providers such as Github and Gitlab. As a result, it is simpler to trace changes and restore older versions of the code.
  5. JavaScript libraries such as LoDash, Moment, Underscore, and any of the NPM packages can be easily integrated into your code.
  6. To construct the HTML frontend that connects to the backend using the Google Script Client API, you can utilise current frameworks such as React, Vue.js, and Angular.

Getting Started with the Apps Script Starter

The Starter package provides a boilerplate for getting started quickly with local Apps Script programming inside VS Code. Run the following commands in your terminal:

1. Clone the Github repository to a local folder

git clone https://github.com/labnol/apps-script-starter my-project

2. Switch to the project folder

cd my-project

3. Install all the project dependencies and utilities

npm install

4. Connect CLASP to your Google account

npx clasp login

5. Create a new Google Apps Script project in your Google Drive with CLASP

npx clasp create --title "My Project" --rootDir ./dist --type standalone

This command will produce a new.clasp.json file in your project folder that connects your Apps Script project to the local folder. Webpack will bundle all of your code into a single JavaScript file and add it to the./dist folder that Clasp will submit to your Apps Script project during the build process.

Then, using the code. command, access the current project folder within VS Code. It contains some sample code, but we’ll start with a blank folder, so remove anything in the src folder.

Create a new file, email.js, within the src folder, and write a basic arrow function that outputs a list of all the email addresses associated with your Gmail account.

apps-script-starter (1).png

Next, in the src folder, create an index.js file (entry point), import the email function from the email.js file, and add it to the global object. This is a prerequisite of the Google Apps Script Webpack plugin.

You can also explicitly add a function expression to the global object, such as doGet in the example below.

htmlservice-doget.png

Now that your JavaScript code is complete, enter the appsscript.json file in your project folder and edit the oAuthScopes value to contain only the scopes necessary by your project.

Then, go to the command line prompt and type deploy to push your code to the Apps Script project.

npm run deploy

When you deploy the project for the first time, you will see a prompt that says “Manifest file has been updated.” Do you wish to overwrite and push? (y/N)” – Yes, please.

After the deployment is complete, use the CLASP open command to launch the related script in the browser.

npx clasp open

Go to the Run menu within the Apps Script Editor and choose the getEmailAddress function from the list. When you open the logs, your email addresses should appear in the window.

Then, from the Publish menu, select Deploy as web app and open the URL in a new browser tab to see the programme output. That is how simple it is to create projects using the Google Apps Script starter kit.

Using Git with Google Apps Script

Make a new repository on Github and save the URL of the new repository. To push your Apps Script project to Github, open the terminal and run the following commands.

github-apps-script.png

Also See: How to Get the Permanent URL of an Email Message in Gmail with Apps Script easily

Video: Google Apps Script Tutorial

Leave a Comment