语境
在我的云端硬盘中,具有以下结构:
Ops文件夹|| 销售文件夹
Ops包含3个文件夹:Process1 / Process2 / Process3
Sales包含3个文件夹:Customer1 / Customer2 / Customer3
目标
我在“驱动器”>“销售”中询问文件夹的名称
在工作表中,我列出了一些潜在客户的名称。我正在逐一检查潜在客户的名称是否与我的云端硬盘中的任何文件夹匹配。
如果存在与潜在客户名称匹配的文件夹,则不执行任何操作。
如果没有适用于该潜在客户的文件夹,我想在Sales中使用潜在客户名称创建一个新文件夹
function prepararPropuesta() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var ssAS = ss.getActiveSheet()
var ssProspects = ss.getSheetByName('Prospects')
var lr = ssProspects.getLastRow()
var lc = ssProspects.getLastColumn()
// I'm looking for the column 'Prospect' in the Sheet
for(i=2; i<lc; i++){
var columnas = ssProspects.getRange(2, i).getValue()
Logger.log(columnas)
if(columnas == 'Nombre Prospect'){
var colProspect = ssProspects.getRange(2, i).getColumn()
Logger.log(colProspect)
}
}
/*
Now I'm looking for the names of the folders in my Drive
This is looking in all my Drive. It would be nice to filter the
search and look just in SubFolders(Sales)
*/
var folders = DriveApp.searchFolders('"me" in owners');
while (folders.hasNext()) {
var folder = folders.next();
Logger.log(folder.getName());
/* The first Prospect Name is in row 2 and I'm creating a loop to
get each Name
If the name matches an existing Folder then ends the function
If the Prospect Name is not found in the Folders list then creates a new Folder under the name prospectName
*/
for(i=2; i<lr; i++){
var prospectName = ssProspects.getRange(i, colCliente).getValue()
var folderName = folder.getName()
if(folderName = prospectName){
return
}
}
DriveApp.createFolder(prospectName)
}
}
在代码的两行之间,您将逐步找到描述
(1)我找到了潜在客户名称所在的列->正常工作
(2)我正在使用WHILE来获取我的云端硬盘中的所有名称文件夹
Logger.log(folder.getName())
退货
[18-04-21 06:33:35:985 PDT] FolderIterator
是否正确获取名称?
(3)我创建了变量folderName和futureName,并循环它们以检查是否存在匹配项->此部分无法正常工作。我想虽然我在developers.google中进行了研究,但我没有使用正确的方法。怎么做?
谢谢!
在循环内使用.getValue()不是一个好主意,因为在这种情况下,您需要为每一行对电子表格进行一次调用。在一次调用中获取所有值,然后在脚本中处理返回的数组被认为是一种好习惯。
我不确定为什么需要遍历各列以查找具有名称的列?假设您知道该列是什么,就可以直接获取该列的值(名称)。
这是一个示例,假设工作表“前景”的B列中的名称(更改范围以适合)。对于找到的每个名称,都会调用函数checkIfFolderExistElseCreate()。该函数检查名称为(第二个参数)的文件夹是否存在。如果确实如此,则不会发生任何其他情况,将创建具有该名称的文件夹。
该函数的第一个参数是将在其中创建新文件夹的“父”文件夹。
function prepararPropuesta() {
var parent = DriveApp.getFolderById("id_of_parentFolder")
SpreadsheetApp.getActive().getSheetByName('Prospects').getRange('B2:B').getValues()
.forEach(function (r) {
if(r[0]) checkIfFolderExistElseCreate(parent, r[0]);
})
}
function checkIfFolderExistElseCreate(parent, folderName) {
var folder;
try {
folder = parent.getFoldersByName(folderName).next();
} catch (e) {
folder = parent.createFolder(folderName);
}
}
我希望这有帮助?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句