I'm trying to do an automation by using the Google Apps Script, and I'm having some trouble. That's why I need it.
1 - The script must identify the subject of an email.
2 - If the subject has a specific word, the script downloads the attachment.
3 - But before it downloads the attachment, the script should read the subject of the email and look in Google Drive if there is a folder with the same name of the subject.
4 - If the folder already exists, the script just downloads the file to this folder in Google Drive.
5 - Else, the script creates a folder with the same name of the email subject and then downloads the attachment to this folder.
If you already have an attempt at this script, I'd recommend sharing it when posting a question and also pointing out the issues you are running into.
In any case, I'll share some documentation and ideas to help you achieve this.
Start by listing the messages you will be using for your automation. I'd recommend having a look at the Gmail API method users.messages.list. The response of this method should contain an array called messages which you can loop through for the next requirements.
From the list of messages returned, you can find the subject of a message by accessing the attribute message.payload.headers. headers is a "name/value" type of array, if you loop through its contents, one of the "names" should be "Subject". Now you can store the subject on a variable to work with it and determine if it meets your criteria. To download a message attachment, you can use the Gmail API method users.messages.attachments.get.
With the full subject retrieved in 2, you can search on a specific folder in Drive to see if a sub-folder with that name exists. To do that I'd recommend having a look at the Drive API method files.list and use the parameter q to enter the search criteria. Example, to search for a folder named SUBJECT_NAME_OF_FOLDER, located inside another folder with id PARENT_FOLDER_DRIVE_ID, my q parameter value would look similar to 'PARENT_FOLDER_DRIVE_ID' in parents and mimeType = 'application/vnd.google-apps.folder' and name = 'SUBJECT_NAME_OF_FOLDER'.
If a folder with your criteria is returned from 3, then you can retrieve that folder ID from the response array files[n].id. With this ID, you can call Drive API files.create, setting the mimeType according to the message attachment mimeType and the parent parameter will be the ID you found from the search in 3.
If your search hasn't returned results, then most likely the folder with that specific name (at least in the location specified) does not exist, then you can create a new folder by calling Drive API files.create making sure to set the mimeType parameter as application/vnd.google-apps.folder. After creating the folder you can store the newly created ID and repeat 4 using this ID.
Hope it helps. Cheers!
Related
I would like to use Apps Script to save anything sent to the Web App URL as a text file on Google Drive. I believe that this should be done with a basic doGet and doPost setup.
If I could get the file saved with a unique filename, that'd be great, but it's not important. I thought there would be a tutorial somewhere about this, but I can't seem to find it at all.
A basic workflow of what you need to do:
Create a WebApp with a doGet() function
Retrieve the data your application has sent to your WebApp url with e.parameter or e.parameters - depending on either it's a single string or an array, your retrieved data should be assigned to certain key
If the key is called "test" and your data is passed as https://script.google.com/a/XXX/macros/s/XXX/exec?test=Hello, you should query for myString = e.parameter.name
Create a text file on your drive with DriveApp.createFile(name, content, mimeType) - specifying a name of your choice, passing the retrieved string as content and chosing mimeType PLAIN_TEXT
I have a script that takes rows from a spreadsheet, and creates individual spreadsheet files for each row. Then the script publishes them to the web.
What I'm not able to get is the id that will allow me to get an url for a web version of it.
A regular spreadsheet id looks like this:
1N8h00iN2L7aaUbI9TYRWioaZHjHNSc_vNucCbVADj1o
And a published to the web id looks like this:
2PACX-1vTPSkv4Rz7CqwabTTfxa3xcVF-Gzxu7IboLyLkfoShP2d3qdX8o0qdvk5d_nWZD2rq43E2LXmVAm5HI
(longer, and it starts with 2PACX-)
Anybody know how to?
Thanks in advance.
You want to retrieve URL from the web published spreadsheet using file ID. If my understanding is correct, how about this answer? Unfortunately, "publishedLink" cannot be retrieved by Drive API v3. "publishedLink" got to not be able to be used from Drive API v3. Furthermore now, Drive API v2 also cannot be used anymore. So it is required to create the link manually.
When the spreadsheet is published to Web, you retrieve URL like as follows.
https://docs.google.com/spreadsheets/d/e/2PACX-1vTPSkv4Rz7CqwabTTfxa3xcVF-Gzxu7IboLyLkfoShP2d3qdX8o0qdvk5d_nWZD2rq43E2LXmVAm5HI/pubhtml
When you want to use the URL using file ID, please use the following URL. You can use both URLs.
Pattern 1:
https://docs.google.com/spreadsheet/pub?key=### fileId ###
Pattern 2:
https://docs.google.com/spreadsheets/d/### fileId ###/pubhtml
If I misunderstand your question, I'm sorry.
So I hope the question makes sense. I am relatively new to GoogleAppScripts. I have a client that has some Scripts that I am trying to interoperate and modify. In order to do so I need to find each document that is associated by 'id'. But when I search for the id in the search bar of drive,I just get the script file that I am working on because the id number is in the text. I need to find the particular sheets doc that is associated with a particular ID, plain n simple. There has to be an easy way to do this aside from writing a function and outputting the result...
The search bar in Drive doesn't do search by ID, however if you know the ID you can hand-craft the file URL & access it that way — if you have permission to access the file in question. e.g. you could open a Drive file with a URL like https://docs.google.com/{file-type}/d/{file-ID}, where {file-type} is document, spreadsheet, etc, & {file-ID} is the ID you have in the code.
An alternative is to get the names of the files for which you have IDs, write the results to the log (i.e. Logger.log(DriveApp.getFileById("your_file_id").getName());) & then search for them by name in the Drive UI.
Is there a script/extension available to automatically sort Google Drive documents that are shared via email and place them into a specified folder created in Google Drive. For example if John D shares the Morning Meeting Notes with me via email then I could automatically have that file saved into John D's folder in Google Drive.
You can get an idea from this post by StackExchange.
So here is the user's needed:
Would it be possible to
Send an automatic received receipt exclusively to emails that send time-sheet type image or PDF attachments.
Label the Gmail with 1 or 2 separate labels like "time-sheets" and "downloaded"( read next part )
send the attachment to a specific google drive folder.
You have exactly same scenario.
Here are the answers:
For 1.
You'll need to turn on the "Canned Responses" Gmail Lab. Create a
canned response with your response text ("Received").
Then, adjust or create a filter:
to:myEmail#gmail.com has:attachment .pdf
That should pick up any PDF file that's sent to you. (You could get a
false match if someone sends some other sort of attachment and puts
the string ".pdf" somewhere in their message.)
If you want to match on more file types, you can do something like
.png OR .jpg OR .gif OR .tiff OR .jpeg OR .tiff
For the action, choose "Send a canned response" and, obviously, choose
the canned response you created earlier.
For the 2nd question, here is the response:
In order to label a message via filter with two separate labels,
you'll need to have two separate filters. Just copy the criteria for
one filter to the other. (If you're ambitious, you can export your
filters, edit the resulting XML, then re-import them.)
And for the last:
You can easily do that with an IFTTT recipe. The Gmail channel lets
you trigger on a Gmail search and then you can use that to save to
Google Drive. Here's a recipe that's similar to what you'd want
to do.
To get sample scripts, here is a documentation for drive and gmail and for further reading as well.
I regularly need to change file share permissions on large numbers of files on Google Drive. I would rather not have to do this manually on a file by file basis. I have no issues extracting the File IDs, but can't seem to find any information associated with setting permissions for specific users.
Suppose I have a file with ID - 1132asdfasdf5sdf564sdf
Suppose I have a user named John Smith whose email address is jsmith#email.com and this user is a Google user.
Here's a sample script:
function myFunction() {
// this grabs the ID of the source file
var sourcefile=DriveApp.getFileById("1132asdfasdf5sdf564sdf");
}
Question. Once I grab the file by ID, is it possible to set either edit or view permissions for this specific user?
I haven't tried this, but I would imagine you could use addviewer(email) or addeditor(email). There is also an option for multiple emails.
source: https://developers.google.com/apps-script/reference/drive/file#methods
You can try to use the Drive Service. This service allows scripts to create, find, and modify files and folders in Google Drive.
// Log the name of every file in the user's Drive.
var files = DriveApp.getFiles();
while (files.hasNext()) {
var file = files.next();
Logger.log(file.getName());
}
By using the enum permission, that represents the permissions granted to users who can access a file or folder, besides any individual users who have been explicitly given access. These properties can be accessed from DriveApp.Permission.
VIEW - Users who can access the file or folder are able only to view it or copy it. Passing this value to File.setSharing(accessType, permissionType) throws an exception if the type of file does not support it.
EDIT - Users who can access the file or folder are able to edit it. Unless File.setShareableByEditors(shareable) is set to false, users can also change the sharing settings. Passing this value to File.setSharing(accessType, permissionType) throws an exception if the type of file does not support it.
Try also to check this SO question for more information.
Are you replacing old users with new ones in the permission scheme? If you are, you need to invoke the File or Document class methods removeEditor(s) or removeViewer(s), and then follow up with addEditor(s) or addViewer(s).
The problem with using built in methods (as far as I can tell) is that it is difficult to stop a blizzard of email notifications going out to the users if you are doing bulk permission inserts. You can use the flexibility of the Drive API to specifically stop email notifications, but may experience with calling the Drive API is that it eats up a lot of time.