Retrieving comments in the most recent revision of Google Doc - google-apps-script

I'm pretty new to Google Apps Script and the API, so please bear with me!
As part of a project I'm working on, I would like to retrieve all the comments that are present in the current revision of a file, hopefully using either Google Apps Script or the Google API.
The reason I want this is that the documents I'm working with have had many collaborators and many different revisions, and some of the people who were supposed to be 'resolving' comments didn't realise that even though deleting the text a comment is anchored to removes the comment from the user interface, this comment still counts as "open". i.e., these comments still appear in the comments thread, it just looks like they link to an earlier revision of the file.
Now, I want to retrieve all the comments present in the most recent versions of these files and -- say -- export them to a Google Sheet. However, if I choose all "open" comments, I get many, many more comments than I want, for the reason I stated above.
One possible way I was considering is to decipher the comment's anchor id "kix.xxxxxxxxxxxx", hoping that it would at least give me information on the revision history, but I see no documentation on this, and I'm not even sure it's possible since this is related to Google proprietary editor Kix.
I've read these articles that don't give me much hope:
How to match comments on an image using kix anchor (or not) in Google
Docs
Anchor documentation does not exist?
Creating anchored comments programmatically in Google Docs
However, If I download the Google Doc to a .docx, then I get only the comments that are present in the most latest revision, and this is what gave me some hope that maybe it would be possible to extract them using GAS.
I suppose if all else fails I could download all the documents and then try to get some sort of macro on Microsoft Word to extract the comments, but I decided to ask here before I resorted to that! If anyone could suggest any ideas, I'd be grateful!

Related

Limiting downloading, copying and printing for EDITORS in Google drive

