Reputation: 239
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?
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.
Upvotes: 0
Views: 2082
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