0
votes

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?

  • FolderID is listed in Column A
  • FolderName (new foldername) is listed in Column B
  • Updated? is in Column C to denote if row has completed.

    function renameFolderName(){
        var s = SpreadsheetApp.openById("sheetIDhere").getSheetByName('TEST');
        var id = s.getRange(2,1,s.getLastRow()).getValues();
        var foldernames = s.getRange(2,2,s.getLastRow()).getValues();
        var updated = "Yes";
    
    for (a = 0; a < id.length-1; a++){
         for (b = 0; b < foldernames.length-1; b++)
             {
         var folderlocation = DriveApp.getFolderById(id[a][0]).setName(foldernames[b][1]);
         s.getRange(a+2,3).setValue(updated);
             }
             }
             }
    

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

1
Have you run this in the debugger with a break before your for loop to make sure foldernames is in the format you believe it should be in? You will be able to expand foldernames and see the values.Karl_S
@Karl_S, yes. It ran through. This is what I saw in Execution Script: [19-04-16 12:31:01:074 PDT] Folder.setName([undefined]) [0.56 seconds] but should be "Rename TestFolder1". Not sure why it is capturing as "undefined". Maybe I used the wrong method???JMMR...
Can you share the spreadsheet or a copy of it? If you set a break point at the for loop you could then run the debug from in the script editor and expand foldernames. To set the break point, click the line number to the left of the for line. Where you see "select function" select your function and then click the bug icon, which will now be available. The code will run up to the break point.Karl_S
Remove the second for loop, you only need 1 loop here. And use a foldernames to get the name.James D
Here is the copy sheet: docs.google.com/spreadsheets/d/…JMMR...

1 Answers

0
votes

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.