finding file in google drive based on id/interoperating previous developers code - google-apps-script

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.

Related

How to download Gmail attachment to Google Drive

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!

File search in csv in uipath

I'm trying to figure out a way to search in a google csv (list of names) if a name is present.
Is it possible in uipath?
According to my research, it may fall into the web recorder and the use the module to read csv?
Here is what I tried again:
In the main screen, I created a sequence.
Open a browser to a url like this
"https://docs.google.com/spreadsheets/d/xyzz/edit#gid=0"
Added a "Navigate to" the above URL again.
Then selected "Data Scraping".
Click on the first row of google sheet.
Then to create a pattern I clicked on next row. It couldn't find a pattern.
So I'm stuck on the final 6th position. I tried downloading and working on the csv as an alternative. But I would rather want it on the google sheet in the browser.
Am I going in the right direction?
Another way is:
to download Google GSuite set of activities from Google GSuite.
Place it under Packages folder in your UiPath installation folder and load it via Manage Packages option in Design Mode.
Use GSuite Application Scope (you need to fill your ClientID and ClientSecret for your account)
Use activity Find Files and Folders to search for the file that you want by name. You will get the file ID (in your case the SpreadSheetID) as output
Use a Read Range activity placing the output from step 3 to get the contents of spreadsheet as DataTable
Use your pattern to search through DataTable for the desired result.
Activities are shown as per below (Highlighted are the ones to use)-(Notification on the second Image is due to the fact that ClientID and ClientSecret are mandatory and i left them empty):
Hope you will find these information useful

Discrepancy between Drive Search and DriveApp.searchFiles(target)

Posting this here because I have found no note or solution elsewhere on the web.
I am currently experiencing a discrepancy between search results when using the drive search toolbar and the DriveApp.searchFiles() functionality of apps scripts as follows.
The drive search bar always returns complete results for a given query [looking at all text within a document and in the title], but DriveApp.searchFiles() does not. Certain files are missing/not returned.
Upon finding a given file that is returned from the drive search bar ONLY (one that was not being returned by the DriveApp search) and opening it, it then starts appearing/being returned by DriveApp.searchFiles().
This issue further seems to be a user-specific one. The script we are developing is used by multiple users and new users (ones that are added to an extant file system and then given our tool) experience this issue for a majority of files. After which, when they open a given 'missing' file, it begins appearing in the search results once more.
For reference, my code is as follows:
var targetParam = 'title contains "'+target+'" or fullText contains "'+target+'"';
var searchResults = DriveApp.searchFiles(targetParam);
In all instances of this issue, the drive search bar returns a complete list and opening a given file 'fixes' its issue. Given the scale of what we are trying to do it is not a possibility to have every user open every file.
For clarification, these files are within a large file system in either team drives or traditional G suite. Users are given access by being added to the highest level file, to the team drive, or by being added to a user group that has access to the file system already. All users are within our domain.
Is this a known discrepancy? Is there something that I may be doing wrong in my search query to cause this? I am interested in any potential solutions or ideas.
Just to mention one of the things that are causing this, from Use Groups to share content (emphasis mine)
If you later add new members to the group, they'll be able to access the document only via the document's URL. To make the document appear in the Shared with Me view of a new member's Google Drive, you must reshare the document with the group or share the document with the new member individually.
A possible solution to this (I don't have a domain to test on) is to enable and then use the Drive "advanced service" and not the DriveApp implementation. If one reviews the Drive REST API for Files#list, one will note that the default corpus used when querying for files is files that the user has accessed. This corpus includes any file the user has created themselves (by UI or by script).
Thus, modifying the search corpora could be the answer. There's a lot of extra stuff that has to be added if you want to search Team Drive items, so I'll leave that to the reader.
function searchDomain(query) {
const listOptions = {
q: query,
corpora: 'domain',
};
const results = [];
do {
var search = Drive.Files.list(listOptions);
listOptions.pageToken = search.nextPageToken;
if (search.items)
Array.prototype.push.apply(results, search.items);
else
console.log({message: "No results for search", search: query, options: listOptions});
} while (listOptions.pageToken);
return results;
}
Given that you mention DriveApp.searchFiles you already know the general structure for the query string, but for anyone who is finding this, you will want to review the documentation on its format here

Sorting & Placing shared Google Drive documents into folders

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.

How to provide a static link to a Google Doc that is unique to each user

This doesn't feel like a unique problem, but I can't find an existing solution.
Background:
I am building a course in an LMS. I can insert links, but they are static links and each user will see the same link.
As part of this course, there is a Google Doc study guide that the students should be filling out (and sharing with their instructor).
I was wondering about which is the most viable solution:
1. When joining the course, use Google (Drive API or Apps Script (?)) to create an instance of the file (presumably by either copying an existing one or using a named template) with the appropriate sharing permissions (the learner and their instructor would both be able to see the files). There would be no link to the file within the course, just instructions on how to access their already created file.
2. Have some sort of handler so that I could insert a static link to this handler into the course; the handler would check if the user was logged into Google Drive, if the file existed it would open it, and if it didn't exist it would create it (using similar technique to above). This would have the benefit of being able to insert the same link anywhere and the student could immediately go to their specific and personalized Google doc.
Any other suggestion or existing solution out there?
Much obliged,
David
Both would need the same code that makes personalized copies. The only difference is the moment where you create them.
2 is better because it has less chances of failing since you create at most one copy at a time, and is also more practical to the user.
The 1 option needs to make bulk copies and those take time (drive api to make copy is slow) and script could timeout or reach some quota.
going with 1, one way is to have a single webapp published as owner which receives the drive file ID as a url parameter. on start, the app figures if the copy has already been done (by finding the file by title or by using script or user properties).
if the file isnt created yet, create a copy and share it with the user. then display a link to the file in the webapp. note that the script wont be able to open the file automatically, thus you need to show it as an anchor for the user to click.
to clarify, with this way the user always makes two clicks. once from your CMS and once in your webapp.