Migrating from DocsList to DriveApp?

前端 未结 2 450
青春惊慌失措
青春惊慌失措 2020-12-17 02:22

I\'ve been using DocsList for a big project and it was working perfectly. Lately, bugs have been popping up and they mostly have roots with getting a folder or file. When I

相关标签:
2条回答
  • 2020-12-17 02:40

    The discussions from wchiquito's comment are an interesting read, but following all the links is time-consuming.

    Bottom line: There will not be DriveApp version of getFolderByPath(), so you will need to roll your own. In the Google+ group, Faustino proposed a work-around and Eric improved it. Here it is, with an added check to allow paths that start 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 that, you can simply do myFolder = getFolderByPath('Main Folder 2/Folder 1');. You will end up with a DriveApp Folder instance.

    0 讨论(0)
  • 2020-12-17 02:55

    The code below works at mine. It is based on having 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 a few 'strategic' positions in my code. I didn't test conversion from DriveApp to DocsList, but I expect this to work as well.

    0 讨论(0)
提交回复
热议问题