How to share or distribute google apps script with other users? - google-apps-script

I have created 1 google app script project. which sends me an email every morning with schedule and tasks.
It has .gs script
And a trigger to execute everyday
If I want to share this with other users, haw can I share with them?
Do I need to share the code and trigger or is there any good way to share apps script.

These are the currently available methods to distribute:
Add on
Published add ons are very useful in distributing the functionality of the script with many users
Unpublished add ons can be shared directly by sharing the script. They have a lot of restrictions.
Simple triggers(like onOpen) are triggered automaticallyref.
Installable Triggers(like onChange) are managed by the script per user
Library
Users need at least read access to the script
Triggers are not shared
End users still need to create their own apps script, which accesses the library
Web app:
Variety of distribution options
If you don't want to share source code, you don't have to, but the web app should be published to run as yourself and should be accessible to "anyone"
Triggers are managed by the script. Installable triggers can be created per user. But simple triggers cannot be triggered "directly" from a http GET/POST. However, simple triggers are triggered in the project itself that is bound to a document(say, Google sheets)
End users still need to create their own apps script, which accesses the web app using HTTP requests GET or POST
google-apps-script-api?:
This is mostly used to run your script as yourself without accessing the code editor, but can also be used to distribute if both the calling project and the target project uses the same GCP
Under most circumstances, you'll be restricted to less than 100 end users, except in case of published add ons or web apps(set to execute as the end user)

You can publish the script as a web app and set the app to execute as the authenticated user.
When your users launch the web app, they would authenticate the app with their own Google account credentials and the triggers would be added to their account.
With this approach, there's no need to create the script's source code with your users.

Related

Apps Script (Google Sheet) not allowing me to Run Script

