Google Apps Script coding examples PDF download Code Snippets

Google Apps Script coding examples  #googleappsscript #appsscript #learnappsscript #javascript #googleworkspace #gsuite #automation #productivity #developer #coding #tech

How to create a new spreadsheet?
How to add a row to a spreadsheet?
How to get the value of a cell in a spreadsheet?
How to set the value of a cell in a spreadsheet?
How to send an email?
How to create a new form?
How to send a notification?
How to create a new calendar event?
How to get the current date and time?
How to log an error?
How to get the value of a property?
How to set the value of a property?
How to loop through an array?
How to call a function asynchronously?
How to get the current user?
How to create a new folder?
How to upload a file?
How to download a file?
How to create a new link?
How to get the size of a file?
How to get the type of a file?
How to get the last modified date of a file?
How to make a copy of a file?
How to move a file to a new folder?
How to delete a file?
How to create a new slide in a presentation?
How to add a text box to a slide?
How to add an image to a slide?
How to add a shape to a slide?
How to change the slide layout?
How to add a transition to a slide?
How to preview a presentation?
How to publish a presentation?
How to protect a presentation?
How to create a new document?
How to add a heading to a document?
How to add a list to a document?
How to add an image to a document?
How to add a table to a document?
How to add a link to a document?
How to add a footnote to a document?
How to add a comment to a document?
How to protect a document?
Send email 10 most recent files exercise
Populate from Google Form Exercise
Create slides from Google Sheet exercise
Create calendar event email user exercise
Create File upload to Drive location exercise

Google Apps Script is a scripting language developed by Google that allows you to automate tasks and extend the functionality of Google Workspace apps. With Apps Script, you can write scripts that do things like:

  • Automate tasks. Create scripts that automate repetitive tasks, such as sending emails, updating spreadsheets, or creating presentations.
  • Extend the functionality of Google Workspace apps. Create custom functions and features for Google Sheets, Docs, Slides, Forms, Gmail, Calendar, Drive, and more.
  • Build web apps. Create web apps that can be used by anyone, even those who don’t have Google Workspace accounts.
  • Connect to other APIs. Apps Script can be used to connect to other APIs, such as Twitter, Facebook, and Google Cloud Platform.

To use Apps Script, you need a Google account. Once you have a Google account, you can create a new Apps Script project in the Google Cloud Platform Console.

Once you have created a project, you can start writing scripts. Apps Script uses the JavaScript programming language, so if you know JavaScript, you’ll be able to get started quickly. If you don’t know JavaScript, there are plenty of resources available to help you learn.

Once you’ve written a script, you can run it from the Google Cloud Platform Console or from within a Google Workspace app. You can also publish your scripts so that other people can use them.

Apps Script is a powerful tool that can help you automate tasks, extend the functionality of Google Workspace apps, and build web apps. If you’re looking for a way to get more out of Google Workspace, Apps Script is a great place to start.

How to create a new spreadsheet?

function createSpreadsheet() {

  // Create a new spreadsheet.

  var spreadsheet = SpreadsheetApp.create(‘My New Spreadsheet’);

  // Add a sheet to the spreadsheet.

  var sheet = spreadsheet.getActiveSheet();

  // Set the sheet’s title.

  sheet.setTitle(‘My First Sheet’);

}

How to add a row to a spreadsheet?

function addRow(spreadsheet, sheet, values) {

  // Get the last row in the spreadsheet.

  var lastRow = sheet.getLastRow();

  // Add a new row to the spreadsheet.

  var newRow = sheet.insertRow(lastRow + 1);

  // Set the values of the new row.

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

    newRow.setValue(i, values[i]);

  }

}

How to get the value of a cell in a spreadsheet?

function getValue(spreadsheet, sheet, row, column) {

  // Get the cell at the specified row and column.

  var cell = sheet.getRange(row, column);

  // Get the value of the cell.

  var value = cell.getValue();

  // Return the value.

  return value;

}

How to set the value of a cell in a spreadsheet?

function setValue(spreadsheet, sheet, row, column, value) {

  // Get the cell at the specified row and column.

  var cell = sheet.getRange(row, column);

  // Set the value of the cell.

  cell.setValue(value);

}

How to send an email?

