I need to rename existing Google Drive folders to match the value in cell A1 of a particular sheet in Google Sheets.
Cell A1 in each sheet contains a dropdown selection. Each sheet has a folder containing documents specific to that sheet. I want each folder to always be named the value shown in A1 of that specific sheet.
eg
Folder 1 name = cellA1 of sheet 1
Folder 2 name = cellA1 of sheet 2
etc etc
Is it possible to link folder names to contents of a specific cell?
If my understanding is correct, how about this answer? Please think of this as just one of several answers.
cellA1 of sheet 1
is "sample1" and when "sample1" is changed to "sample2" by the dropdown list, the folder of new name cannot be tracked when this modification is not detected. So as one of several workarounds, I would like to propose to use the OnEdit event trigger and developer metadata for achieving your situation.
There are 2 scripts in this workaround. At first, copy and paste the following script to the container-bound script of Spreadsheet.
// Please run this function for the first time. By this script, the current folder IDs are set to the developer metadata.
function setDevelopermetadata() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
sheets.forEach(function(sheet) {
var folderName = sheet.getRange("A1").getValue();
var folders = DriveApp.getFoldersByName(folderName);
if (folders.hasNext()) {
sheet.addDeveloperMetadata("folderId", folders.next().getId());
}
});
}
// Please install OnEdit event trigger to this function as the installable trigger.
function installableOnEdit(e) {
var range = e.range;
var sheet = range.getSheet();
if (range.getA1Notation() == "A1") {
sheet.getDeveloperMetadata().forEach(function(d) {
if (d.getKey() == "folderId") {
DriveApp.getFolderById(d.getValue()).setName(e.value);
}
});
}
}
At first, please run the function of setDevelopermetadata()
. By this script, the current folder IDs are set to the developer metadata for each sheet.
Please install the OnEdit event trigger to the function of installableOnEdit(e)
as the installable trigger.
installableOnEdit
for "Choose which function to run". Manually modify the value of cell "A1" of Spreadsheet. By this, the OnEdit event trigger is fired and run the script. Then, the folder name is modified by the new value.
If I misunderstood your question and this was not the direction you want, I apologize.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.