我正在尝试根据电子表格中的数据在 Google 云端硬盘中自动创建新文件夹。电子表格从表单获取信息。我已经弄清楚如何使用脚本创建新文件夹,但有以下问题:
- 如何避免每次运行脚本时创建重复的文件夹?例如,如果已创建名为“1808”的文件夹,我不希望脚本创建“1808(1)”
我想从其他位置复制电子表格并插入到我正在创建的新文件夹中。新文件名将使用同一电子表格中的信息创建。
function makeFolders(){ //Get array of folder names (fixed & variable) var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getActiveSheet(); var bottomRow = sheet.getMaxRows()-2; var folderNames = sheet.getRange(2, 12, bottomRow, 1).getValues(); //If fixed name cell is blank it throws up an alert and stops if (folderNames === "") { SpreadsheetApp.getUi().alert('Cell A3 is blank.'); } //If folder ID cell is blank it throws up an alert and stops var folderUrl = sheet.getRange(3,14).getValue(); if (folderUrl === "") { SpreadsheetApp.getUi().alert('Cell C3 is blank.'); } //If fixed name cell and file URL aren't empty, run the program if (folderNames !== "" && folderUrl !== "") { //Get ID from folder URL var folderId = folderUrl.match(/[-\w]{25,}/); //Get master folder var getRootFolderId = DriveApp.getFolderById(folderId); //Copy master n times and rename each copy with the new name //stops when it comes to a blank cell for (n=0;n<bottomRow;n++){ if(folderNames[n] == ""){break;} var newFolder = getRootFolderId.createFolder(folderNames[n]); } ss.toast("Your folders have been made! :)","Finished",3); } }**strong text**
最佳答案
How do I avoid creating duplicate folders each time the script is run? For example if a folder named '1808' has already been created, I don't want the script to create '1808(1)'
您可以获得所有文件夹名称的列表,并循环遍历它们以检查是否有任何与新文件夹名称匹配!像这样的东西:
function MakeNewFolders(rootFolderID, folderNames)
// Get master folder
var rootFolder = DriveApp get folder (rootFolderID)
var subfolders = rootFolder.getFolders()
var alreadyExists = false;
var newFolder;
// Check if any subfolder already exists
// loop for folders first, to minimize GAS function calls
while (subfolders.hasNext()) {
var subfolderName = subfolders.next().getName();
loop (each folder in folderNames) {
if (subfolderName == newFolderName)
alreadyExists = true;
}
if (!alreadyExists)
newFolder = rootFolder.createFolder(newFolderName)
}
如果您需要循环遍历大量文件夹(超过 60 个左右),here is a code snippet you might find handy 。基本上,the original Google example当您可以仅使用 try-catch block 并忽略“您已到达末尾!”时,通过检查 folder.hasNext()
来获取子文件夹列表的运行速度会慢两倍。它抛出异常。应该将执行时间减少一半! :)
I want to copy a spreadsheet from another location and insert in the new folders that I am creating. The new file name is to be created with information from the same spreadsheet.
尝试这样的事情!
function CopyFile(originalFileID, destinationFolderID) {
var destinationFolder = DriveApp.getFolderById(destinationFolderID) ;
var fileToCopy = DriveApp.getFileById(originalFileID)
var copiedFile = fileToCopy.makeCopy(<new name>, destinationFolder)
fileToCopy.setTrashed(true); // alternatively, you can use
// Drive.Files.remove(originalFileID);
}
希望这有帮助!自从我使用 GAS 以来已经有一段时间了,希望它能编译:)
关于google-apps-script - 创建新文件夹并复制谷歌表格,使用新名称到新文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47800475/