eRic
eRic

Reputation: 15

Why Google Apps Script hangs on calling next() function

Today I can't run scripts connected to a specific Google Spreadsheet which contains .next() function calls, which I use to access other Spreadsheets by name. It shows Running script Cancel Dismiss message at the top of spreadsheet until exceeding maximum execution time. The strange thing is that I have similar files for every day with exactly same scripts, and the one for yesterday works fine. I created MWE below. So, when I run it I can get the alerts for the first 2 messages, but not the 3rd one.

function test_script_hanging() {
    var ui = SpreadsheetApp.getUi();
    ui.alert("before getting files by name");
    var getFilefile = DriveApp.getFilesByName("file");
    ui.alert("after getting files by name");
    var getID = getFilefile.next().getId();
    ui.alert("after getting Id");
}

In some situations I notice an error message saying something like "Authorization is needed to perform this action" on the line where the .next() function is being called. I tried to revoke authorization of the file and give authorization again, but that didn't help. I tried to give full access to a given script, but couldn't google the way to do it. Maybe I can add some more functions in my script to require full access during authorization, and maybe that will help to run my main function. My questions are:

  1. Why the script hangs?
  2. How to fix this?
  3. Can such kind of things (randomly and without any notice) happen in a free version of Google account?

Upvotes: 0

Views: 1474

Answers (1)

Cooper
Cooper

Reputation: 64032

This works for me:

  var fldr = DriveApp.getFolderById(folderID)
  var file = fldr.getFilesByName(filename);
  while(file.hasNext())
  {
    var fi = file.next();
    if(fi.getName() == filename)
    {
      var id=fi.getId();
    }
  }

Upvotes: 1

Related Questions