function sendEmail(to, subject, body) {

  // Create a new mail message.

  var mail = GmailApp.createEmail();

  // Set the mail’s recipient.

  mail.setRecipient(to);

  // Set the mail’s subject.

  mail.setSubject(subject);

  // Set the mail’s body.

  mail.setBody(body);

  // Send the mail.

  mail.send();

}

How to create a new form?

function createForm() {

  // Create a new form.

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

  // Add a question to the form.

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

  // Set the question’s type.

  question.setType(‘text’);

  // Add another question to the form.

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

  // Set the question’s type.

  question.setType(’email’);

}

How to send a notification?

function sendNotification(message) {

  // Create a new notification.

  var notification = Notifications.create(‘My New Notification’);

  // Set the notification’s message.

  notification.setMessage(message);

  // Send the notification.

  notification.send();

}

How to create a new calendar event?

function createCalendarEvent(title, start, end) {

  // Create a new calendar event.

  var event = CalendarApp.createEvent(title);

  // Set the event’s start date.

  event.setStart(start);

  // Set the event’s end date.

  event.setEnd(end);

  // Add the event to the calendar.

  CalendarApp.insertEvent(event);

}

How to get the current date and time?

function getCurrentDateTime() {

  // Get the current date and time.

  var dateTime = Utilities.formatDate(new Date(), ‘GMT-05:00’);

  // Return the date and time.

  return dateTime;

}

How to log an error?

function logError(message) {

  // Log an error.

  Logger.log(LoggingLevel.ERROR, message);

}

How to get the value of a property?

function getProperty(object, property) {

  // Get the value of the specified property.

  var value = object[property];

  // Return the value.

  return value;

}

How to set the value of a property?

function setProperty(object, property, value) {

  // Set the value of the specified property.

  object[property] = value;

}

How to loop through an array?

function loopThroughArray(array) {

  // Loop through the array.

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

    // Do something with the current element.

    var element = array[i];

  }

}

How to call a function asynchronously?

function callFunctionAsynchronously(functionName, parameters) {

  // Create a new task.

  var task = new Task(functionName, parameters);

  // Add the task to the queue.

  TaskQueue.add(task);

}

How to get the current user?

function getCurrentUser() {

  // Get the current user.

  var user = UserInfo.getActiveUser();

  // Return the user.

  return user;

}

How to create a new folder?

function createFolder(folderName) {

  // Create a new folder.

  var folder = DriveApp.createFolder(folderName);

}

How to upload a file?

function uploadFile(fileName, filePath) {

  // Upload a file.

  var file = DriveApp.createFile(fileName);

  file.setContent(filePath);

}

How to download a file?

function downloadFile(fileName, filePath) {

  // Download a file.

  var file = DriveApp.getFileById(fileName);

  file.download(filePath);

}

How to create a new link?

function createLink(url) {

  // Create a new link.

  var link = Link.create(url);

}

How to get the size of a file?

function getFileSize(fileName) {

  // Get the size of a file.

  var file = DriveApp.getFileById(fileName);

  var size = file.getSize();

  // Return the size.

  return size;

}

How to get the type of a file?

function getFileType(fileName) {

  // Get the type of a file.

  var file = DriveApp.getFileById(fileName);

  var type = file.getType();

  // Return the type.

  return type;

}

How to get the last modified date of a file?

function getLastModifiedDate(fileName) {

  // Get the last modified date of a file.

  var file = DriveApp.getFileById(fileName);

  var date = file.getLastModifiedDate();

  // Return the date.

  return date;

}

How to make a copy of a file?

function copyFile(fileName, newFileName) {

  // Make a copy of a file.

  var file = DriveApp.getFileById(fileName);

  var copy = file.copy(newFileName);

}

How to move a file to a new folder?

function moveFile(fileName, newFolderId) {

  // Move a file to a new folder.

  var file = DriveApp.getFileById(fileName);

  file.moveTo(newFolderId);

}

How to delete a file?

function deleteFile(fileName) {

  // Delete a file.

  var file = DriveApp.getFileById(fileName);

  file.delete();

}

How to create a new slide in a presentation?

function createSlide(presentation, slideTitle) {

  // Create a new slide in a presentation.

  var slide = presentation.createSlide(slideTitle);

}

How to add a text box to a slide?

