javascript 如何根据Google工作表列中的值移动子文件夹?

bihw5rsg  于 2023-02-07  发布在  Java
关注(0)|答案(1)|浏览(118)

我有一个谷歌表,我用它来索引属性列表,与应用程序脚本设置编辑创建一个属性子文件夹内的草案文件夹使用的值在A和B列时,'草案'是在C列选定。
即参考+位置
我目前正在尝试让脚本移动属性子文件夹之间的'草稿文件夹','活文件夹','存档文件夹'取决于列C中的列表状态.(这些文件夹存在于一个主要的父文件夹,我想避免手动移动属性子文件夹之间的草稿,活和存档.
然而,我遇到了问题,在属性子文件夹不移动方面。文件夹ID是正确的,文件夹名称完全匹配,但当我选择“实时”或“存档”在C列,属性子文件夹不从草稿移动到选择文件夹,什么也没有发生。
有谁能告诉我哪里出错了,以及如何在C列移动选择的属性文件夹?
应用程序脚本很简单,它检查文件夹是否已经存在,如果不存在,则在草稿中创建子文件夹,ELSE检查文件夹是否需要移动。

以下是要将属性子文件夹移动到的设置文件夹的ELSE IF

} else if ((status == 'Live' && statusFoldername != 'Live Folder') || (status == 'Archived' && statusFoldername != 'Archive Folder')) {
    
                if (statusFoldername != 'Live Folder') {
                    var moveToFolder = liveFolder;
                } else {
                    var moveToFolder = archveFolder;
                }
                propertyFolder.moveTo(moveToFolder);
            }

以下是完整版本。

function createFolderSubFolders(e) {
    // Get Parent folder
    var parentFolder = DriveApp.getFolderById('ID');
    var draftFolder = DriveApp.getFolderById('ID');
    var liveFolder = DriveApp.getFolderById('ID');
    var archveFolder = DriveApp.getFolderById('ID');

    // Get current spreadsheet
    var ss = SpreadsheetApp.openById('ID');
    var i = e.range.getRow();

    // Process if Col B is not empty and Col C equals 'Draft'

    if(ss.getRange('C' + i).getValue() == 'Draft') {
        
    //Set intended folder name
        var ref = ss.getRange('A' + i).getDisplayValue();
        var location = ss.getRange('B' + i).getValue();
        var status = ss.getRange('C' + i).getValue();
        var propertyFolderName = ref + ' - ' + location;
        
    ///Check if folder already exists
        var findTest = false;
        //Get FolderIterator for looking into status folders
        var statusFolders = parentFolder.getFolders();
        // Checks if Parent Folder already has Folder with given name
        while (statusFolders.hasNext() && findTest == false) {
            var statusFolder = statusFolders.next();
            //Property Listings in current iterated status folder.
            var propertyFolders = statusFolder.getFolders();
            while (propertyFolders.hasNext() && findTest == false) {
                var propertyFolder = propertyFolders.next();

                if (propertyFolderName == propertyFolder.getName()) {
                    //Check where property folder lies currently.
                    var statusFoldername = statusFolder.getName();
                    findTest = true;
                    break;
                }
            }
        }
        // Create new property folder in draftFolder if not already created.
        if (findTest == false) {

            var propertyFolder = draftFolder.createFolder(propertyFolderName);

            //Otherwise check if folder needs to be moved to other section.
        
    } else if ((status == 'Live' && statusFoldername != 'Live Folder') || (status == 'Archived' && statusFoldername != 'Archive Folder')) {

**//Set folder where to move the propertyFolder
            if (statusFoldername != 'Live Folder') {
                var moveToFolder = liveFolder;
            } else {
                var moveToFolder = archveFolder;
            }
            propertyFolder.moveTo(moveToFolder);
        }**
    }
}
p8h8hvxi

p8h8hvxi1#

我相信你的目标如下。

  • 您有一个包含"A"至"C"列的工作表。当"C"列的下拉列表发生变化时,您希望使用"A"和"B"列创建一个文件夹名称,并希望从"Live", "Archived", "Draft"的3个文件夹中搜索该文件夹名称。当未找到该文件夹名称且"C"列的值为"草稿"时,你想创建一个新的文件夹,使用文件夹名称为"草稿"文件夹。当找到文件夹名称时,你想使用列"C"的值移动文件夹。

