what I'm trying to do?
I'm trying to list all files in my subfolder "Annual Reports" on my Google Drive to my google spreadsheet but
I have a tons of folders and subfolders on my google drive
and that caused an error "exceeded maximum execution time" when I tried to run the code to get list of all folders and files
The structure of my google drive folder is
- My Drive
- Subfolder 1: Documents by country or issue
- Subfolder2 :Albania
- Subfolder3 :Annual Reports >files
- Subfolder2 : Bulgaria
- Subfolder3 : Annual Reports >files
and so on..for every country
So,I manage to set my subfolder "Documents by country or issue" to be rootfolder and get ID list of 'Annual Report' folder
as in this script code
var sheet = SpreadsheetApp.getActiveSheet();
function generateFolderTree() {
sheet.clear();
sheet.appendRow(["Folder Name", "Rootfolder name", "ID"]);
var parent = DriveApp.getFoldersByName("Documents by country or issue").next();
getChildFolders(parent);
}
function getChildFolders(parent) {
var childFolders = parent.getFolders();
var parentName = parent.getName();
while (childFolders.hasNext()) {
var childFolder = childFolders.next();
fileName = childFolder.getName();
root = parentName + "/" + fileName;
fileID = childFolder.getId();
if (fileName == "Annual Reports") {
sheet.appendRow([fileName, root, fileID]);
} else if (fileName == "_Annual Reports") {
sheet.appendRow([fileName, root, fileID]);
}
and it works , I got the result on my sheet like this(can't upload pic,don't know why)
column A || column B || column C
Folder Name || Rootfolder name || ID
Annual Reports || Colombia/Annual Reports||
0Bz02kQQ7_a6paTM1SmdpZ01HYjQ_Annual Reports || Slovakia (SK)/_Annual Reports ||0BwW8rsOAeCzneUlDUk1pcGNlVU0
Annual Reports || Switzerland (CH)/Annual Reports ||0Bz02kQQ7_a6pZTJsWDR0elJTaDQ
So I want to know ***how to write a script to create new sheet for each country and
get the list of files inside folder from folderID(like get value from ranges--column C) that I got on my spreadsheet.
Or Is there recommended way to make this more easier?
Thank you.