Tuesday 15 January 2019

Batch get existing folder IDs & names

The following Google Apps Script code was developed to get the folder IDs of a large number of current Google drive folders along with parts of their name - for capturing in a spreadsheet. This was required for a project I am currently working on - to develop a Gmail add-on for saving an email into an existing drive folder. It is split into 2 functions.

The folders have a consistent naming convention:
  • SURNAME, Forename (Preferred Forename) (Student No) (Username) - Subject
Image of example folders to extract details from
Example folders to extract details from
The end result would be:
Image of extracted details in a spreadsheet
Extracted details in spreadsheet
1) getFolders
Start with the folder ID of the parent that contains all of the sub-folders to query 'getFolders'. I then work through each sub-folder in turn 'hasNext' to 'getId' and store this as a variable. We next need to 'getName' so it can be split via a regex (explained in this blog post) to get the surname, forename and username.

An array of the above data is then created and passed through to the below function for copying into the spreadsheet.

2) pasteData
With an array of data as the input parameter we simply get the relevant spreadsheet details and the next empty row ('getLastRow'). As there are 4 pieces of data we set the dimensions of the range to 4 columns and then finally 'setValues'.

Get existing Folder IDs/Names.xlsx

No comments:

Post a Comment