function addTextBox(slide, text) {

  // Add a text box to a slide.

  var textbox = slide.addTextBox();

  textbox.setText(text);

}

How to add an image to a slide?

function addImage(slide, imageUrl) {

  // Add an image to a slide.

  var image = slide.addImage(imageUrl);

}

How to add a shape to a slide?

function addShape(slide, shapeType, shapeOptions) {

  // Add a shape to a slide.

  var shape = slide.addShape(shapeType, shapeOptions);

}

How to change the slide layout?

function changeSlideLayout(slide, layoutId) {

  // Change the slide layout.

  slide.setLayout(layoutId);

}

How to add a transition to a slide?

function addTransition(slide, transition) {

  // Add a transition to a slide.

  slide.addTransition(transition);

}

How to add an animation to an object on a slide?

function addAnimation(object, animation) {

  // Add an animation to an object on a slide.

  object.addAnimation(animation);

}

How to preview a presentation?

function previewPresentation(presentation) {

  // Preview a presentation.

  presentation.preview();

}

How to publish a presentation?

function publishPresentation(presentation, publishOptions) {

  // Publish a presentation.

  presentation.publish(publishOptions);

}

How to protect a presentation?

function protectPresentation(presentation, password) {

  // Protect a presentation.

  presentation.protect(password);

}

How to create a new document?

function createDocument(documentType) {

  // Create a new document.

  var document = DocumentApp.create(documentType);

}

How to add a paragraph to a document?

function addParagraph(document, text) {

  // Add a paragraph to a document.

  var paragraph = document.appendParagraph(text);

}

How to add a heading to a document?

function addHeading(document, level, text) {

  // Add a heading to a document.

  var heading = document.appendHeading(level, text);

}

How to add a list to a document?

function addList(document, listType) {

  // Add a list to a document.

  var list = document.appendList(listType);

}

How to add an image to a document?

function addImage(document, imageUrl) {

  // Add an image to a document.

  var image = document.appendImage(imageUrl);

}

How to add a table to a document?

function addTable(document, rows, columns) {

  // Add a table to a document.

  var table = document.appendTable(rows, columns);

}

How to add a link to a document?

function addLink(document, text, url) {

  // Add a link to a document.

  var link = document.createLink(text, url);

}

How to add a footnote to a document?

function addFootnote(document, text) {

  // Add a footnote to a document.

  var footnote = document.appendFootnote(text);

}

How to add a comment to a document?

function addComment(document, text) {

  // Add a comment to a document.

  var comment = document.appendComment(text);

}

How to protect a document?

function protectDocument(document, password) {

  // Protect a document.

  document.protect(password);

}

Send email 10 most recent files exercise

function myFunction() {

  // This function will send an email to the user with a list of their top 10 most recent Google Sheets files.

  // Get the user’s email address.

  var userEmail = Utilities.getUserEmail();

  // Get the list of the user’s top 10 most recent Google Sheets files.

  var files = DriveApp.getFilesByType(‘spreadsheet’).sort(‘created’).limit(10);

  // Create an email message.

  var message = GmailApp.createEmail();

  // Set the email’s subject line.

  message.setSubject(‘Your top 10 most recent Google Sheets files’);

  // Set the email’s body text.

  message.setBody(‘Here is a list of your top 10 most recent Google Sheets files:’);

  // Add the list of files to the email’s body text.

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

    message.appendBody(files[i].getName() + ‘\n’);

  }

  // Send the email.

  message.send();

}

This function will send an email to the user with a list of their top 10 most recent Google Sheets files. It does this by first getting the user’s email address and then getting the list of the user’s top 10 most recent Google Sheets files. Once it has these two pieces of information, it creates an email message, sets the email’s subject line and body text, and then sends the email.

This function is a good example of how Apps Script can be used to automate tasks. In this case, it is automating the task of sending an email to the user with a list of their top 10 most recent Google Sheets files. This could be a useful task for users who want to keep track of their recent work or who want to share their recent work with others.

Populate from Google Form Exercise

