How to update DocsList to DriveApp in my code

≡放荡痞女 提交于 2019-11-26 20:58:12

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!