I am looking to find out if it is POSSIBLE to restrict EDITORS from downloading, copying or printing a Google sheet or other documents in a Google Drive. We share sheets/documents with our customers so they can fill in the details. For that we need to make them EDITORS (so they can edit and even invite others to the party). I know that we can restrict COMMENTERS and VIEWERS from downloading, but in our case we need to prevent EDITORS.
We have a LOT of intellectual property in our sheets (custom formulas and approaches), and we would like to be able to prevent people from simply downloading it. As I understand it this SHOULD be possible using the Google Drive API, but I have not been able to figure out how to do it, yet. Looking at the API it obliquely says it MAY be possible, but it is not clear :-(
Direction, or sample code, would be VERY much appreciated.
TIA
It's not possible to restrict editors from downloading files. The documentation mentions that only commenters and viewers can be prevented from this. Then in the API docs concerning permissions and their definitions you will see that there's nothing controlling downloads either. This is just a UI change.
If you think about it, the reason is clear: Even if you manage to stop direct downloads, to anyone with at least read access to the file or API this is just a minor inconvenience. They can still read all the content and metadata from the API and replicate the file perfectly. Even viewers with copy disabled can still read the formulas from the formula bar. Sharing the Sheets file is inherently unsafe if you have confidential data in it, since a determined attacker can still get all your trade secrets easily. You're only supposed to share these files with trusted users.
My suggestion is to take a different approach. Do not share the Sheets file at all and use something else as intermediary to request data from your customers. For example:
Create a Google Form to save the responses to a Sheet. Your customers will only need to fill out the form and the sheet will be filled with data that you can handle on your side.
If you need the users to also view some information in the Sheet before filling out their info you can build an Apps Script Web App that displays only the plain data that you need to show them. With this you can hide the formulas and other sensitive information. Using templates and server functions you can allow the users to interface with the Sheet data similarly to how they do it now, but with a more restricted view. You could even allow them to edit only the data you want them to. This requires more work and is starting to delve into web development, but It's much easier than a fully fledged website since the hosting and interfacing with APIs is handled by Apps Script.
You could just create your own application and use the Sheets API to read and write data from the Sheet. This is pretty much the previous suggestion but much harder, though in the end it will give you more flexibility.
The bottom line is that sharing your Sheet in any way is akin to giving your users full database read or write access, and there's no single setting that can prevent that. Your best bet is to avoid sharing these files and use a different method to request user data.

How can I search for a specific function and then edit it?

Unless I'm missing something, the App Script Editor lacks the ability to find a specific function within the project.
I'd like to be able to search for a specific function and then edit it.
Alternatively, how can I view a list of functions within my project (and which .gs file each is in)?
To be clear, I have a number of (logically named) .gs script files within my project each with a number of functions within.
This is not yet an available feature for the new Apps Script IDE - however there is already a fearure request for enabling cross-file search on Google's Issue Tracker:
Search function in Apps Script editor should allow searching of all project files, not just the currently open one.
Google prioritises feature requests by popularity, so make sure to give the issue a star on the above page to let them know this is highly requested.
In the mean time, the legacy editor still has cross-file search available, so you can use this to search in all project files at once.
The new Monaco script editor lacks cross-file search, but the legacy script editor has this feature. So a workaround is to click Use legacy editor in the top right-hand corner and do your search there.
Click the star icon ☆ in the top left-hand corner of issue 175233560 to vote for this feature request and get notified of changes in status.
Yes it does seem to lack that ability as far as I can see. I use control f and then just select between the different files. It keeps the search active between files. Making it possible to find strings in other files. I would prefer if it would just tell me where but I haven't identified that ability as yet.

Is there a way to list who viewed a file (and when)?

I'm searching for a little while and I can't find what I'm looking for, so it may be a good idea to ask it here.
Is there a way to obtain, by API, for a specific file, the name of the last user (and timestamp) who accessed a file?
Goal: to write to script to know who access what on my drive.
Unfortunately, what you want cannot be achieved. The Drive Activity API is used only for retrieving the activity on the specified file. You can gather the users who have view access but not the ones who have actually viewed the file.
The action object from the Drive Activity API is represented by the action detail. For a document at the moment, the action details are the following: create, edit, move, rename, delete, restore, permissionChange, comment, dlpChange, reference, settingsChange. Therefore, taking these into account as well, you cannot see who viewed a file.
Since the option of seeing the actual viewers of a file is still a relatively new feature in the UI, the option for the API does not yet exist.
What you can do instead is to file a feature request on Issue Tracker by accessing this link here.
Reference
Drive Activity API
You can use the Drive Activity API.

Given a script URL, how do I determine the actual document?

I was experimenting with Webextensions earlier this year connecting to scripts in my Google account, but now I cannot determine which script I was connecting to.
So I have the script URL. It looks like a normal script URL, e.g.:
https://script.google.com/macros/s/jG5dForTySatijhf-nmG49F64j62RzkgH6dFgr4SalpUyvF/dev
But I cannot work out which script that URL belongs to! Given a script URL, how do I determine the script in my Google Drive?
UPDATE:
I think I can phrase my question a little better now, and I cannot imagine I am the first person to have this problem so I imagine there must be some solution.
I have the web app URL (like the one above) for a published script. But I cannot remember which actual script (in my Google Drive) it is associated with.
Given a web app URL, how do I determine which file in my Google Drive it is associated with? I know it's one of them but I don't know how to determine which one!
Perhaps I can suggest an alternative approach. This would be particularly useful if you have been experimenting with scripts, as you suggest, and have not created a lot of projects.
All your projects can be found on Google at:
https://script.google.com/home
The metadata for each project includes the name of the container, and you open the container from that screen

How to programmatically list the suggested edits in a Google Doc

When listing the comments in a Google Doc file, using the comments.list API method, the returned list only contains "regular comments", and does not contain "suggested edits", i.e. the suggestions that users can make when the document is edited in "suggesting" mode.
Is there any way to programmatically enumerate the suggested edits, and is there a way to programmatically accept them?
I think as of now, you can not get it programmatically. If you check the Google Drive documentation, you will not find any documentation about suggested edits. Also, this SO question prove it that Suggested edits is not yet accessible via API.
The question here is
Can I include "Suggested Edits" from my Google Document alongside the
comments. Can those be accessed via API yet?
And the answer by Mogsdad is NO.
So what can I recommend to you is file a feature request about this issue :)
Unfortunately (yes, in 2020), this is still not possible via the API. It is possible to see what the suggested edit is (added or removed text, changed style, etc.) from the API, but you can't see who made the edit or when they made the edit.
The work around I am currently using is (does not work retroactively):
Subscribe to Google Doc changes via email (Gmail specifically)
Use the Gmail API to get all emails received from comments-noreply#docs.google.com
Parse the email contents to get comments AND suggestions, including when, who, and what
Now that you have the suggestions and comments, you can do whatever you want with it. Save it to Google spreadsheet, save it to .csv, etc.
This answers the first part of your question: programmatically enumerating them. As for programmatically accepting them, currently not possible through the API. I haven't done it, but I believe it is possible to do it via JavaScript.