I have a fairly simple dataset in a Google Sheet. I created an AutoSort script. I saved it, and when I click "Run," I get the following errors. One from a pop-up, and another from the Execution Log.
Pop-up error:
Authorization required
This project requires your permission to access your data.
*For this error, there is a button to "Review Permissions" and I log in using my google account and then just nothing happens.
Execution Log error:
Warning This project requires access to your Google Account to run. Please try again and allow it this time.
The Owner of this Google Sheet is my personal Gmail account, and I am making these edits and created the script using my business Gmail Admin account. I also tried to access this sheet and run the script USING my personal Gmail account, and received the same error:
Google hasn’t verified this app
The app is requesting access to sensitive info in your Google Account. Until the developer ({mypersonalemail}#gmail.com) verifies this app with Google, you shouldn't use it.
Any insight as to how I can authorize this would be appreciated. It sounds like something small I'm missing.
Also, in my personal email I receive a message with subject:
Review edits to your Apps Script project within your document
and it allows me links to access the worksheet and the script, but I don't see any way to approve the edits, or anything like that.
Expected behavior: What I am expecting is for the script to Run, when I click "Run."
It's not a good idea to mix accounts from different domains, specially when using a free account and a Google Workspace account like you have done because that is the cause of the situation that you are facing.
My hypothesis is that the Google Cloud default project linked to the bounded script is created with the account used to create the project.
If you need that you personal account be the spreadsheet owner the best is to create the script using the personal account, and when needed, create a Google Cloud Standard project (GCSP) using the the personal account. You might try to fix the problem with your spreadsheet and the current Apps Script project by creating a GCSP, as was mentioned previously, by using the account that is the owner of the spreadsheet and linking it to the Apps Script project.
Note: If your script is using sensitive scopes you might have to set the OAuth Consent Screen publishing status to tes and add your Google Workspace account as tester.
Ref: Setting up your OAuth consent screen
Once you have finished the setup of your Google Apps Script project you should be able to use your Google Workspace account to update and run the Apps Script code but any new deployment and new version should be done using your personal account.
If you have access to Shared Drives and are allowed to use them for your spreasheet, consider to move it to a Shared Drive as this will make a lot easier to manage your script.

How to allow a user to execute a google script that changes another spreadsheet the user doesn't have the access to

I'm building an accounting system for a small business using Google Sheets. I have different spreadsheets:
1 with the list of all the students attending courses
1 used by the secretary to manage the invoicing
1 for the "boss" where he can see his business balance.
I wrote many functions with Google script to automatically update all the others spreadsheet when some changes are made.
Here is the problem, as you can imagine I can't grant the access to the balance sheet to all the users. Is there a way to allow every user to run a script that makes changes to a spreadsheet they don't have the access to?
You can do this by using a web app to allow users call your script as you.
Create your script
Create and publish a web app which calls your script / function
crate another script / function that the end user can run to call your web app URL
Google Sheets web app to run script as owner

Google Script in Google Sheet - Make copy of sheet loses permissions

I have a google sheet that has a script embedded.
In the script > resources > cloud platform project, I have added and linked a project to this script to enable the APIs. This works great in the current Google Sheet.
Challenge:
When I make a copy of the Google Sheet the Cloud Platform assigned project is lost and it requires users to enable these permissions again.
Is there a way to prevent this from happening? Am I missing a setting?
What you're trying to do is not possible. Even if you try making a copy of a standalone script that has a GCP project associated, you will get the same functionality (no GCP project associated).
Also, it makes sense that each new user that runs the script has to explicitly authorize the APIs to make changes in their files, send emails on their behalf, or what have you. It's a basic step in the 3-legged OAuth 2.0 workflow, and it can only avoided if you use a Service Account.
So in your situation, I would just use Advanced Google services, even if each new user has to grant authorization. In the end, the appropriate advanced services are automatically enabled in your copied project; users don't need to enable them manually, only grant them authorization.
Reference:
https://developers.google.com/identity/protocols/OAuth2
https://support.google.com/a/answer/2538798?hl=en

Is there a way to build an Google Apps Script application that uses BigQuery Service as a service account instead as a user?

What I trying to do here is an application that connects to bigquery, executes some query and writes a spreadsheet in current user's Google Drive.
I know that I can change the "Execute app as" option when deploying the app. But when I do this, and the user try to use the app, Google asks for permission to access the BigQuery.
I do not want to give permission to each user to access BigQuery.
We have about 50 ~ 100 users who will use (and this number can increase)
Some images:
When access the application's url: http://i.imgur.com/kGzJ3ps.png.
When the user clicks in the "continue" button:
http://i.imgur.com/4cRmhZ6.png
Hope I've been clear.
You would probably need an App, that had people sign in with their Google account using oAuth2
Google Accounts Authentication and Authorization
I think that this link gives an overview of what your options are:
Permissions and Types of Scripts
I guess you would need to have an App, that ran as User at the keyboard, so that the spreadsheet saved to their Google Drive. If you wanted the user to be able to download a file to their computer drive, that's a little different.
Just use HTML Service. It's basically just building a website. You just don't have a domain name. But the spreadsheet you are distributing doesn't have a domain name.
HTML Service Google Documentation
From an Apps Script HTML Service, you can write to Spreadsheets, Documents, and run HTTP Requests (urlFetch).
URL Fetch Service
The permissions in the Apps Script App can be set to run the Apps Script file as yourself, with access to anyone, even anonymous. That way, the user will never be asked for permission for any files that you own. You will need to authorize it once, the first time any script that needs access to your files is run.
Because anyone could run your Apps Script App, you might want to have a log in system to authenticate users.
You would need to use the Big Query API with Apps Script urlFetch to access Big Query.
So, with the HTML Service, you can do most everything you could do with HTML, CSS, and Javascript on a regular website. You can create custom input forms, have different pages and display data in tables. Plus it's served over HTTPS.
One solution that you can use if you don't want all your users to enable BigQuery is to use a webapp deployed as a service running as "you" and that will respond to an urlFetch coming from each user through the script they run as "themselves".
This service will be deployed as any other webapp using a doGet main function and return its data using contentService
Yes, that is possible. Check out this answer here: Using Advanced Google Services with Service Account and also check this awesome library: https://github.com/googlesamples/apps-script-oauth2

Make a Google web app that's triggered from a Google Spreadsheet form?

I have a Google Apps script (written in a Google Spreadsheet) that creates a separate calendar and events for it with respect to a university course and the university calendar. The GUI is a Spreadsheet form, which triggers the execution of my script. The script gets its input from the spreadsheet form entry.
The trouble I have is that I can't easily deploy my solution to other users. Yes, they can make a copy of my sheet/form/scripts, so that they can run it on their account. Several problems, however, result from this approach:
triggers don't get copied - this is because authorization to access local info (calendars, contacts, etc.) must be given by the users before a trigger is enabled. So users have to re-connect the form submission trigger to the Apps Script for it to work (this is not user friendly).
if I change the code of my apps script, the users are using a copy and won't see the changes.
The solution seems to be Google Web Apps, but from what I've read, I can't re-use the form-based GUI of my approach. Or can I?
Google Spreadsheet Forms have the advantage that they are very easy to create, and you can already specify what fields are mandatory, etc. I don't want to have to re-write all of this in a complicated GUI front-end to my Google Apps Script (Edit: even the GUI builder tool seems way more complicated than a Spreadsheet Form), yet I want other users to be able to create the calendars and events in their own Google accounts. Is there a way to keep my form, but have it hooked to a Web App version of my code?
Try adding the form on a google sites (intranet). This way, any user having access, will be able to use the form as it is. All logic you created will remain the same.
Creating a script however will allow the user to execute the script "under their login", thus enabling access to calendars, contacts......