user7935276
user7935276

Reputation: 239

Rename existing google drive folder names

I have over 100+ google drive folders I would like to update the name using GAS. I use this code below but when I tested it, the folder name changes to "undefined". Is .setName() correct method to use or do I need to copy the folder and rename it to accomplish this?

The current test changes the folder name to "undefined" but actural result should be whatever the value that is listed in column B.

Upvotes: 0

Views: 2082

Answers (1)

J. G.
J. G.

Reputation: 1832

Your function is taking columns BC rather than AB.

function renameFolderName() {
    var s = SpreadsheetApp.openById("sheetIDhere").getSheetByName('TEST');

    var data = s.getDataRange().getValues();
    var updated = [];

    for (a = 1; a < data.length - 1; a++) {
        var id = data[i][0];
        var newName = data[i][1];
        var folderlocation = DriveApp.getFolderById(id).setName(newName);
        updated.push(["Yes"]);

    }
    s.getRange(2, 3,updated.length, updated[0].length).setValues(updated);
}

Should be clearer. If you don't care if it runs more slowly, you can do:

function renameFolderName() {
    var s = SpreadsheetApp.openById("sheetIDhere").getSheetByName('TEST');

    var data = s.getDataRange().getValues();

    for (a = 1; a < data.length - 1; a++) {
        var id = data[i][0];
        var newName = data[i][1];
        var folderlocation = DriveApp.getFolderById(id).setName(newName);
        s.getRange(a+1,3).setValue("Yes");
        SpreadsheetApp.flush();

    }

}

This will update as it is running.

Upvotes: 0

Related Questions