0

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.

Ong K.S
  • 229
  • 1
  • 4
  • 15
  • In your situation, how do you execute the function of `check_files()`? – Tanaike Jun 13 '21 at 03:00
  • @Tanaike execute `locate_file` and I expect variable `folder` is stored with something, then execute `check_files`. – Ong K.S Jun 13 '21 at 03:04
  • Thank you for replying. From your replying, at Google Apps Script, after `locate_file` was run with script editor, when `check_files` is run with script editor, the values of `let folder` and `let file` are cleared. By this, such error occurs. And also, even when the functions `locate_file()` and `check_files()` are run in order in one running, the folderIterator has already been finished. By this, the while loop in the function `check_files()` is not used. [Ref](https://developers.google.com/apps-script/reference/drive/folder-iterator) Please be careful this. – Tanaike Jun 13 '21 at 03:08
  • @Tanaike can you please show me the correct way to make it work? – Ong K.S Jun 13 '21 at 03:10
  • Thank you for replying. I proposed one direction for achieving your goal. Could you please confirm it? If that was not the direction you expect, I apologize. – Tanaike Jun 13 '21 at 03:24
  • @Tanaike meaning of "propse one direction"? I don't get it – Ong K.S Jun 13 '21 at 06:28
  • Thank you for replying. I deeply apologize for my poor English skill. I'm not sure whether my proposed answer (a direction) which uses PropertiesService is what you want. If the method for using PropertiesService is not what you want, I apologize. – Tanaike Jun 13 '21 at 06:40
  • hi @Tanaike it is ok. I check that PropertyService is one of the ways to do it. However, I'm looking for complete code for solution. – Ong K.S Jun 13 '21 at 06:42
  • Thank you for replying. I have to apologize for my poor English skill. About `However, I'm looking for complete code for solution.`, in my proposed script, your `check_files()` is modified using PropertiesService. In that case, by storing the folder ID, you can run `check_files()`. So, I cannot understand about your replying. So can I ask you about the detail of your goal? By this, I would like to try to understand it. – Tanaike Jun 13 '21 at 06:44
  • Is there anything that I can do for your question? If you can cooperate to resolve your issue, I'm glad. I would like to think of about the solution. – Tanaike Jun 17 '21 at 02:30
  • hi @Tanaike sorry, i have been busy recently. Can we keep in touch personally? please reach me via Linkedin https://www.linkedin.com/in/ong-ks/ – Ong K.S Jun 17 '21 at 03:36

1 Answers1

0

If you want to use the folder as the global variable, in this case, I would like to propose to use the folder ID as the global variable. Because folder of folder = DriveApp.getFolderById("###").getFiles() is the folder iterator. For this, I think that PropertiesService can be used. When this is reflected to Google Apps Script, it becomes as follows.

Sample script:

// At first, please run this function. By this, the value of "folderId" is stored to PropertiesService.
function setGlobalVariable() {
  const folderId = "162jksCkY98VeQAgnHeAzmnCVbGRKg9rd";
  PropertiesService.getScriptProperties().setProperty("folderId", folderId);
}

// As the next step, please run this function. By this, "folderId" is retrieved from PropertiesService and it is used.
function check_files() {
  const folderId = PropertiesService.getScriptProperties().getProperty("folderId");
  if (folderId) {
    const folder = DriveApp.getFolderById(folderId).getFiles();
    while (folder.hasNext())  {
      file = folder.next().getName();
      console.log(file);
    }
  } else {
    throw new Error("No folder ID.");
  }
}
  • At first, please run setGlobalVariable(). By this, the value of "folderId" is stored to PropertiesService. And, as the next step, please run check_files(). By this, "folderId" is retrieved from PropertiesService and it is used.

  • If you want to change the folder ID, please modify setGlobalVariable() and run again.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165