I have been creating a script to iterate through a folder structure and copy specific data to rows in a Google Sheet. The folder structure is similar to this
- Root folder
- Street Name 1
- 102
- 104
- etc
- Street Name 2
- 98-110
- Street Name 1
After some work, I came up with this code
// GetFolders function goes through first and second level subfolder
using while loop
// Requires folderID string, an array for storage, and an active sheet to be
passed as arguments
function GetFolders(folderID, dataArray, sheet)
{
var rootFolder = DriveApp.getFolderById(folderID).getFolders();
while (rootFolder.hasNext())
{
var subFolder = rootFolder.next();
var subFolderName = subFolder.getName();
var subFolderID = subFolder.getId();
//append first subfolder name to array
dataArray[0] = subFolderName;
var subFolderIterator = DriveApp.getFolderById(subFolderID).getFolders();
while (subFolderIterator.hasNext())
{
var subFolder2 = subFolderIterator.next();
var subFolder2Name = subFolder2.getName();
//append second subfolder name to array
dataArray[1] = subFolder2Name;
//append second subfolder hyperlink to array
dataArray[2] = subFolder2.getUrl();
//default flag column to blank string
dataArray[3] = '';
//check subFolder2 for hyphen in name
if (subFolder2Name.indexOf('-') > -1) //if a hyphen is present in foldername
{
//append flag to dataArray
dataArray[3] = 'X';
} //end if
//write array contents to next empty row in spreadsheet
sheet.appendRow(dataArray);
} //end while
} //end while
} //end GetFolders()
At first, this appeared to work wonderfully, writing about 3,500 rows before hitting the timeout limit. However, I have over 12,000 rows that need to be written (preferrably with one single execution). Looking at Google's "Best Practices" page tells me that this can be more efficient, but I'm at a loss with how to go about doing this. From my research, it seems that batching would be my best option. Does anyone know a good way to implement this in my script?
You can persist states by saving continuation token of the FileIterator using PropertiesService. The script below checks if the script is nearing the 5-minute time quota limit, saves the continuation token and creates a trigger for the function to run again after set time: