How to provide a static link to a Google Doc that is unique to each user - google-apps-script

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.

Related

Access - Scripts Bound to Google Sheets

From the documentation of the the bound script:
https://developers.google.com/apps-script/guides/bound
"Only users who have permission to edit a spreadsheet, document, or form can run its bound script. Collaborators who have only view access cannot open the script editor, although if they make a copy of the parent file, they become the owner of the copy and will be able to see and run a copy of the script."
This feature is kinda limited. I have created a big sheet that are used by multiple users. I need the script to be executable by Read Only access users. The script that I created are not making changes to the document, so it should not affect by the access level of the users.
Administrator have Edit access to the document. They have access to all calculation/ configuration cells/ sheet.
Managers have Edit access to the document. Managers have Edit access to most of the cells/ sheet. Managers task is to update the cells
All remaining users only have Read Only access to the document. They can view cells and the automated calculation.
Because the cells have too many columns, I have created a button (eventually I want to move it to onOpen) for easy navigation to a specific cell. This is done dynamically based on today's date.
This is working fine as expected for Administrator and Manager (have Edit access), however not available for the rest of the users with Read Only access.
Is there any workaround for this?
Thanks
Unfortunately not. You are the edge case. The whole point of GAS is to make editing easier, providing tools. A Google document/Spreadsheet/etc. normally isn't THAT hard to maneuver through.
However, if your REALLY want viewers to see specific parts of your spreadsheet, but not edit it, you could try making a standalone script. Have it pull data from the spreadsheet using the spreadsheet's id and then project it on the HTML page, in a table or list.
I Agree with EvSunWood, though I would go about the task in a slightly different way. You could give read only access to the main sheet to the user. And then give them full access to a separate sheet with code attached which onOpen updates itself from the original with all of the latest data and then navigates to the appropriate row.
I agree too with this answer, it's not possible to allow view only users to execute scripts, but contrary to using an script to overwrite edited data, I suggest you use sheets/range protection this way we reduce risks like a failure on the script by on open trigger due to "change collisions" caused by several users opening the same file at almost the same time.

Sharing a Google Spreadsheet containing a script with a workgroup so they can use it, but not modify it

I have written a Google Spreadsheet script which reads data from a user provided CSV file, populates a sheet with the data, makes a copy of the spreadsheet with a different name, and provides a link for the user to click to see the new spreadsheet. After making the new copy, the original spreadsheet is then cleared of user data and restored to its original state so other users can use it.
I want to share this spreadsheet with the workgroup (several hundred users), but I don't want them to be able to modify either the sheet or the script. If I share it read-only the script won't run, but to get the script to run I have to allow users edit capability and that is a bad thing.
I have googled myself silly trying to find a direction in which to go, but I am still wandering in the wilderness.
Can any of you point me in the right direction? If I need to be more specific or provide more information I will be glad to do so.
Thanks,
Larry
P.S. Other questions of note:
o Is it possible for a app-script in a spreadsheet to make the new copy active and 'close' the original shared spreadsheet?
o Can the user be made the owner of the new copy?
There is the option to put the code that you don't want modified, into a Stand Alone Apps Script file, then use that code as a "Library" in your script bound to the sheet. You can set the permissions on the Stand Alone Apps Script to VIEW only. That will keep people from changing it. When you share the Apps Script file with the users, they will get an email notification. So, they'll have the URL to the file, and can view it, but they can't edit it. (If you set it to VIEW only)
So, you'll be using both your spreadsheet file, and a Apps Script file. Two files, and setting the sharing to VIEW only on the Apps Script file. You can still give people EDIT access to the spreadsheet.
You'll need to go through a process of making the function available to your spreadsheet. This is called a "Library", but don't pay much attention to that name when creating the file.
Create a Stand Alone Apps Script
Create the function you want to share.
Get the Project Key --> FILE, PROJECT PROPERTIES
Share the Apps Script file with users you want to give access to, but only give them VIEW access to the file. NOT Edit.
Give the Project Key to the user, (In this case that's just you.) and have them add the library, or if you have access to the spreadsheet, you can probably add the key yourself. So, in your case, you will not be sharing the Project Key with anyone. You don't need to.
In the script file that is bound to the spreadsheet, click the RESOURCES menu, and the LIBRARIES menu item.
Enter the Project Key
Click SELECT button, and turn the library ON.
Make sure to set the library Identifier to a key word you'd like to use.
Go to the code editor, create a function, and type the Library Identifier.
All the functions that are available from the Library will show up
Google Documentation - Gaining Access to a Library
Keep in mind, that creating a library is nothing more than writing code in an Apps Script in a stand alone Apps Script file. If you've done that, then all you need to share is the Project Key. The user of the Library won't see a list of all the available Libraries when Resources, Libraries is opened. You need the Key.

Can a script automatically share Google Drive files to another email address?

Does anyone know if it would be possible to set up a script on my Google account that triggers when a new item is shared to the email and then automatically shares it with another email address?
I use a personal Gmail account, but whenever I do school work documents are always shared with my school email and it is a pain to have to share everything manually or keep switching accounts.
When I looked a few weeks back I was unable to find a way to have a script trigger on new shared items, but I am new to these scripts so if anyone has a suggestion I would appreciate it. Thanks!
I don't think you can actually triggers on new items. But you can write a script to read the Drive feed to easily check if there's new files.
Such script would still have to be triggered on a time-driven basis, so you wouldn't have "instant" access on your second account. Also, if I'd write such script, I would just create a folder, share it with the desired accounts, then use the script just to move the files to this folder. This approach has the benefit of easily allowing you to check which files are shared (with your specific account) and which aren't. And since folders are actually just labels in Drive, it is not any problem if you like or need to have your files organized in another folder structure.
By the way, have you seen Chrome users feature? It helps a lot when working with multiple accounts.

Make spreadsheet only editable through script

I have a script which adds new rows and changes the sheet in some ways.
I'd like to lock this sheet so it cannot be edited manually, but allow the script to be run.
Is there a way to do this?
Yes, it's not the most easier thing to do, but it's possible.
But before we start it's important to understand how Google spreadsheet/sheet protection works.
There's no way to protect a sheet or range from the file owner. So, if you also want to protect a file from yourself (assuming you're the owner). Then the only solution available now (I do hope they change this in the future) is to change the ownership of the file to another account. What most people do is use another Google account that they don't use actively (you can create a bogus easily if you don't already have one).
Then, after you have transferred the file to another account, you can easily lock changes to ranges, sheets or the whole spreadsheet using the regular GUI.
Ok, now to the script. When a script runs, it's always using the authorization of a specific account, and it can do everything that this account can. For example, when someone runs a script directly from the script editor, or clicking on a custom menu or image: it runs under the account of who's clicking. But if you set a installable trigger, then the script always runs under the account of who setup the trigger in the first place (not who's performing the action that actually triggers it). Simple event triggers run as who's triggering the action (you can think anonymously), but they can't do much, it's a security feature (read the link for a better understanding).
Lastly, when one is publishing a script as a web-app, then there's a selection box to choose if the script runs as the developer or the user. Very simple.
Back to the problem. Since we have locked out everybody from the desired sheet/range. For a script to make changes to this locked area, it must run as the file owner!
If the changes you do are automatic, e.g. via a installable trigger, then you're good. Just setup the trigger using the file owner account and that's it. Also, if your users are accessing the script "externally", that is, via a web-app. Then that's easy to, just setup the web-app to run as the developer (the file owner).
The most complicated scenario is if you need to run the script from a custom menu, which will then run under the privileges of who's clicking on it, which themselves can not make changes to the protected area. The solution to this is to deploy the script as web-app running as the developer, and have the function running from the button click to call the deployed url using UrlFetch, possibly passing some parameters to designate what needs to be done. Then, since the webapp runs as the developer (which is the file owner), it can make any changes required, and return (if there's something to return) any value (usually a JSON) to the calling function (which is running as the user and is associated with his session), so you can show him a message on a popup or toaster, etc.
It's kind of tricky but works great, and you can do all that on the same script contained in the spreadsheet. But if you're concerned that your users may access the script editor and change the code (which is possible), than you should separate the web-app part on a different file, that you don't need to share with them.
Yes but only for others not for the script owner. Just use 2 accounts one as the owner and the other one as the reader.

Fetch and open a user specific url stored in a spreadsheet database using Google Apps Script in a Google Site

I've got an ambitious first Google Apps Script project for use in a Google site.
Using a spreadsheet database I have a list of Google users that each have a unique URL to a service at a non-Google website.
What I need to do is place a link in a Google Site that will
Get current user id based on logged in user
Interrogate spreadsheet database for the unique URL based on Google user
open the unique url in another tab/window
Any pointers or code snippets would be very much appreciated, especially around minimising user authorisation. As it is a Google Apps domain the script could be pre-authorised as a trusted script if that is even possible.
Thank you
I did write a site gadget that does exactly what you want, (except that users have to enter their name because the site is public and I wanted to avoid asking authorization), you'll find the code and explanations here, I'm not going to reproduce the whole code here but in short it allows to enter a name (even partially) and looks for any match in a spreadsheet list then shows a link to an individual url. It is also possible to see it working on the same page.
(see page bottom, it looks like this)
To answer your second point, there is no way to avoid the authorization process, the idea of 'trusted source in a domain' has already been mentioned in a few discussions with project members but for now it is not possible.