function myFunction() {

  // This function will create a new Google Sheets file and populate it with data from a Google Form.

  // Get the form ID.

  var formId = ‘1234567890’;

  // Get the list of form responses.

  var responses = FormApp.getResponses(formId);

  // Create a new Google Sheets file.

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

  // Add a new sheet to the spreadsheet.

  var newSheet = sheet.getActiveSheet();

  // Add a column for each field in the form.

  for (var i = 0; i < responses[0].length; i++) {

    newSheet.appendRow([responses[0][i]]);

  }

  // Rename the columns.

  newSheet.renameColumn(0, ‘Name’);

  newSheet.renameColumn(1, ‘Email’);

  newSheet.renameColumn(2, ‘Phone’);

  newSheet.renameColumn(3, ‘Comment’);

  // Set the sheet’s title.

  newSheet.setTitle(‘My Form Responses’);

  // Display the sheet.

  sheet.show();

}

This function will create a new Google Sheets file and populate it with data from a Google Form. It does this by first getting the form ID and then getting the list of form responses. Once it has these two pieces of information, it creates a new Google Sheets file, adds a new sheet to the file, adds a column for each field in the form, renames the columns, and sets the sheet’s title. Finally, it displays the sheet.

This function is a good example of how Apps Script can be used to automate tasks. In this case, it is automating the task of creating a new Google Sheets file and populating it with data from a Google Form. This could be a useful task for users who want to collect data from a Google Form and then analyze the data in a Google Sheets file.

Create slides from Google Sheet exercise

function myFunction() {

  // This function will create a new Google Slides presentation and populate it with data from a Google Sheet.

  // Get the sheet ID.

  var sheetId = ‘1234567890’;

  // Get the list of sheet data.

  var data = SpreadsheetApp.openById(sheetId).getDataRange().getValues();

  // Create a new Google Slides presentation.

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

  // Add a new slide to the presentation.

  var slide = presentation.insertSlide();

  // Add a title slide to the presentation.

  slide.setTitle(‘My Slides 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 function will create a new Google Slides presentation and populate it with data from a Google Sheet. It does this by first getting the sheet ID and then getting the list of sheet data. Once it has these two pieces of information, it creates a new Google Slides presentation, 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 function is a good example of how Apps Script can be used to automate tasks. In this case, it is automating the task of creating a new Google Slides presentation and populating it with data from a Google Sheet. This could be a useful task for users who want to create presentations from data that is stored in a Google Sheet.

Create calendar event email user exercise

function myFunction() {

  // This function will create a new Google Calendar event and send an email notification to the user.

  // Get the user’s email address.

  var userEmail = Utilities.getUserEmail();

  // Get the event details.

  var eventTitle = ‘My Event’;

  var eventStart = new Date();

  var eventEnd = new Date();

  var eventDescription = ‘This is the description of my event.’;

  // Create a new Google Calendar event.

  var event = CalendarApp.createEvent(eventTitle, eventStart, eventEnd, eventDescription);

  // Send an email notification to the user.

  var message = GmailApp.createEmail();

  message.setSubject(‘Your event reminder’);

  message.setBody(‘You have an event scheduled for ‘ + eventStart + ‘. Please see the details below.’);

  message.appendBody(‘Event title: ‘ + eventTitle);

  message.appendBody(‘Event start date: ‘ + eventStart);

  message.appendBody(‘Event end date: ‘ + eventEnd);

  message.appendBody(‘Event description: ‘ + eventDescription);

  message.sendTo(userEmail);

}

This function will create a new Google Calendar event and send an email notification to the user. It does this by first getting the user’s email address and then getting the event details. Once it has these two pieces of information, it creates a new Google Calendar event, sends an email notification to the user, and then displays the event.

This function is a good example of how Apps Script can be used to automate tasks. In this case, it is automating the task of creating a new Google Calendar event and sending an email notification to the user. This could be a useful task for users who want to create events in Google Calendar and then send email notifications to remind themselves or others about the events.

Create File upload to Drive location exercise

function myFunction() {

  // This function will create a new Google Drive file and upload it to a specific folder.

  // 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);

  // Upload the file to a specific folder.

  var folderId = ‘1234567890’;

  file.moveToFolderById(folderId);

}

This function will create a new Google Drive file and upload it to a specific folder. It does this 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 uploads the file to a specific folder.

This function is a good example of how Apps Script can be used to automate tasks. In this case, it is automating the task of creating a new Google Drive file and uploading it to a specific folder. This could be a useful task for users who want to create and store files in Google Drive.