如果文件夹名称重复,如何根据上传的文件自动重命名文件夹 [英] How to automatically rename folder based on uploaded file if there are duplicated folder name Google Form

查看:192
本文介绍了如果文件夹名称重复,如何根据上传的文件自动重命名文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我上一个问题的第二集(已解决):

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

因此,我根据用户使用此脚本在NAME字段中输入的值成功在Google云端硬盘上创建了一个新文件夹.

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

问题是,当有2个或更多具有相同NAME的人上载文件时,它将无法创建文件夹.并将文件放在根文件夹中.

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.

我需要在文件夹名称上添加后缀.假设NAME = noobsee有3个人.因此,该文件夹应为:

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

该怎么做?

推荐答案

我相信您的目标如下.

  • 当存在值为Name的文件夹名称时,您想通过添加后缀来创建新文件夹.
    • 作为一种示例情况,当sample的文件夹名称存在并且提交sampleName时,您想要将现有文件夹名称重命名为sample_01,并希望使用该文件夹创建新文件夹sample_02的名称.
    • 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 this case, I would like to propose to modify the script of "Prepare the folder." as follows.

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

      收件人:

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

      • 在这种情况下,仅当提交相同的Name值时,才添加后缀.
      • 根据您的问题,它假设后缀的位数为2,例如_01_02,....
        • 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,,,.
        • 这篇关于如果文件夹名称重复,如何根据上传的文件自动重命名文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