Google Apps Script Examples 2

Google Apps Script Examples 2

Example 1: Create a Google Form
Example 2: Send an email notification
Example 3: Create a Google Sheet
Example 4: Get the current time
Example 5: Get the user’s location

Example 1: Create a Google Form

This code will create a new Google Form.

function createForm() {

  // Create a new Google Form.

  var form = FormApp.create(‘My Form’);

  // Add a question to the form.

  var question = form.addQuestion(‘What is your name?’);

  question.setRequired(true);

  // Add another question to the form.

  var question = form.addQuestion(‘What is your email address?’);

  question.setRequired(true);

  // Save the form.

  form.save();

}

This code works by first creating a new Google Form object. Then, it adds two questions to the form, one for the user’s name and one for the user’s email address. Finally, it saves the form.

This code could be used to create a form that users can fill out to provide their contact information. For example, you could use this code to create a form that users can fill out to sign up for your newsletter or to request a consultation.

Example 2: Send an email notification

This code will send an email notification to the user.

function sendEmail() {

  // Get the user’s email address.

  var userEmail = Utilities.getUserEmail();

  // Create an email message.

  var message = GmailApp.createEmail();

  message.setSubject(‘Your email notification’);

  message.setBody(‘This is the body of your email notification.’);

  // Send the email notification.

  message.sendTo(userEmail);

}

This code works by first getting the user’s email address. Then, it creates an email message and sets the subject line and body of the message. Finally, it sends the email notification.

This code could be used to send email notifications to users about important events or updates. For example, you could use this code to send email notifications to users when their account is created or when their password is changed.

Example 3: Create a Google Sheet

This code will create a new Google Sheet.

function createSheet() {

  // Create a new Google Sheet.

  var sheet = SpreadsheetApp.create(‘My Sheet’);

  // Add a row to the sheet.

  var row = sheet.appendRow([1, 2, 3]);

  // Add another row to the sheet.

  var row = sheet.appendRow([4, 5, 6]);

  // Save the sheet.

  sheet.save();

}

This code works by first creating a new Google Sheet object. Then, it adds two rows to the sheet, one with the values 1, 2, and 3 and one with the values 4, 5, and 6. Finally, it saves the sheet.

This code could be used to create a Google Sheet that stores data. For example, you could use this code to create a Google Sheet that stores the names and contact information of your customers.

Example 4: Get the current time

This code will get the current time.

function getCurrentTime() {

  // Get the current time.

  var currentTime = Utilities.now();

  // Display the current time.

  Logger.log(‘The current time is: ‘ + currentTime);

}

This code works by first getting the current time using the Utilities.now() method. Then, it displays the current time using the Logger.log() method.

This code could be used to get the current time for any purpose. For example, you could use this code to get the current time to display on a website or to log the current time in a database.

Example 5: Get the user’s location

This code will get the user’s location.

function getUserLocation() {

  // Get the user’s location.

  var userLocation = Geolocation.getGeolocation();

  // Display the user’s location.

  Logger.log(‘The user’s location is: ‘ + userLocation);

}

This code works by first getting the user’s location using the Geolocation.getGeolocation() method. Then, it displays the user’s location using the Logger.log() method.