修改点:

  • 当我看到你的脚本时,每个文件夹ID都是唯一的ID。所以,在这种情况下,不需要扫描每个文件夹中的所有文件夹。所以,我认为可以直接使用每个文件夹中的文件夹名称搜索文件夹。
  • 并且,为了从列"A"到"C"检索值,这可以通过一次调用来实现。

通过修改这些要点,脚本可能会变得简单,脚本的处理成本可能会降低。当这些要点反映在您的脚本中时,以下修改如何?

修改脚本1:

请将以下脚本复制粘贴到Spreadsheet的脚本编辑器中并保存。并且,在这种情况下,为了通过改变列"C"的下拉列表来运行脚本,请在函数createFolderSubFolders中安装OnEdit触发器。

function createFolderSubFolders(e) {
  // I prepared this object from your script.
  // Please set your folder IDs.
  var obj = {
    parentFolder: "###",
    searchFolders: [
      { name: "Draft", id: "###" },
      { name: "Live", id: "###" },
      { name: "Archived", id: "###" },
    ]
  };
  var sheetName = "Sheet1"; // Please set your sheet name.

  var { range, value } = e;
  var sheet = range.getSheet();
  if (sheet.getSheetName() != sheetName || range.columnStart != 3 || !["Live", "Archived", "Draft"].includes(value)) return;
  var [ref, location, status] = sheet.getRange(range.rowStart, 1, 1, 3).getDisplayValues()[0];
  var propertyFolderName = ref + ' - ' + location;
  var check = obj.searchFolders.reduce((o, { name, id }) => {
    var f = DriveApp.getFolderById(id).getFoldersByName(propertyFolderName);
    if (f.hasNext()) {
      o = { name, f: f.next() };
    }
    return o;
  }, null);
  if (!check && status == "Draft") {
    DriveApp.getFolderById(obj.searchFolders[0].id).createFolder(propertyFolderName);
  } else if (check && status != "Draft" && check.name != status) {
    check.f.moveTo(DriveApp.getFolderById(obj.searchFolders.find(({ name }) => name == status).id));
  }
}
  • 在这个修改后的脚本中,我认为上述目标已经实现。

示例脚本2:

我想下面的修改脚本也许可以使用,在这个修改中,propertyFolderName的文件名是通过一个调用来搜索的。

function createFolderSubFolders(e) {
  // I prepared this object from your script.
  // Please set your folder IDs.
  var obj = {
    parentFolder: "###",
    searchFolders: [
      { name: "Draft", id: "###" },
      { name: "Live", id: "###" },
      { name: "Archived", id: "###" },
    ]
  };
  var sheetName = "Sheet1"; // Please set your sheet name.

  var { range, value } = e;
  var sheet = range.getSheet();
  if (sheet.getSheetName() != sheetName || range.columnStart != 3 || !["Live", "Archived", "Draft"].includes(value)) return;
  var [ref, location, status] = sheet.getRange(range.rowStart, 1, 1, 3).getDisplayValues()[0];
  var propertyFolderName = ref + ' - ' + location;
  var q = `title='${propertyFolderName}' and mimeType='${MimeType.FOLDER}' and (` + obj.searchFolders.map(({ id }) => `'${id}' in parents`).join(" or ") + ") and trashed=false";
  var f = DriveApp.searchFolders(q);
  var check = f.hasNext();
  if (!check && status == "Draft") {
    DriveApp.getFolderById(obj.searchFolders[0].id).createFolder(propertyFolderName);
  } else if (check && status != "Draft") {
    var fol = f.next();
    var pid = fol.getParents().next().getId();
    var pname = obj.searchFolders.find(({ id }) => id == pid).name;
    if (pname != status) {
      fol.moveTo(DriveApp.getFolderById(obj.searchFolders.find(({ name }) => name == status).id));
    }
  }
}

参考文献:

相关问题