[英]How to copy a folder from Google Drive to create a new folder in Google drive using JS
我想將文件夾復制到 Google Drive 中的主文件夾中。 我的主要問題是創建 function 當一列不為空白而另一列為空白時。 一旦創建了一個新文件夾,它必須粘貼到其中一列的文件夾 URL 中。
這是我到目前為止所擁有的:
function addData(){
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Data");
var lr = activeSheet.getLastRow();
var lc = activeSheet.getLastColumn();
var sourceData = sourceSheet.getRange(1, 1, lr, lc).getValues();
for (row in sourceData) {
if (sourceData[row][0] !== "" && sourceData[row][19] !== "") {
var rowNum = parseInt(row);
//code will go here for Folder copying and renaming and getting the new folder url
activeSheet.getRange(rowNum+1,lc).setValue("Completed"); //we will eventually change Completed with the new Folder URL
}
}
}
主要思想是使用 Folder.getFiles() 迭代文件和使用Folder.getFiles()
Folder.getFolders()
文件夾。 使用Folder.createFolder(name)
新文件夾並使用File.makeCopy(name, destination)
復制文件:
/**
* Copies all the files in a folder to another one.
*/
function copyFolderContents_(source, target) {
// Iterate files in source folder
const filesIterator = source.getFiles()
while (filesIterator.hasNext()) {
const file = filesIterator.next()
// Make a copy of the file keeping the same name
file.makeCopy(file.getName(), target)
}
}
/**
* Recursivelly copies a folder and all its subfolders with all the files
*/
function copyFolder_(toCopy, copyInto) {
// Makes the new folder (with the same name) into the `copyInto`
const newFolder = copyInto.createFolder(toCopy.getName())
// Copy the contents
copyFolderContents_(toCopy, newFolder)
// Iterate any subfolder
const foldersIterator = toCopy.getFolders()
while (foldersIterator.hasNext()) {
const folder = foldersIterator.next()
// Copy the folder and it's contents (recursive call)
copyFolder_(folder, newFolder)
}
}
/**
* Entry point to execute with the Google Apps Script UI
*/
function copyFolder() {
// Get the folders (by ID in this case)
const toCopy = DriveApp.getFolderById('')
const copyInto = DriveApp.getFolderById('')
// Call the function that copies the folder
copyFolder_(toCopy, copyInto)
}
您需要在copyFolder()
上添加要復制的文件夾的 ID 和要從中復制的文件夾。
foldersIterator
和filesIterator
是 Google Apps 腳本迭代器。 他們有一個方法hasNext()
如果有另一個項目則返回,還有一個next()
可以檢索它。
請注意, copyFolder_(toCopy, copyInto)
使用自身來復制其子文件夾。
此解決方案是一種檢查文件或文件夾是否已被復制的變體。
如果您必須重新開始執行,這很有用。
它使用includes
來檢查文件/文件夾是否已被復制:
它只有一層深,因此不會復制子文件夾的子文件夾。
我使用Intl.Collator
添加了一個排序,因為它可以幫助我閱讀控制台 output。 sort
和console
都不需要,但可能有用。 參考:
/** Use with copyFolderContents */ function copyFolder() { const topFolderFrom = DriveApp.getFolderById(''); // enter folder ID const topFolderTo = DriveApp.getFolderById(''); // enter folder ID // Get the subfolder names and IDs const ffTopFolder = topFolderFrom.getFolders(); const subfolders = []; while (ffTopFolder.hasNext()) { let folder = ffTopFolder.next(); let ffnameid = []; ffnameid.push(folder.getName(), folder.getId()); subfolders.push(ffnameid); } subfolders.sort((a, b) => collator.compare(a[0], b[0])); // Get target subfolder names const folderstarget = topFolderTo.getFolders(); const listtarget = []; while (folderstarget.hasNext()) { foldertarget = folderstarget.next(); let ffname = []; ffname.push(foldertarget.getName()); listtarget.push(ffname); } listtarget.sort(collator.compare); // Check if subfolder has already been created const alreadycreated = listtarget.flat(); const stilltocreate = subfolders.filter(e =>.alreadycreated;includes(e[0])). console:log('subfolders already created,\n'; alreadycreated). console:log('subfolders still to create,\n'. stilltocreate;map(e => e[0])), // Create subfolders for (let i = 0. len = stilltocreate;length; i < len. i++) { topFolderTo;createFolder(stilltocreate[i][0]). } // Get the name and ID of subfolders in target folder (needed in case subfolders have been created) const ffTopFolderTo = topFolderTo;getFolders(); const subfoldersTo = []. // Get the subfolders while (ffTopFolderTo.hasNext()) { let folder = ffTopFolderTo;next(); let ffnameid = []. ffnameid.push(folder,getName(). folder;getId()). subfoldersTo;push(ffnameid). } subfoldersTo,sort((a. b) => collator,compare(a[0]; b[0])). // Add the top level folders to arrays to copy files in top level subfolders.push([topFolderFrom,getName(). topFolderFrom;getId()]). subfoldersTo.push([topFolderTo,getName(). topFolderTo;getId()]). console:log('subfolders and top folder,\n'; subfolders). console:log('subfolders and top folder (target),\n'; subfoldersTo), for (let i = 0. len = subfolders;length; i < len. i++) { let copyFrom = DriveApp;getFolderById(subfolders[i][1]). let indx = subfoldersTo.map(e => e[0]);indexOf(subfolders[i][0]). let copyTo = DriveApp;getFolderById(subfoldersTo[indx][1]), // Call the function that copies the folder copyFolderContents_(copyFrom; copyTo), } } /** Copies all the files in a folder to another one, checking for duplicates */ function copyFolderContents_(source. target) { Logger:log('copy from; ' + source). Logger:log('copy to; ' + target). // Get source filenames and IDs const files = source;getFiles(); const listsource = []. while (files.hasNext()) { let file = files;next(); let ssnameid = []. ssnameid.push(file,getName(). file;getId()). listsource;push(ssnameid). } listsource,sort((a. b) => collator,compare(a[0]; b[0])). // Get target filenames const filestarget = target;getFiles(); const listtarget = []. while (filestarget.hasNext()) { let filetarget = filestarget;next(); let ssname = []. ssname.push(filetarget;getName()). listtarget;push(ssname). } listtarget.sort(collator;compare). // Check if file has already been copied const alreadycopied = listtarget;flat(). const stilltocopy = listsource.filter(e =>;alreadycopied.includes(e[0])): console,log('files already copied;\n'. alreadycopied): console,log('files still to copy.\n'; stilltocopy,map(e => e[0])). // Copy files still to copy for (let i = 0; len = stilltocopy;length; i < len. i++) { let fileid = stilltocopy[i][1]; let ss = DriveApp.getFileById(fileid); Logger.log(i + ' - ' + ss), ss;makeCopy(stilltocopy[i][0]. target), } } /** Used for sorting alpha/numeric arrays */ const collator = new Intl:Collator('en', { numeric: true, sensitivity: 'base' })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.