Google Apps Script中的文件夹结构问题



我有一个脚本,它在文件夹中创建一个文件(文件夹从单元格值命名,文件有一个静态名称)。我所希望的是一个脚本,将创建一个父值(在这种情况下,一年从单元格eg。2021),然后在其下的子文件夹(在这种情况下,员工类型是设置的,而不是来自单元格)。司机)。

我真正纠结的是得到我想要的文件夹结构——它最终看起来像这样:

"Year"(from cell value)——>"员工Type"(静态值)——比;"员工Name"(from cell values)——>"index.html"(静态值)

使用下面的代码,我使结构的最后两部分工作,但无法获得"Year"或"员工类型";文件夹。

我当前的代码如下,但我似乎不知道如何将上面的2个文件夹添加到这个脚本。

function driverPages() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getActiveSheet();
var rg = sh.getRange(2, 17, sh.getLastRow(), 2);
var vs = rg.getValues().filter(r => r.every(Boolean));
vs.forEach(r => DriveApp.getFolderById(deletedriverPages('1Xsl2r_RPgqQ1i8K3aF23hov82JmNFySu',r[0], '2020')).createFile('index.html', r[1]));
}
function deletedriverPages(parentId, name) {
var parent = DriveApp.getFolderById(parentId);
var id;
var folders = parent.getFoldersByName(name);
if (folders.hasNext()) {
var folder = folders.next();
id = folder.getId();
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
if (file.getName() == 'index.html') { file.setTrashed(true) }
}
}
else {
var folder = parent.createFolder(name);
id = folder.getId();
}
return id;
}

你可以试着做一个递归函数来遍历文件夹层次结构和创建任何缺失的文件夹。下面是一个示例(未测试):

function driverPages() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getActiveSheet();
var rg = sh.getRange(2, 17, sh.getLastRow(), 2);
var vs = rg.getValues().filter(r => r.every(Boolean));
vs.forEach(r => DriveApp.getFolderById(deletedriverPages('1Xsl2r_RPgqQ1i8K3aF23hov82JmNFySu' [r[0], 'Employee Type', '2020'])).createFile('index.html', r[1]));
}
function deletedriverPages(parent, folderNames) {
//if parent is a string, it is assumed to be a folder ID
if(typeof parent === "string") {
return deletedriverPages(DriveApp.getFolderById(parent), folderNames);
//while there are more folder names in the array, keep processing and then recursively call the function
} else if(folderNames.length > 0) {
let name = folderNames.pop();
let folders = parent.getFoldersByName(name);
//if the folder exists, get it - otherwise create it
let folder = folders.hasNext() ?  folders.next() : parent.createFolder(name);
return deletedriverPages(folder, folderNames);
//when the array is empty, delete index files if they exist
} else {
var files = parent.getFiles();
while (files.hasNext()) {
var file = files.next();
if (file.getName() === 'index.html') { file.setTrashed(true) }
}
return parent.getId();
}
}

相关内容

  • 没有找到相关文章

最新更新