Briggott
Briggott

Reputation: 1

Google Script DriveApp assistance

I've been using this script that helps me find manage folders on Google drive, by showing empty folders and deleting them.

I'm trying to amend it to specify the starting folder, by ID, using DriveApp.getFolderById('folderID') but I keep getting errors.

This is a section of the original code:

  function getEmptyFolders() {

    // Get sheet
    var sheet = SpreadsheetApp.getActive().getSheetByName('Manage Empty Folders')
    if (!sheet) {
        SpreadsheetApp.getActive().insertSheet('Manage Empty Folders')
    }

    var sheet = SpreadsheetApp.getActive().getSheetByName('Manage Empty Folders')
    var clearContent = sheet.clear()

    var folders = DriveApp.getFolders();

    var fileArray = [
        ["Folder Name", "Folder Id", "Folder URL"]
    ]
    while (folders.hasNext()) {

        var folder = folders.next()

        // If the folder has no files or child folder in it, call them out.
        if (folder.getFiles().hasNext() == false && folder.getFolders().hasNext() == false) {

            var name = folder.getName()
            var id = folder.getId()
            var url = folder.getUrl()

            // Push empty folder details into array
            fileArray.push([name, id, url])


        }

    }

    // formatting 
    var headerRow1 = sheet.getRange("A1:C1").setBackground("#d9d9d9").setFontColor("#000000").setFontWeight("Bold")
    sheet.setColumnWidths(1, 4, 300)

    // write results to the sheet
    sheet.getRange(1, 1, fileArray.length, 3).setValues(fileArray)

}

which works great but when I try to change

var folders = DriveApp.getFolders();

to

var folders = DriveApp.getFolderByID('folderID');

I get an error:

TypeError: folders.hasNext is not a function

I'm quite new to Google Script and thought I'd understood the documentation, but obviously not!

Upvotes: 0

Views: 270

Answers (1)

Marios
Marios

Reputation: 27348

Issue

  • That is because getFolderById(id) returns an object of type folder and as the error also suggests, folder does not have a method hasNext as you can also see in the attached link.

  • On the other hand, getFolders() returns an object of type FolderIterator which is actually a collection of folder objects and therefore hasNext() is required to iterate over these folders.

Although you can get the folder and iterate over all the subfolders:

var main_folder = DriveApp.getFolderByID('folderID'); 
var folders = main_folder.getFolders(); 

Solution:

If you want to use getFolderById then you should remove the while loop. Since you have a single folder there is no need of iterating and in fact it is wrong to do so:

function getEmptyFolders() {
    // Get sheet
    var sheet = SpreadsheetApp.getActive().getSheetByName('Manage Empty Folders')
    if (!sheet) {
        SpreadsheetApp.getActive().insertSheet('Manage Empty Folders')
    }
    var sheet = SpreadsheetApp.getActive().getSheetByName('Manage Empty Folders')
    var clearContent = sheet.clear()
    // get the parent folder by its id
    var main_folder = DriveApp.getFolderById('folderID'); // new code
    // get all subfolders
    var folders = main_folder.getFolders(); // new code


    var fileArray = [
        ["Folder Name", "Folder Id", "Folder URL"]
    ]
    while (folders.hasNext()) {

        var folder = folders.next()

        // If the folder has no files or child folder in it, call them out.
        if (folder.getFiles().hasNext() == false && folder.getFolders().hasNext() == false) {

            var name = folder.getName()
            var id = folder.getId()
            var url = folder.getUrl()

            // Push empty folder details into array
            fileArray.push([name, id, url])
        }
    }
    // formatting 
    var headerRow1 = sheet.getRange("A1:C1").setBackground("#d9d9d9").setFontColor("#000000").setFontWeight("Bold")
    sheet.setColumnWidths(1, 4, 300)
    // write results to the sheet
    sheet.getRange(1, 1, fileArray.length, 3).setValues(fileArray)
}

Upvotes: 1

Related Questions