简体   繁体   中英

How to name a Google Drive folder based on cell value in Google Sheets

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?

  • You want to change the folder name based on the value of cell "A1".
  • The cell "A1" has the dropdown list.
  • When the value of "A1" is modified, you want to also modify the folder name by the modified value.
  • You want to achieve this using Google Apps Script.

If my understanding is correct, how about this answer? Please think of this as just one of several answers.

Issue and workaround:

  • At Google Drive, all files and folders are managed by IDs. So the same folder name can be set to to several folders under the same folder.
  • For example, 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.

  • The OnEdit event trigger is used for executing the script when the cell is modified.
  • The developer metadata is used for setting the folder ID. Using the folder ID, the folder name is modified.
    • I think that by knowing the folder IDs, the process cost can be reduced when the folder name is modified.

Sample script:

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);
      }
    });
  }
}

Usage:

  1. At first, please run the function of setDevelopermetadata() . By this script, the current folder IDs are set to the developer metadata for each sheet.

    • So this function supposes that the current folder names of the cell "A1" of each sheet are corresponding to the actual folder names.
  2. Please install the OnEdit event trigger to the function of installableOnEdit(e) as the installable trigger.

    • Open the script editor.
    • Edit -> Current project's triggers.
    • Click "Add Trigger".
    • Set installableOnEdit for "Choose which function to run".
    • Set "From spreadsheet" for "Select event source".
    • Set "On edit" for "Select event type".
  3. 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.

Note:

  • In this sample script, from your question, it supposes that all sheets in the active Spreadsheet has the values of folder names at the cell "A1". If I misunderstood about this, can you provide the detail information?
  • If you don't want to use the OnEdit event trigger, please tell me.

References:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM