A folder contains methods to create, find, and access files inside of itself.
// This example gets a folder named 'kittens' and logs the files it contains.
var folder = DocsList.getFolder('kittens');
var files = folder.getFiles();
for (var i in files) {
Logger.log(files[i].getName());
}
Methods
Method | Return type | Brief description |
---|---|---|
addEditor(emailAddress) | Folder | Adds a new editor to the Folder . |
addEditor(user) | Folder | Adds a new editor to the Folder . |
addEditors(emailAddresses) | Folder | Adds new editors to the Folder . |
addToFolder(parent) | void | Adds the item to the given folder. |
addViewer(emailAddress) | Folder | Adds a new viewer to the Folder . |
addViewer(user) | Folder | Adds a new viewer to the Folder . |
addViewers(emailAddresses) | Folder | Adds new viewers to the Folder . |
createFile(blob) | File | Creates a file using the data stored in this blob. |
createFile(name, contents) | File | Creates a file with the given name and contents in the current folder. |
createFile(name, contents, mimeType) | File | Creates a file with the given name and contents in the current folder with the given MIME type. |
createFolder(name) | Folder | Creates a sub-folder with the given name in the current folder. |
find(query) | File[] | Returns an array of all the files in the container that contain the given string. |
find(query, start, max) | File[] | Returns a subrange of the files that result from the given query. |
findForPaging(query, number) | FilesResult | Returns the next number files maching the search query and a paging token. |
findForPaging(query, number, token) | FilesResult | Returns the next number files maching the search query,
picking up from where the token from the previous lookup left off. |
getDateCreated() | Date | Gets the date that this item was created. |
getDescription() | String | Returns the description of the item or null if a description doesn't exist. |
getEditors() | User[] | Gets the editors of this Folder . |
getFiles() | File[] | Returns all the files in the container (up to a maximum of
DocsList.DEFAULT_RESULT_SIZE ). |
getFiles(start, max) | File[] | Returns a subrange of the files in this container. |
getFilesByType(type) | File[] | Returns all files of a given type. |
getFilesByType(type, start, max) | File[] | Returns a subrange of the files of the given type in this container. |
getFilesByTypeForPaging(type, number) | FilesResult | Returns the next number files of the given type in this container and a
paging token. |
getFilesByTypeForPaging(type, number, token) | FilesResult | Returns the next number files of the given type in this container,
picking up from where the token from the previous lookup left off. |
getFilesForPaging(number) | FilesResult | Returns the next number files in this container and a paging token. |
getFilesForPaging(number, token) | FilesResult | Returns the next number files in this container, picking up
from where the token from the previous lookup left off. |
getFolders() | Folder[] | Returns all the folders in this container (up to a maximum of
DocsList.DEFAULT_RESULT_SIZE ). |
getFolders(start, max) | Folder[] | Returns a subrange of the folders in this container. |
getFoldersForPaging(number) | FoldersResult | Returns the first number of folders in this container and a paging token. |
getFoldersForPaging(number, token) | FoldersResult | Returns the next number folders in this container, picking up
from where the token from the previous lookup left off. |
getId() | String | Returns the document ID associated with the item. |
getLastUpdated() | Date | Gets the date that this item was last updated. |
getName() | String | Returns the name of the item. |
getOwner() | User | Gets the owner of the item. |
getParents() | Folder[] | Returns the parent folders. |
getSize() | Integer | Returns the amount of disk space used by the item. |
getUrl() | String | Returns a URL to access the particular item. |
getViewers() | User[] | Gets the viewers of this Folder . |
isStarred() | Boolean | Gets whether the item is starred. |
isTrashed() | Boolean | Checks whether the item is trashed. |
removeEditor(emailAddress) | Folder | Removes an editor from the Folder . |
removeEditor(user) | Folder | Removes an editor from the Folder . |
removeFromFolder(parent) | void | Removes this object from the given folder. |
removeViewer(emailAddress) | Folder | Removes a viewer from the Folder . |
removeViewer(user) | Folder | Removes a viewer from the Folder . |
rename(newName) | void | Rename the item. |
setDescription(description) | void | Update's the item's description. |
setStarred(starred) | void | Sets the item's starred status in drive. |
setTrashed(trash) | void | Sets the trashed status of an item but does not permanently delete it. |
Detailed documentation
addEditor(emailAddress)
Adds a new editor to the Folder
.
Parameters
Name | Type | Description |
---|---|---|
emailAddress | String | the email address of the user to add |
Return
addEditor(user)
Adds a new editor to the Folder
.
Parameters
Name | Type | Description |
---|---|---|
user | User | the user to add |
Return
addEditors(emailAddresses)
Adds new editors to the Folder
.
Parameters
Name | Type | Description |
---|---|---|
emailAddresses | String[] | the email addresses of the users to add |
Return
addToFolder(parent)
Adds the item to the given folder.
// This example creates a new file and adds it to a folder
// Note: This can also be used on a folder to add it to another folder
var file = DocsList.createFile('my test file', 'test file contents');
var folder = DocsList.createFolder('My Test Folder');
file.addToFolder(folder);
Logger.log(file.getParents()[0].getName()); // logs 'My Test Folder'
Parameters
Name | Type | Description |
---|---|---|
parent | Folder | the folder to add the item to |
addViewer(emailAddress)
Adds a new viewer to the Folder
.
Parameters
Name | Type | Description |
---|---|---|
emailAddress | String | the email address of the user to add |
Return
addViewer(user)
Adds a new viewer to the Folder
.
Parameters
Name | Type | Description |
---|---|---|
user | User | the user to add |
Return
addViewers(emailAddresses)
Adds new viewers to the Folder
.
Parameters
Name | Type | Description |
---|---|---|
emailAddresses | String[] | the email addresses of the users to add |
Return
createFile(blob)
Creates a file using the data stored in this blob.
// Fetches the Google logo and saves it to a folder
var url = 'https://www.google.com/images/srpr/logo3w.png';
var imageBlob = UrlFetchApp.fetch(url).getBlob();
var folder = DocsList.createFolder('my folder');
folder.createFile(imageBlob);
Parameters
Name | Type | Description |
---|---|---|
blob | BlobSource | the blob to create the file from |
Return
File
— the newly created file
createFile(name, contents)
Creates a file with the given name and contents in the current folder. If the file name does not contain an extension, it defaults to plain text.
// Creates a file called 'new kitten' in the 'kittens' folder
var folder = DocsList.getFolder('kittens');
folder.createFile('new kitten', 'meow');
Parameters
Name | Type | Description |
---|---|---|
name | String | the name of the new file |
contents | String | the contents of the new file |
Return
File
— the newly created file
createFile(name, contents, mimeType)
Creates a file with the given name and contents in the current folder with the given MIME type. If the file name does not contain an extension, it defaults to plain text.
// Creates a simple XML file in the 'kittens' folder
var folder = DocsList.getFolder('kittens');
var contents = '<kitten><name>Tiny</name><age>1</age></kitten>';
folder.createFile('kittens.xml', contents, 'application/xml');
Parameters
Name | Type | Description |
---|---|---|
name | String | the name of the new file |
contents | String | the new file contents |
mimeType | String | the mime-type of the new file |
Return
File
— the newly created file
createFolder(name)
Creates a sub-folder with the given name in the current folder.
// Creates a subfolder in a folder called 'cats'
var folder = DocsList.createFolder('cats');
folder.createFolder('cool cats');
Parameters
Name | Type | Description |
---|---|---|
name | String | the name of the new folder |
Return
Folder
— the newly created folder
find(query)
Returns an array of all the files in the container that contain the given string.
// Logs the name of all the files resulting from a search for 'cool cats'
var folder = DocsList.getFolder('cats');
var coolCats = folder.find('cool cats');
for (var i in coolCats) {
Logger.log(coolCats[i].getName());
}
Parameters
Name | Type | Description |
---|---|---|
query | String | the search string |
Return
File[]
— the matching files
find(query, start, max)
Returns a subrange of the files that result from the given query. If there are not enough results to fill the range, a partial range will be returned.
// Logs the name of the first 3 files resulting from a search for 'cool cats'
var folder = DocsList.getFolder('cats');
var coolCats = folder.find('cool cats', 0, 3);
for (var i in coolCats) {
Logger.log(coolCats[i].getName());
}
Parameters
Name | Type | Description |
---|---|---|
query | String | the search string |
start | Integer | the 0-indexed position to start returning results from |
max | Integer | the maximum number of objects to return |
Return
File[]
— the requested number of matching files
findForPaging(query, number)
Returns the next number
files maching the search query and a paging token.
If the requested number of files do not exist, it returns the available
files.
// Logs the name of the first 3 files resulting from a search for 'cool cats'
var folder = DocsList.getFolder('cats');
var filesResult = folder.findForPaging('cool cats', 3);
var files = FilesResult.getFiles();
for (var i in files) {
Logger.log(files[i].getName());
}
Parameters
Name | Type | Description |
---|---|---|
query | String | the search string |
number | Integer |
Return
FilesResult
— the requested number of matching files and a paging token
findForPaging(query, number, token)
Returns the next number
files maching the search query,
picking up from where the token
from the previous lookup left off.
If the requested number of files do not exist, it returns the available
files.
// Logs the first 6 file names resulting from a search but does it 3 at
// a time.
var folder = DocsList.getFolder('cats');
var filesResult = folder.findForPaging('cool cats', 3);
var files = filesResult.getFiles();
var token = filesResult.getToken();
// log the first 3 file names
for (var i in files) {
Logger.log(files[i].getName());
}
filesResult = folder.findForPaging('cool cats', 3, token); // pass in previous token
files = filesResult.getFiles();
// log the next 3 file names
for (var i in files) {
Logger.log(files[i].getName());
}
Parameters
Name | Type | Description |
---|---|---|
query | String | the search string |
number | Integer | |
token | Token | the token from the previous lookup |
Return
FilesResult
— the requested number of matching files and a paging token
getDateCreated()
Gets the date that this item was created.
// This example logs the date the first file was created.
// Note: This can also be used on a folder
var file = DocsList.getAllFiles[0];
Logger.log(file.getDateCreated());
Return
Date
— the date this item was created
getDescription()
Returns the description of the item or null if a description doesn't exist.
// Gets the first document's description.
// Note: This can also be used on a folder
var doc = DocsList.getFilesByType(DocsList.FileType.DOCUMENT)[0];
Logger.log(doc.getDescription());
Return
String
— the item's description
getFiles()
Returns all the files in the container (up to a maximum of
DocsList.DEFAULT_RESULT_SIZE
).
// Logs the number of files in the 'kittens' folder
var folder = DocsList.getFolder('kittens');
var files = folder.getFiles();
Logger.log(files.length);
Return
File[]
— the requested files
getFiles(start, max)
Returns a subrange of the files in this container. If there are not enough results to fill the range, a partial range will be returned.
// Logs the first 3 file names (or less if there are fewer
// than 3 files) in the 'kittens' folder
var folder = DocsList.getFolder('kittens');
var files = folder.getFiles(0, 3);
for (var i in files) {
Logger.log(files[i].getName());
}
Parameters
Name | Type | Description |
---|---|---|
start | Integer | the 0-indexed position to start returning results from |
max | Integer | the maximum number of files to return |
Return
File[]
— the requested files
getFilesByType(type)
Returns all files of a given type.
See FileType
for the list of possible file types.
// Logs the names of the first document in the 'kittens' folder
var folder = DocsList.getFolder('kittens');
var file = folder.getFilesByType(DocsList.FileType.DOCUMENT)[0];
Logger.log(file.getName());
Parameters
Name | Type | Description |
---|---|---|
type | FileType | the type of files to get |
Return
File[]
— the requested files
See also
getFilesByType(type, start, max)
Returns a subrange of the files of the given type in this container.
If there are not enough results to fill the range, a partial range will be
returned. See FileType
for the list of possible file types.
// Logs the names of the first three documents in the 'kittens' folder
var folder = DocsList.getFolder('kittens');
var files = folder.getFilesByType(DocsList.FileType.DOCUMENT, 0, 3);
for (var i in files) {
Logger.log(files[i].getName());
}
Parameters
Name | Type | Description |
---|---|---|
type | FileType | |
start | Integer | the 0-indexed position to start returning results from |
max | Integer | the maximum number of files to return |
Return
File[]
— the requested files
See also
getFilesByTypeForPaging(type, number)
Returns the next number
files of the given type in this container and a
paging token.
If the requested number of files do not exist, it returns the available
files.
// Logs the first 3 document names (or less if there are fewer
// than 3 documents) from the 'kittens' folder
var folder = DocsList.getFolder('kittens');
var filesResult = folder.getFilesForPaging(DocsList.FileType.DOCUMENT, 3);
var files = FilesResult.getFiles();
for (var i in files) {
Logger.log(files[i].getName());
}
Parameters
Name | Type | Description |
---|---|---|
type | FileType | the type of files to get |
number | Integer | the number of files to get |
Return
FilesResult
— the requested files and a paging token
See also
getFilesByTypeForPaging(type, number, token)
Returns the next number
files of the given type in this container,
picking up from where the token
from the previous lookup left off.
If the requested number of files do not exist, it returns the available
files.
// Logs the first 6 document names in the 'kittens' folder but does it 3 at
// a time.
var folder = DocsList.getFolder('kittens');
var filesResult = folder.getFilesForPaging(DocsList.FileType.DOCUMENT, 3);
var files = filesResult.getFiles();
var token = filesResult.getToken();
// log the first 3 document names
for (var i in files) {
Logger.log(files[i].getName());
}
// pass in previous token
filesResult = folder.getAllFilesForPaging(DocsList.FileType.DOCUMENT, 3, token);
files = filesResult.getFiles();
// log the next 3 document names
for (var i in files) {
Logger.log(files[i].getName());
}
Parameters
Name | Type | Description |
---|---|---|
type | FileType | the type of files to return |
number | Integer | the number of files to return |
token | Token | the token from the previous lookup |
Return
FilesResult
— the requested files and paging token
getFilesForPaging(number)
Returns the next number
files in this container and a paging token.
If the requested number of files do not exist, it returns the available
files.
// Logs the first 3 file names (or less if there are fewer
// than 3 files) from the 'kittens' folder
var folder = DocsList.getFolder('kittens');
var filesResult = folder.getFilesForPaging(3);
var files = filesResult.getFiles();
for (var i in files) {
Logger.log(files[i].getName());
}
Parameters
Name | Type | Description |
---|---|---|
number | Integer | the number of files to return |
Return
FilesResult
— the requested files and paging token
getFilesForPaging(number, token)
Returns the next number
files in this container, picking up
from where the token
from the previous lookup left off.
If the requested number of files do not exist, it returns the available
files.
// Logs the first 6 file names in the 'kittens' folder but does it 3 at
// a time.
var folder = DocsList.getFolder('kittens');
var filesResult = folder.getFilesForPaging(3);
var files = filesResult.getFiles();
var token = filesResult.getToken();
// log the first 3 file names
for (var i in files) {
Logger.log(files[i].getName());
}
filesResult = folder.getAllFilesForPaging(3, token); // pass in previous token
files = filesResult.getFiles();
// log the next 3 file names
for (var i in files) {
Logger.log(files[i].getName());
}
Parameters
Name | Type | Description |
---|---|---|
number | Integer | the number of files to return |
token | Token | the paging token from the previous lookup |
Return
FilesResult
— the requested files and paging token
getFolders()
Returns all the folders in this container (up to a maximum of
DocsList.DEFAULT_RESULT_SIZE
).
// Logs the number of folders in the 'kittens' folder
var folder = DocsList.getFolder('kittens');
var subFolders = folder.getFolders();
Logger.log(subFolders.length);
Return
Folder[]
— the requested folders
getFolders(start, max)
Returns a subrange of the folders in this container. If there are not enough results to fill the range, a partial range will be returned.
// Logs the first 3 folder names (or less if there are fewer
// than 3 folders) in the 'kittens' folder
var folder = DocsList.getFolder('kittens');
var subFolders = folder.getFolders(0, 3);
for (var i in subFolders) {
Logger.log(subFolders[i].getName());
}
Parameters
Name | Type | Description |
---|---|---|
start | Integer | the 0-indexed position to start returning results from |
max | Integer | the maximum number of folders to return |
Return
Folder[]
— the requested folders
getFoldersForPaging(number)
Returns the first number
of folders in this container and a paging token.
If the requested number of folders do not exist, it returns the available
folders.
// Logs the first 3 folder names (or less if there are fewer
// than 3 folders) from the 'kittens' folder
var folder = DocsList.getFolder('kittens');
var subFoldersResult = folder.getFoldersForPaging(3);
var subFolders = subFoldersResult.getFolders();
for (var i in subFolders) {
Logger.log(subFolders[i].getName());
}
Parameters
Name | Type | Description |
---|---|---|
number | Integer | the number of folders to return |
Return
FoldersResult
— the requested folders and a paging token
getFoldersForPaging(number, token)
Returns the next number
folders in this container, picking up
from where the token
from the previous lookup left off.
If the requested number of folders do not exist, it returns the available
folders.
// Logs the first 6 folder names in the 'kittens' folder but does it 3 at
// a time.
var folder = DocsList.getFolder('kittens');
var foldersResult = folder.getFoldersForPaging(3);
var subFolders = foldersResult.getFolders();
var token = foldersResult.getToken();
// log the first 3 sub folder names
for (var i in subFolders) {
Logger.log(subFolders[i].getName());
}
folderResult = folder.getAllFoldersForPaging(3, token); // pass in previous token
subFolders = foldersResult.getFolders();
// log the next 3 sub folder names
for (var i in subFolders) {
Logger.log(subFolders[i].getName());
}
Parameters
Name | Type | Description |
---|---|---|
number | Integer | the number of folders to return |
token | Token | the token from the previous lookup |
Return
FoldersResult
— the requested folders and paging token
getId()
Returns the document ID associated with the item.
// Logs the first file's document ID.
// Note: This can also be used on a folder.
var file = DocsList.getAllFiles()[0];
Logger.log(file.getId());
Return
String
— the document ID of the item
getLastUpdated()
Gets the date that this item was last updated.
// This example logs the date the first file was last updated.
// Note: This can also be used on a folder
var file = DocsList.getAllFiles[0];
Logger.log(file.getLastUpdated());
Return
Date
— the date this item was last updated
getName()
Returns the name of the item.
// Logs the first file's name.
// Note: This can also be used on a folder.
var file = DocsList.getAllFiles()[0];
Logger.log(file.getName());
Return
String
— the name of the item
getOwner()
Gets the owner of the item.
// This example creates a file and logs the owner's email address
// Note: This can also be used on a folder
var file = DocsList.createFile('my test file', 'test file contents');
Logger.log(file.getOwner().getEmail()); // logs your own email address
Return
User
— the owner of the item
getParents()
Returns the parent folders. If called on a top level item, it returns the root folder. An item can be placed in multiple folders in drive. See the drive documentation.
// This example logs the name of the parent folder of the first doc.
// Note: This can also be used on a folder to get its parent
var file = DocsList.getFilesByType(DocsList.FileType.DOCUMENT)[0];
Logger.log(file.getParents()[0].getName());
Return
Folder[]
— the parent folders containing the item
getSize()
Returns the amount of disk space used by the item. Note that it returns zero if called on an item that does not consume disk space, such as a Google document type file.
// This example logs the first file's size in bytes
// Note: This can also be used on a folder to get the size of its contents
var file = DocsList.getAllFiles[0];
Logger.log(file.getSize());
Return
Integer
— the item's size in bytes, or zero if the item is a Google document
type or does not consume space
getUrl()
Returns a URL to access the particular item.
// This example logs the first file's URL
// Note: This can also be used on a folder
var file = DocsList.getAllFiles[0];
Logger.log(file.getUrl());
Return
String
— the item's URL
isStarred()
Gets whether the item is starred.
// This example creates a new file, stars it, and tests whether it is starred.
// Note: This can also be used on a folder
var file = DocsList.createFile('my test file', 'test file contents');
file.setStarred(true); // the file should be starred in drive
Logger.log(file.isStarred()); // logs true
Return
Boolean
— true if the item is starred
isTrashed()
Checks whether the item is trashed.
// This example creates a new file and then sets it trashed.
// Note: This can also be used on a folder
var file = DocsList.createFile('my test file', 'test file contents');
file.setTrashed(true); // the file should be trashed in drive
Logger.log(file.isTrashed()); // logs true
Return
Boolean
— true if the item is trashed
removeEditor(emailAddress)
Removes an editor from the Folder
.
Parameters
Name | Type | Description |
---|---|---|
emailAddress | String | the email address of the user to remove |
Return
removeEditor(user)
Removes an editor from the Folder
.
Parameters
Name | Type | Description |
---|---|---|
user | User | the user to remove |
Return
removeFromFolder(parent)
Removes this object from the given folder. If the item does not belong to the folder, this does nothing.
// This example creates a new file, adds it to a folder, and then removes
// it from the folder.
// Note: This can also be used on a folder to remove it from another folder
var file = DocsList.createFile('my test file', 'test file contents');
var folder = DocsList.createFolder('My Test Folder');
file.addToFolder(folder);
Logger.log(folder.getFiles().length); // logs 1
file.removeFromFolder(folder);
Logger.log(folder.getFiles().length); // logs 0
Parameters
Name | Type | Description |
---|---|---|
parent | Folder | the folder to remove the item from |
removeViewer(emailAddress)
Removes a viewer from the Folder
.
Parameters
Name | Type | Description |
---|---|---|
emailAddress | String | the email address of the user to remove |
Return
removeViewer(user)
Removes a viewer from the Folder
.
Parameters
Name | Type | Description |
---|---|---|
user | User | the user to remove |
Return
rename(newName)
Rename the item.
// Creates a file called 'Old File' and renames it 'New File'.
// Note: This can also be used on a folder
var oldFile = DocsList.createFile('Old File', '');
oldFile.rename('New File');
Logger.log(oldFile.getName()); // This should log "New File"
Parameters
Name | Type | Description |
---|---|---|
newName | String | the new name of the item |
setDescription(description)
Update's the item's description.
// This example sets the first document's description
// Note: This can also be used on a folder
var doc = DocsList.getFilesByType(DocsList.FileType.DOCUMENT)[0];
doc.setDescription('My First Doc');
Logger.log(doc.getDescription()); // logs 'My First Doc'
Parameters
Name | Type | Description |
---|---|---|
description | String | the item's description |
setStarred(starred)
Sets the item's starred status in drive.
// This example creates a new file and stars it.
// Note: This can also be used on a folder
var file = DocsList.createFile('my test file', 'test file contents');
file.setStarred(true); // the file should be starred in drive
Parameters
Name | Type | Description |
---|---|---|
starred | Boolean | stars the item if true; unstars it if false |
setTrashed(trash)
Sets the trashed status of an item but does not permanently delete it.
// This example creates a new file and then sets it trashed.
// Note: This can also be used on a folder
var file = DocsList.createFile('my test file', 'test file contents');
file.setTrashed(true); // the file should be trashed in drive
Logger.log(file.isTrashed()); // logs true
Parameters
Name | Type | Description |
---|---|---|
trash | Boolean | trashes the item if true; untrashes it if false |