I have managed to record a folder's file/folder directory into a Google Sheet. From this Google Sheet I would like to copy this folder structure by reading each row.
Google Sheet Link: https://docs.google.com/spreadsheets/d/1qgDd8PEmHSYz5IsN9banYBjbmAyoLLVe3WMnOvmHdlE/edit?usp=sharing
function copyFolderTree() {
const sourceFolderId = '1uTGq2MRHzbev23sQzzFCi3Pl-v-ntMql';
const sourceFolder = DriveApp.getFolderById(sourceFolderId).getName();
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getSheetByName('Sheet1');
const data = ws.getDataRange().getValues();
range.shift()
const destinationRootFolder = DriveApp.createFolder(`${sourceFolder}`)
data.forEach(function(row){
Logger.log(row)
let depth = row[4]
if (row[4] === depth && row[2] === 'Folder') {
Logger.log(`Name of folder is currently ${row[4]}`)
Logger.log(`Depth is currently ${row[4]}`)
Logger.log(`Type is currently ${row[2]}`)
destinationRootFolder.createFolder(row[0])
row.push('hello')
Logger.log(row)
range.setValues(values);
}
})
}
I realise this is the incorrect thinking. Do I need to place the newly created folder into it's copied parent folder using the folder's name?
Thank you,
[1]: https://docs.google.com/spreadsheets/d/1qgDd8PEmHSYz5IsN9banYBjbmAyoLLVe3WMnOvmHdlE/edit?usp=sharing
CodePudding user response:
From From this Google Sheet I would like to copy this folder structure by reading each row.
and First I would like to create the folder structure. My next step would be to work on transferring the files across.
, when you want to copy a folder (in your sample Spreadsheet, it's "Amy Bits".) including all subfolders and files using Google Apps Script, how about the following sample script? I have created a Google Apps Script library for achieving this situation. So in this answer, I would like to propose the script using the Google Apps Script library.
Usage:
1. Install library.
You can see the method for installing the library at here.
2. Enable Drive API.
In this library, Drive API is used. So please enable Drive API at Advanced Google services.
3. Sample script:
When your sample Spreadsheet is used, the sample script is as follows.
function myFunction() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
const sourceFolderId = sheet.getRange("B2").getValue().split("/")[5]; // Please set the source folder ID.
const destinationFolderId = DriveApp.createFolder("sample").getId(); // Please set the destination folder ID.
const object = { sourceFolderId, destinationFolderId, overwrite: true };
const res = CopyFolder.copyAllFilesFolders(object);
console.log(res);
}
If you can directly put the source folder ID and the destination folder ID, you can also the following sample script.
function myFunction() { const object = { sourceFolderId: "###", // Please set the source folder ID. destinationFolderId: "###", // Please set the destination folder ID. overwrite: true, }; const res = CopyFolder.copyAllFilesFolders(object); console.log(res); }
Note:
- If the script process time is over 6 minutes, you can see one more sample script. Ref
Reference:
CodePudding user response:
For folder tree, you can try
function copyFolderTree() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getSheetByName('Sheet1');
const range = ws.getDataRange();
const values= range.getValues();
var levels = []
values.forEach(r => {
if (r[2]=='Folder'){
if (r[4]==0){ // root
var dossier = DriveApp.createFolder(r[0]);
levels[1] = dossier.getId(); // specific correction as 1 doesn't exists in the example provided
}
else {
var parent = DriveApp.getFolderById(levels[r[4]-1]);
var dossier = parent.createFolder(r[0]);
levels[r[4]] = dossier.getId();
}
}
})
}