Google Apps Script: Build web apps and automate tasks with Google Apps Script. Tutoriel

 

https://www.google.com/script/start/




Google Apps Script
: Build web apps and automate tasks with Google Apps Script.



What is Google Script


Google Apps Script is a scripting language and runtime that is used to personalize and extend Google's suite of productivity tools, including Google Sheets, Google Docs, and Google Forms. 

It is primarily based on JavaScript and is tightly integrated with Google's cloud services, allowing you to automate tasks and workflows, add customized functionality, and connect with external APIs and services.


Some of the things you can do with Google Apps Script include:

Automate repetitive tasks: Google Apps Script approves you to automate repetitive tasks in Google Sheets, such as sending emails, generating reports, and formatting data.


Create custom add-ons and extensions: You can create customized add-ons and extensions for Google Sheets and other Google apps, adding new functionality to these tools.


Connect to exterior services: You can use Google Apps Script to connect to external services and APIs, permitting you to integrate with third-party tools and services.


Build custom applications: With Google Apps Script, you can construct custom web applications that run on Google's infrastructure, making it effortless to create and deploy your own applications.

Google provides enormous documentation and resources for Google Apps Script, including tutorials, sample code, and a developer forum. 


You can get started with Google Apps Script by creating a new project in the Google Apps Script editor, which is reachable within Google Sheets, Google Docs, and other Google apps. 


From there, you can start writing code and experimenting with the competencies of the platform.





Google Script: Automate repetitive tasks


Google Apps Script is a powerful tool for automating repetitive duties in Google Sheets, Google Docs, and other Google apps.

 Here are some ideas and examples of how you can use Google Apps Script to automate tasks and store time:

Send personalized emails: You can use Google Apps Script to send personalized emails primarily based on data in a Google Sheet. 


For example, you can write a script that sends a customized email to every customer in a sales list, using their identity and purchase history to tailor the message.


Automate report generation: If you want to generate reports regularly, you can use Google Apps Script to automate the process. 


For example, you can write a script that pulls data from a Google Sheet, creates a record in Google Docs, and then emails the report to a list of recipients.


Automatically format data: If you often work with data that needs to be formatted in a specific way, you can use Google Apps Script to automate the process. 

For example, you can write a script that mechanically formats dates, converts text to uppercase, or adds prefixes/suffixes to a certain range of cells in a Google Sheet.



Collect data from external sources: You can use Google Apps Script to join external APIs and services and collect information automatically. 


For example, you can write a script that collects data from a weather API and updates a Google Sheet with the latest climate information.



Schedule tasks: You can use Google Apps Script to schedule tasks to run at specific times. 

For example, you can write a script that sends a reminder electronic mail to yourself every day at 8am or update a report robotically at the end of each week.

Here is an example script that sends personalized emails using data from a Google Sheet:


function sendEmails() { var sheet = SpreadsheetApp.getActiveSheet(); var data = sheet.getDataRange().getValues(); for (var i = 1; i < data.length; i++) { var row = data[i]; var name = row[0]; var email = row[1]; var message = "Hi " + name + ", thanks for your purchase!"; MailApp.send email(email, "Thanks for your purchase!", message); } }


This script gets data from the active sheet and sends a personalized email to each row of data, using the name and email address in the first two columns of the sheet. 


You can run this script by opening the script editor in Google Sheets and clicking the "Run" button, or by setting up a trigger to run the script automatically at a specific time.


Here are some extra ideas and examples for automating repetitive tasks with Google Apps Script:

Automatically update records from external sources: You can use Google Apps Script to automatically update records in a Google Sheet from external sources. 

For example, you can write a script that pulls data from a database or API and updates a Google Sheet with the latest information.

Build customized forms and surveys: You can use Google Apps Script to build custom types and surveys that collect and store data in a Google Sheet. 

For example, you can create a shape that collects customer feedback and stores the responses in a Google Sheet for later analysis.


Create customized menus and user interfaces: You can use Google Apps Script to create custom menus and user interfaces for your Google Sheets and different Google apps. 

For example, you can create a custom menu that includes shortcuts to frequently-used scripts or functions.


Analyze data and generate insights: You can use Google Apps Script to analyze statistics in a Google Sheet and generate insights and visualizations. 

For example, you can write a script that calculates average sales by location and generates a chart to visualize the data.

Integrate with other Google services: You can use Google Apps Script to integrate with other Google services, such as Google Analytics, Google Drive, and Google Calendar. 

For example, you can write a script that pulls statistics from Google Analytics and updates a Google Sheet with the latest traffic information.


Here is an example script that automatically updates data from an external source:

function updateData() { var url = "https://api.example.com/data"; var response = UrlFetchApp.fetch(url); var json = JSON.parse(response.getContentText()); var sheet = SpreadsheetApp.getActiveSheet(); var data = sheet.getDataRange().getValues(); for (var i = 1; i < data.length; i++) { var row = data[i]; var id = row[0]; var value = json[id]; sheet.getRange(i+1, 2).setValue(value); } }


This script pulls data from an external API and updates a Google Sheet with the latest values, based on a unique ID in the first column of the sheet. Depending on your needs, you can customize this script to work with different APIs and data sources.



GoogleScript. Create custom add-ons and extensions


You can use Google Apps Script to create custom add-ons and extensions for Google Sheets, Docs, Slides, and Forms.

 
Add-ons are small applications that add new functionality to Google Apps, while extensions allow you to customize and extend the capabilities of Google Apps.

Here are some ideas for custom add-ons and extensions you can create with Google Apps Script:

1. Mail Merge Add-on: Create a custom add-on that allows users to send personalized emails and letters using data from a Google Spreadsheet.


2. Workflow Automation: Create a custom add-on that automates common workflows, such as document approvals, task assignments, and email notifications.


