Share Google Drive Files with Apps Script

I wrote a little Google Script that saves Gmail attachments to Drive and shares the saved files with users based on rules defined in the message body itself. Here’s a sample rule:

# view@example.com, v #
# comment2@example.com, comment@example.com, c#
# edit1@example.com, edit2@example.com, e #

In the above case, the file(s) would be shared with all these users but the sharing permissions would be different. The user view@example can only view (v) the document, the users edit@example have editing permissions while others have commenting permissions.