How to automatically rename folder based on uploaded file if there are duplicated folder name Google Form

馋奶兔 提交于 2020-12-13 03:58:07

问题


This is the next episode of my previous question (which was solved): Google Form Upload files to specific new folder based on the value submitted

SO, I succeeded to create a new folder on Google Drive based on value inputted by user in field NAME using this script.

function onFormSubmit(e) {
  const folderId = "###";  // Please set top folder ID of the destination folders.
  const form = FormApp.getActiveForm();
  const formResponses = form.getResponses();
  const itemResponses = formResponses[formResponses.length-1].getItemResponses();

  Utilities.sleep(3000); // This line might not be required.

  // Prepare the folder.
  const destFolder = DriveApp.getFolderById(folderId);
  const folderName = itemResponses[0].getResponse();
  const subFolder = destFolder.getFoldersByName(folderName);
  const folder = subFolder.hasNext() ? subFolder : destFolder.createFolder(folderName);

  // Move files to the folder.
  itemResponses[1].getResponse().forEach(id => DriveApp.getFileById(id).moveTo(folder));
}

The problem is when there are 2 or more person with the same NAME uploaded a file it will fail to create the folder. And put the files on the root folder instead.

What I need is to add suffix on the folder name. Let's say there are 3 person with NAME = noobsee. SO, the folder should be:

  1. noobsee_01
  2. noobsee_02
  3. noobsee_03

How to do it?


回答1:


I believe your goal as follows.

  • When the folder name of value of Name is existing, you want to create new folder by adding the suffix.
    • As a sample situation, when the folder name of sample is existing and Name of sample is submitted, you want to rename the existing folder name to sample_01 and want to create new folder with the folder name of sample_02.
  • In your situation, the script is put in the container-bound script of Google Form. And onFormSubmit has been installed as the installable trigger of OnSubmit trigger.

In this case, I would like to propose to modify the script of "Prepare the folder." as follows.

Modified script:

From:

// Prepare the folder.
const destFolder = DriveApp.getFolderById(folderId);
const folderName = itemResponses[0].getResponse();
const subFolder = destFolder.getFoldersByName(folderName);
const folder = subFolder.hasNext() ? subFolder : destFolder.createFolder(folderName);

To:

// Prepare the folder.
const destFolder = DriveApp.getFolderById(folderId);
let folderName = itemResponses[0].getResponse();
const subFolder = destFolder.searchFolders("title contains '" + folderName + "'");
const reg = new RegExp(`^${folderName}$|^${folderName}_\\d{2}$`);
if (subFolder.hasNext()) {
  const folderNames = {};
  while (subFolder.hasNext()) {
    const fol = subFolder.next();
    const fName = fol.getName();
    if (reg.test(fName)) folderNames[fName] = fol;
  }
  const len = Object.keys(folderNames).length;
  if (len == 1 && folderNames[folderName]) {
    folderNames[folderName].setName(folderName + "_01");
  }
  folderName += "_" + ("00" + (len + 1)).slice(-2);
}
const folder = destFolder.createFolder(folderName);
  • In this case, only when the same value of Name is submitted, the suffix is added.
  • From your question, it supposes that the number of digits of suffix is 2 like _01, _02,,,.


来源:https://stackoverflow.com/questions/64565806/how-to-automatically-rename-folder-based-on-uploaded-file-if-there-are-duplicate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!