AppsScriptPulse

Bulk add unique data to an SQL Table in Apps Script

The following code combines Google Apps Script and SQL to bulk insert a Google Sheet of data into an existing SQL Table, but only after checking the record does not already exist so that it can be safely re-run.

Source: The Gift of Script: Bulk add unique data to an SQL Table in Apps Script

Email scheduler for Gmail – overview

Email scheduler spreadsheet columns image

Email scheduler spreadsheet columns

Over the next few blog posts I look to explore my development of an email scheduler for Gmail that is designed to let you schedule when you want an email composed in your drafts to be sent (by specifying the date/time).

This was written prior to Google introducing an email scheduler themselves.

Source: The Gift of Script: Email scheduler for Gmail – overview

Loop delete specific rows with counter

Example table with specific rows to delete

Example table with specific rows to delete

Table with row 3 now deleted and data shifted up

Table with row 3 now deleted and data shifted up

The following Google Apps Script has been created to help with a recent difficulty I was having with deleting rows containing a specific value, where the loop was losing the row position due to the spreadsheet data all shifting up a row each time one was deleted. Some searching online suggested looping through a spreadsheet in reverse (from bottom up) to avoid the shift, but that seems more like a workaround than a direct solution …

Source: The Gift of Script: Loop delete specific rows with counter

Data Validation – check for blank cells

The following Google Apps Script code was developed as a tool to check that all the necessary data existed in a spreadsheet before another function was triggered. The reason for this was that if any data was missing it would affect the student Group files I was creating (eg no email address or name) that would eventually prevent further functions from running at a later date.

Source: The Gift of Script: Data Validation – check for blank cells

Bulk save emails from Gmail

Screenshot of setup sheet for tool.

Screenshot of setup sheet for tool.

This tool is designed to bulk save emails (and associated attachments) from a specified Gmail label into a Google Drive folder, with relevant threads combined into a single PDF document.

Source: The Gift of Script: Bulk save emails from Gmail – overview

Log actions performed by a user running a Google Apps Script

Screenshot of spreadsheet with logs from script

Example log output from script to spreadsheet

A standalone function that you could include in your scripting projects to help determine which user ran a script and what actions it has performed. It is designed to output the information into a Google sheet – Date/Time, User, Action.

Source: The Gift of Script: Log actions performed by a user running a script

Convert Doc to PDF and move into a new folder with Google Apps Script

Create a PDF version of a Google Doc, move it into a new folder, remove its parents so it only exists in the new folder (typically also exists in My Drive) and then trash the Doc.

Source: The Gift of Script: Convert Doc to PDF and move into a new folder

Check if a date is more than a month ago

Recently during a consultation I was asked if it would be possible to check if the date submitted on a Google form was over a month ago, to which I responded “sure …”.

Admittedly this was a lot more difficulty than I thought it would (and should) be – I was bamboozled by online forums and posts suggesting to convert dates into numbers and perform other incoherent functions to achieve this. Eventually however I came across the single JavaScript function that I would need to achieve this feat …… ‘getMonth’.

Source: The Gift of Script: Check if a date is more than a month ago

Protect a named range in a Google sheet

The following Google Apps Script code is from some recent learning I have been doing when asked about locking-down certain areas of a sheet. I knew of named ranges and protecting cells but not quite that it could be done with apps script.

Source: The Gift of Script: Protect a named range in a Google sheet

Get weekday name from a date

Take a date and extract the day of the week from it. By making use of ‘formatDate’ and the pattern ‘EEEE’ within a few lines of code we have a workable week-day name that could be used in an if statement as discussed in the previous post.

Source: The Gift of Script: Get weekday name from a date – alternative

Dynamically remove Google Form options

Screenshot of Google Form appointment slots
Google Form appointment slots

This was developed as I wanted to learn about removing Google Form option choices as part of exploring its capabilities for use with appointment slots. I am aware there are a number of Google add-ons that achieve this but they come with potential data protection issues when accessing your Google Account, hence an in-house approach.

