I'm new to Google Apps Script. I define locate_file
function to get name of each file within a folder New York Bike Share
with the help of DriveApp.getFolderById
.
let folder
let file
function locate_file() {
folder = DriveApp.getFolderById("162jksCkY98VeQAgnHeAzmnCVbGRKg9rd")
.getFiles()
while (folder.hasNext()) {
file = folder.next().getName()
console.log(file)
}
}
Codes above return result below in Execution Log:
10:47:07 AM Info 201906-citibike-tripdata.csv
10:47:07 AM Info 201905-citibike-tripdata.csv
10:47:07 AM Info 201904-citibike-tripdata.csv
10:47:07 AM Info 201903-citibike-tripdata.csv
10:47:07 AM Info 201902-citibike-tripdata.csv
10:47:07 AM Info 201901-citibike-tripdata.csv
10:47:08 AM Notice Execution completed
Since I already define global variable folder
, I plan to reuse the variable in another function. Function below is just for demo purpose, to print out file name stored in folder
variable. It failed.
function check_files() {
while (folder.hasNext()) {
file = folder.next().getName()
console.log(file)
}
}
10:53:44 AM Notice Execution started
10:53:45 AM Error TypeError: Cannot read property 'hasNext' of undefined
check_files @ Code.gs:16
Appreciate your help.