Class FileIterator
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Trình duyệttệp
Một trình lặp cho phép tập lệnh lặp lại trên một tập hợp tệp có thể lớn. Bạn có thể truy cập trình duyệt tệp từ DriveApp
hoặc Folder
.
// Log the name of every file in the user's Drive.
const files = DriveApp.getFiles();
while (files.hasNext()) {
const file = files.next();
Logger.log(file.getName());
}
Phương thức
Phương thức | Loại dữ liệu trả về | Mô tả ngắn |
getContinuationToken() | String | Lấy một mã thông báo có thể dùng để tiếp tục vòng lặp này sau. |
hasNext() | Boolean | Xác định xem việc gọi next() có trả về một mục hay không. |
next() | File | Lấy mục tiếp theo trong bộ sưu tập tệp hoặc thư mục. |
Tài liệu chi tiết
getContinuationToken()
Lấy một mã thông báo có thể dùng để tiếp tục vòng lặp này sau. Phương thức này hữu ích nếu việc xử lý một trình lặp trong một lần thực thi sẽ vượt quá thời gian thực thi tối đa.
Mã thông báo tiếp tục thường có hiệu lực trong một tuần.
Cầu thủ trả bóng
String
– mã thông báo tiếp tục có thể dùng để tiếp tục vòng lặp này với các mục còn lại trong trình lặp khi mã thông báo được tạo
hasNext()
Xác định xem việc gọi next()
có trả về một mục hay không.
Cầu thủ trả bóng
Boolean
– true
nếu next()
trả về một mục; false
nếu không
next()
Lấy mục tiếp theo trong bộ sưu tập tệp hoặc thư mục. Gửi một trường hợp ngoại lệ nếu không còn mục nào.
Cầu thủ trả bóng
File
– mục tiếp theo trong bộ sưu tập
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-07-26 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-07-26 UTC."],[[["\u003cp\u003eFileIterator allows scripts to iterate over a large collection of files within Google Drive, accessible via \u003ccode\u003eDriveApp\u003c/code\u003e or a \u003ccode\u003eFolder\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eIt provides methods to check for more files (\u003ccode\u003ehasNext()\u003c/code\u003e), retrieve the next file (\u003ccode\u003enext()\u003c/code\u003e), and manage long iterations with continuation tokens (\u003ccode\u003egetContinuationToken()\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003egetContinuationToken()\u003c/code\u003e helps to resume iterations that might exceed execution time limits, with tokens typically valid for a week.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003ehasNext()\u003c/code\u003e returns \u003ccode\u003etrue\u003c/code\u003e if there are more files to iterate, allowing scripts to control the loop.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003enext()\u003c/code\u003e retrieves the subsequent file in the collection, throwing an exception if none remain.\u003c/p\u003e\n"]]],[],null,["# Class FileIterator\n\nFileIterator\n\nAn iterator that allows scripts to iterate over a potentially large collection of files. File\niterators can be accessed from [DriveApp](/apps-script/reference/drive/drive-app) or a [Folder](/apps-script/reference/drive/folder).\n\n```javascript\n// Log the name of every file in the user's Drive.\nconst files = DriveApp.getFiles();\nwhile (files.hasNext()) {\n const file = files.next();\n Logger.log(file.getName());\n}\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|---------------------------------------------------|-------------------------------------------|-------------------------------------------------------------------------|\n| [getContinuationToken()](#getContinuationToken()) | `String` | Gets a token that can be used to resume this iteration at a later time. |\n| [hasNext()](#hasNext()) | `Boolean` | Determines whether calling [next()](#next()) will return an item. |\n| [next()](#next()) | [File](/apps-script/reference/drive/file) | Gets the next item in the collection of files or folders. |\n\nDetailed documentation\n----------------------\n\n### `get``Continuation``Token()`\n\nGets a token that can be used to resume this iteration at a later time. This method is useful\nif processing an iterator in one execution would exceed the maximum execution time.\nContinuation tokens are generally valid for one week.\n\n#### Return\n\n\n`String` --- a continuation token that can be used to resume this iteration with the items that\nremained in the iterator when the token was generated\n\n*** ** * ** ***\n\n### `has``Next()`\n\nDetermines whether calling [next()](#next()) will return an item.\n\n#### Return\n\n\n`Boolean` --- `true` if [next()](#next()) will return an item; `false` if not\n\n*** ** * ** ***\n\n### `next()`\n\nGets the next item in the collection of files or folders. Throws an exception if no items\nremain.\n\n#### Return\n\n\n[File](/apps-script/reference/drive/file) --- the next item in the collection"]]