The principle is simply that a user would select their desired appointment slot on the Form and then that option would be unavailable for the next user accessing the Form.

Source: The Gift of Script: Dynamically remove Google Form options

Copy template sheet to Google Sheets and reorder

Template sheet with signatures to copy to other files

Screenshot of template sheet with signatures to copy to other files

Take a template Google Sheet (containing fields for 3 signatures) and copy it into other Google Sheet files within a given Google Drive folder and then reorder those sheets so that it was the first one.

The idea was based around needing users to authorise student marks with their signature from multiple Google Sheet reports produced from a third party system.

Source: The Gift of Script: Copy template sheet to Google Sheets and reorder

Batch get existing Google Drive folder IDs & names

Get the folder IDs of a large number of current Google drive folders along with parts of their name – for capturing in a Google Sheet.

Source: The Gift of Script: Batch get existing folder IDs & names – version 2.0

Fixing dates from a Google Form

For those getting a ‘date’ from a Google Form and finding that it flips the day/month the wrong way around. This small piece of code can be inserted to create a correct date format.

Source: The Gift of Script: Fixing dates from a Google Form

‘While’ loop with a ‘Sleep’ and ‘Toast’

Screenshot of toast notification in Google Sheet
Screenshot of toast notification in Google Sheet

Understanding how to use a simple ‘while’ loop that pops up a ‘toast’ message every 5 seconds by making use of ‘Utilities.sleep’ to pause the script.

This was created as part of some testing for another script designed to bulk move files between Google Drive folders. Without the toast notification it was difficult to determine the progress being made, so adding it in allowed for the name of the file last copied to be displayed to the user. This script removes all of the extra features and shows the basic while loop and toast.

Source: The Gift of Script: While loop with a Sleep and Toast

Clear cell contents on Google Sheet edit

Screenshot of coloured columns to highlight actions
Screenshot of coloured columns to highlight actions

This is designed to clear the adjacent cell to the right of the one that has just been edited – so long as it is within the column we specify.

It was developed as I was testing the available options for a Stock Inventory system where an item that was to be marked as back in stock would automatically clear an email timestamp in the adjacent cell (ready for the whole ordering process to start again).

Source: The Gift of Script: Clear cell content on Google Sheet edit

Create alphabetised folders in Google Drive

Screenshot of Google Drive folder layout

The script is designed to bulk create folders from a Google Sheet and to arrange them into alphabetised folders within Google Drive, by extracting the first letter of a user’s surname.

If a suitable alphabetised folder does not exist in Google Drive, then one is created. The script also adds the created folder to an ‘Everybody’ folder so that there is a single area with all the folders listed in.

Source: The Gift of Script: Create alphabetised folders in Google Drive

Bulk rename files in Google Drive

Screenshot of File renamer text fields

Screenshot of File renamer text fields

This is designed to automate bulk renaming of multiple files within a single Google Drive folder. It has been developed as a Google Sheet with flexibility towards entering your own search pattern and replacement string as you see fit.

Source: The Gift of Script: Bulk rename files in Google Drive

Search Google Calendar date range for specific events

Search through a given date range on Google Calendar, look through the events and then extract their Title & Id.

Source: The Gift of Script: Search Google Calendar date range for specific events

Bulk send emails from a Google Sheet

This is designed to loop through each row of a Google Sheet and send an email containing information from it. This means you can send personalised emails with ad-hoc information for each recipient quickly.

It has been developed so that it is easily expandable for you to add further columns/rows. The script has a number of features to achieve this:

  1. A menu option for easily selecting to send email, with a confirmation dialogue popup.
  2. A timestamp as confirmation for when an email was sent.
  3. A check that an Email Address is present and there is no current value in the Email Sent column – to prevent repeat email sending but also allow ad-hoc emailing by clearing this column for those you wish to send again.
  4. A try/catch to help automatically deal with problematic emails that may break the script – an error message will be written into the Email Sent column and the script will continue on to the next row.

Source: The Gift of Script: Bulk send emails from Google Sheet

Subscribe to Apps Script Pulse...