1

My google app script stop work after a certain times. the following code changes the file permission. in my folder there is like 10K files. when i run the script after changing 1~2K file sharing permission script stop work. is there anyway to solve the issue?

function myFunction() {
  var folderId = "ID";
  var files = DriveApp.getFolderById(folderId).getFiles();
  var result = [];
  while (files.hasNext()) {
      var file = files.next();
      file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
      var temp = {
        file_name: file.getName(),
        url: "http://drive.google.com/uc?export=view&id=" + file.getId(),
      };
      result.push(temp);
  };

}
1
  • 2
    You should split it because Folder work is pretty slow. You can't work with 10K file under 6 min max execution... Commented Nov 14, 2019 at 13:06

1 Answer 1

1

There are several workarounds, from upgrading your account to grant access to longer timeouts, you could also make the script work with a cache service and store the state of the iteration before the timeout, then run again the script with this value stored on the cache.

Although, all of this may be to much for what you are attempting to: permissions on Drive have inheritance. What does this mean? This means that if you change the permission settings on any folder you'll override the permissions of each file on that folder.

In summary, you really do not need to make any iteration thru all the files, change the permissions for that folder and you are done.

Check this docs in case of doubt.

A working code to change your folder permission and all its contained files:

  function myFunction() {
  var folder = DriveApp.getFolderById("YOUR_FOLDER_ID");
  folder.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
  }

This works, but due to the large number of files on your folder propagation may take some time, up to several hours, be patient and check it tomorrow.

Sign up to request clarification or add additional context in comments.

2 Comments

i changed folder permission, but it does not override the file sharing permission.
thanks for the code which works fine. however folder permission does not override if permission is changed manually for specific files. anyway i guess i will manually move group of files may be 500 files, then run script to avoid script timeout issue. thanks again for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.