3. Custom data validation: Create a custom add-on that adds custom data validation rules to Google Sheets, such as validating phone numbers or email addresses.


4. Document formatting: Create a custom extension that adds custom document formatting options to Google Docs, such as page margins or custom font styles.


5. Image Manipulation: Create a custom extension that adds image manipulation tools to Google Slides, such as cropping, resizing, and adding filters.


-Here is an example of creating a custom add-on for Google Sheets using Google Apps Script:

1. Create a new Google sheet and open the script editor by selecting "Tools" -> "Script editor" from the menu.


2. Write a script that adds a custom menu item to the sheet, such as "My add-on" -> "Do something".


function onOpen() { var ui = SpreadsheetApp.getUi(); ui.createMenu('My Add-on') .addItem('Do Something', 'doSomething') .addToUi(); }


3. Write a function that performs some action when the menu item is selected, such as displaying a dialog box with a message.


function doSomething() { var ui = SpreadsheetApp.getUi(); ui.alert('Hello, world!'); }


4. Save the script and go back to the sheet. You should see a new menu item called "My Add-on" with a submenu item called "Do Something".


5. Click on "Do Something" and you should see a dialog box with the message "Hello, world!".


This is a simple example, but you can customize the script to perform more complex tasks depending on your needs.

 You can also publish the add-on to the Google Workspace Marketplace so that others can install and use it.




Google Script: Connect to external services


Google Apps Script allows you to connect to external offerings and APIs to retrieve data or perform actions. 

Here are some examples of how you can use Google Apps Script to connect to exterior services:

1. Google Maps API: You can use Google Apps Script to connect to the Google Maps API and retrieve data such as geolocation information, driving directions, and distance calculations. 

You can additionally use Google Apps Script to create custom maps and overlays on top of Google Maps.


2. Twitter API: You can use Google Apps Script to connect to the Twitter API and retrieve statistics such as tweets, followers, and trends. 

You can also use Google Apps Script to automate Twitter actions such as sending tweets or following users.


3. Slack API: You can use Google Apps Script to connect to the Slack API and retrieve records such as messages, channels, and users. 

You can also use Google Apps Script to automate Slack actions such as sending messages or creating channels.


4. Google Analytics API: You can use Google Apps Script to join the Google Analytics API and retrieve data such as website traffic, user behavior, and conversion rates.

 You can additionally use Google Apps Script to automate Google Analytics actions such as creating reports or sending alerts.



Here is an instance of how you can use Google Apps Script to connect to an external service (in this case, the OpenWeatherMap API) and retrieve data:

1. Create a new Google Sheet and open the script editor by selecting "Tools" -&gt; "Script editor" from the menu.


2. Write a script that connects to the OpenWeatherMap API and retrieves weather data for a unique location. 

function get weather data() { var apiKey = 'your_api_key'; var location = 'New York'; var URL = 'https://api.openweathermap.org/data/2.5/weather?q=' + location + '&appid=' + apiKey; var response = UrlFetchApp.fetch(URL); var data = JSON.parse(response.getContentText()); return data; }

3. Replace 'your_api_key' with your actual OpenWeatherMap API key.


4. Call the getWeatherData function to retrieve the weather data and log it to the console.

function test() { var data = get weather data(); Logger.log(data); }


5. Save the script and run the test function. You should see the weather data for New York logged into the console.


This is a simple example, but you can use Google Apps Script to connect to any external service or API that provides a web-based interface. 


You can combine multiple APIs to create more complex workflows and automate repetitive tasks.



Google Script: Build custom applications



Google Apps Script lets you build custom applications that can be used inside the Google ecosystem. 


Here are some ways in which you can use Google Apps Script to build custom applications:


Custom Google Sheets add-ons: You can use Google Apps Script to create customized add-ons for Google Sheets that add new functionality or automate repetitive tasks.

 These add-ons can be shared with others and installed directly from the Google Sheets Add-ons menu.


Custom Google Forms add-ons: You can use Google Apps Script to create customized add-ons for Google Forms that add new features or integrate with external services.

 These add-ons can be shared with others and established directly from the Google Forms Add-ons menu.


Custom Google Drive apps: You can use Google Apps Script to build custom purposes that integrate with Google Drive and allow users to operate actions such as uploading files, organizing folders, and sharing documents. 

These applications can be posted to the Google Workspace Marketplace and made available to a wider audience.


Custom Google Sites: You can use Google Apps Script to customize Google Sites and add new features such as dynamic content, interactive widgets, and customized branding. 

These customizations can be applied to both public and private Google Sites.


Here is an instance of how you can use Google Apps Script to build a custom application that integrates with Google Sheets:


1. Create a new Google Sheet and open the script editor by selecting "Tools" -&gt; "Script editor" from the menu.


2. Write a script that adds a custom menu to the Google Sheets UI and permits users to perform custom actions. 


function onOpen() { var ui = SpreadsheetApp.getUi(); ui.createMenu('Custom Menu') .addItem('Do Action 1', 'doAction1') .addItem('Do Action 2', 'doAction2') .addToUi(); } function doAction1() { // Add your custom code here } function doAction2() { // Add your custom code here }


3. Replace the placeholder code in doAction1 and doAction2 with your own custom code.


4. Save the script and reload the Google Sheet. You should see a new "Custom Menu" option in the menu bar.


5. Click on the "Custom Menu" option and select one of the custom actions. 

Your custom code will be executed and the results will be displayed in the Google Sheet.


This is a simple example, but you can use Google Apps Script to build more complex applications that interact with external services, perform data analysis, and automate business processes. 

The possibilities are endless, and the Google Apps Script platform provides a powerful and flexible environment for building custom applications within the Google ecosystem. 


Good Luck


Comments

Popular Posts