Migrating from DocsList to DriveApp?

I am using DocsList for a large project and it works great. Recently, errors have appeared, and basically they have roots with getting a folder or file. When I was doing research, I found that DriveApp was updated. The problem is that DriveApp has no search options like DocsList.

For example, if I had a folder structure like this:

Root -Main Folder 1 --Folder 1 --Folder 2 -Main Folder 2 --Folder 1 --Folder 2 

To get the folder "Folder 1" in "Main folder 2", I can add a search parameter like this: DocsList.getFolder ("Main folder 2 / Folder 1")

With DriveApp, I just don’t understand how to work with it. From what I understand, I need to do something like this for DriveApp:

 var mainFolders = DriveApp.getFoldersByName('Main Folder 2'); while (mainFolders.hasNext()) { var mainFolder = termFolders.next(); var subFolders = termFolder.getFoldersByName('Folder 1'); // Something like this... } 

So, if I had a deeper folder, I would have to expand it even more.?

I feel that instead of simplifying the situation, they have complicated the work of FileIterators and FolderIterators. And just making it hard to “get” a file or folder in code terms.

So basically this question is to find out how a person who uses DocsList to navigate and edit files and folders on a drive can go to DriveApp and achieve the same.

Small / discrete examples of different scenarios would be really helpful. I can take it from there. I will edit this more if you guys think that I don’t understand what I need help about.

+6
source share
2 answers

Discussions with wchiquito comments are interesting to read, but it takes a long time after all the links.

Bottom line: there will be no version of GetAlderByPath () that will be implemented on DriveApp, so you will need to flip it. In the Google+ group, Faustino suggested a workaround, and Eric improved it. Here it with added validation resolves paths starting with "/".

 function getFolderByPath(path) { var parts = path.split("/"); if (parts[0] == '') parts.shift(); // Did path start at root, '/'? var folder = DriveApp.getRootFolder(); for (var i = 0; i < parts.length; i++) { var result = folder.getFoldersByName(parts[i]); if (result.hasNext()) { folder = result.next(); } else { return null; } } return folder; } 

With this, you can simply do myFolder = getFolderByPath('Main Folder 2/Folder 1'); . You will receive an instance of the DriveApp folder.

+6
source

The code below works on mine. It is based on the fact that the same Id

 function convertFileFromDocsListToDriveApp(file) { // Because of difference between DocsList and DriveApp return (file === null) ? null : DriveApp.getFileById(file.getId()); } function convertFolderFromDocsListToDriveApp(folder) { // Because of difference between DocsList and DriveApp return (folder === null) ? null : DriveApp.getFolderById(folder.getId()); } 

I call this in several “strategic” positions in my code. I have not tested the conversion from DriveApp to DocsList, but I expect this to work as well.

+2
source

Source: https://habr.com/ru/post/958731/


All Articles