How to update DocsList to DriveApp in my code

后端 未结 2 600
北恋
北恋 2020-11-27 21:43

My script that generates a pdf document from a template within Google Drive and emails it to a recipient based on columns in a spreadsheet stopped working today due to the d

相关标签:
2条回答
  • 2020-11-27 22:17

    Many of the DriveApp methods are identical to DocsList. So, in many cases, you can simply replace DocsList with DriveApp.

    Problems arise with the old getFolder() method.

    var folder = DocsList.getFolder('Name of your folder');
    

    In this case, simply replacing DocsList with DriveApp will give you a problem. There is no getFolder() method for DriveApp. In Google Drive, you can have multiple files and folders with identical names (But different ID's). The old DocsList, getFolder() method did not take this situation into account. With DriveApp, you can still search for a folder by name, but the return is a Folder Iterator. With DriveApp the method name for getting a folder by name is very slightly different; it's "folders", with an "s" on the end. getFoldersByName(name) Plural, not singular. So, even though 98% of the time, getting folders by name will result in only one folder, you still need process the Folder Iterator. You don't need to loop through the folder iterator. You can just get the first folder. Just use the next() method without using a programming loop.

    Folder Iterator

    So, I suggest that you do just replace DocsList with DriveApp except in the case of getting a folder by name. Read the documentation, and fix that line of code, then run it. If you get an error, VIEW the EXECUTION TRANSCRIPT, and it will probably tell you what line of code failed. If you still need help, always post what line of code failed.

    Also, there is no addToFolder() method of the new DriveApp Folder class.

    Folder Class

    You will need to change that to:

    folder.addFile(moveFile);
    

    addFile() Method

    0 讨论(0)
  • 2020-11-27 22:39

    There is no method for "adding to folder" for a File object in DriveApp. Instead, you need to get the folder (which it looks like you store in var folder), and then use the Folder object to add a file. It would look something like this:

    var folder = DriveApp.getFolderById(string id);
    var movefile = DocsList.createFile(pdf);
    folder.addFile(moveFile);
    

    You can use Folder objects to add a file to the Folder, but DriveApp has no method for File objects to add themselves to a folder.

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