Google Apps Script Examples  4

Google Apps Script Examples  4

Example 1: Create a new Google Slides presentation
Example 2: Send an email notification to the user
Example 3: Create a Google Drive file
Example 4: Get the current time

Example 1: Create a new Google Slides presentation

This code will create a new Google Slides presentation.

function createPresentation() {

  // Create a new Google Slides presentation.

  var presentation = SlidesApp.create(‘My Presentation’);

  // Add a new slide to the presentation.

  var slide = presentation.insertSlide();

  // Add a title slide to the presentation.

  slide.setTitle(‘My Presentation’);

  // Add a subtitle slide to the presentation.

  slide = presentation.insertSlide();

  slide.setSubtitle(‘This is a subtitle slide.’);

  // Add a new slide for each row of data in the sheet.

  for (var i = 0; i < data.length; i++) {

    slide = presentation.insertSlide();

    // Add a title to the slide.

    slide.setTitle(data[i][0]);

    // Add a paragraph to the slide for each column of data in the row.

    for (var j = 1; j < data[i].length; j++) {

      slide.appendParagraph(data[i][j]);

    }

  }

  // Save the presentation.

  presentation.save();

}

This code works by first creating a new Google Slides presentation object. Then, it adds a new slide to the presentation, adds a title slide to the presentation, adds a subtitle slide to the presentation, adds a new slide for each row of data in the sheet, adds a title to each slide, and adds a paragraph to each slide for each column of data in the row. Finally, it saves the presentation.

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

Example 2: Send an email notification to the user

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 Drive file

This code will create a new Google Drive file.

function createFile() {

  // Get the file name.

  var fileName = ‘My File.txt’;

  // Get the content of the file.

  var content = ‘This is the content of my file.’;

  // Create a new Google Drive file.

  var file = DriveApp.createFile(fileName);

  // Set the content of the file.

  file.setContent(content);

  // Save the file.

  file.save();

}

This code works by first getting the file name and then getting the content of the file. Once it has these two pieces of information, it creates a new Google Drive file, sets the content of the file, and then saves the file.

This code could be used to create a file that users can download. For example, you could use this code to create a file that users can download that contains a list of your products or services.

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