How can I let others install my Google Apps Script? - google-apps-script

I've made a simple script in Google Apps Script. The script builds forms based on a spreadsheet, the script itself is part of the spreadsheet. It adds a menu item. People would like to use it. They are non-technical people, so I would like to make the installation of my script as easy as possible. With 'installation' I mean a way to 'import' my script into their spreadsheet and automatically set up the needed triggers so the menu item becomes visible. It should be possible for anyone to install it.
I've been thinking of the following solutions:
Publishing as an add-on: my script wouldn't qualify, and even then I can't wait for Google's approval.
Copy-paste. I don't like this, it's way too complicated.
I've looked at this question: https://stackoverflow.com/questions/5334751/how-do-i-share-a-script-i-wrote-with-my-co-workers-on-the-same-google-apps-accou, but the people wanting to install it are not on the same domain in my case. The question is also 3 years old.
EDIT: As it turns out, there doesn't seem to be a solution. I've filed a feature request here: https://code.google.com/p/google-apps-script-issues/issues/detail?id=4122&thanks=4122&ts=1403949074

If your Google Account administrator allows you, you can share scripts/spreadsheets outside your organisation either by sharing directly with another person with a Google Account (Gmail or Google Apps) or by making it public (share with a link or published online).
https://support.google.com/drive/answer/2494822?hl=en
Keep in mind that if you use the Script Properties to store values, each time someone uses the shared script it will overwrite the values. In a shared context it is best to use User Properties instead. This way each user can store their own values.
https://developers.google.com/apps-script/guides/properties

There is no way to import a script in an existing spreadsheet , the "normal" workflow is , in a way, the reverse process, that's to say create a sheet from an existing template that already has the script in it and start to work with that copy.
From your description I'm not sure this method could be useable but I'm afraid there is no other way if you want people who will be using it should not go into the script editor at all.
There would be a couple of functions to write that should run at install time and would create the triggers (if needed) and trigger the authorization process.
I made such a SS some time ago that needed authorizations and I added an "install" menu that made its use quite simple (example here on a friendly hosting site).
I know this is probably not the answer you were expecting but it was definitely too long to fit in a comment anyway.

Related

How to avoid saving a google sheet and have multiple people editing at the same time?

I’ve created a Google Sheet – kind of like an app – using script. It’s a number of blank cells where once added some information, it creates a string with that information in the right order. I created this to help some colleagues. We are using free accounts.
I face 2 problems:
The app is supposed to be used by only one person at a time, but I’m sure at some point two or more people will want to use it at the same time. Is there a way to allow this without they interfering with each other? I’ve read you can share a link that creates copies of your doc, but that wouldn’t work in this case because I intend to keep updating it regularly.
The changes people make to the sheet will stay there. Right now I have a onOpen function that just rewrites everything, but if someone accesses the document while someone is using it they’ll rewrite everything and ruin their work.
Any ideas on how to solve these two problems? Thank you so much in advance!
I think that you have to find another way to implement your "app" because Google Sheets not a good tool for limiting that only one user edit a spreadsheet a time, but if for any reason you decide to keep with it,...
... you need to implement a workflow like the following
Save the active user email on PropertiesService.getUserProperties().
This should be done by each user by running a script themselves. The most user-friendly will be by using a macro, custom menu or button (a drawing with an assigned Google Apps Script function). You might use an open installable trigger to validate that the active user have already done this.
You might have to make the function that clears the data a "smart" function.
You might have to consider additional cases, like if the owner of the spreadsheet opens it when there is an editor working on the spreadsheet.
Use installable triggers to manage the sharing settings.
Use an open trigger to remove all the editors except the active user
Use a time-driven trigger to add again the editors. To make this work effectively should define how the time-driven trigger will know that the last editor have finished their session, i.e. you might use DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).getLastUpdated()
The above should help you with both problems, as long you as the owner do not open the spreadsheet as is used by someone else.
Other alternatives that might work better is to create an add-on or a web application.
Related
Determine current user in Apps Script
Last modified date in Google App Script GAS sheets

How can I create g-suite add-ons or share Appscripts that I've written with others?

Every two weeks my boss posts the shift schedule on google sheets app. Using Appscript I built a program that scans the schedule for my name and updates my google calendars app so I can have my shifts automatically updated. I copy and paste the schedule into a personal spreadsheet and then run the program.
Is there a simple way I can share the program with my co-workers?
I figured the easiest way to do it would be to build an add-on, that takes their personal g-suite and does the same thing, but I have no idea how to do that.
Does anyone know where I can find sources to execute this?
One option here that would be a lot easier for you would be to make a template spreadsheet which has the code written in it, that your coworkers could make a copy of the spreadsheet and run the code. To make it more user-friendly, I would recommend using a UI menu to prompt them to enter their name, then it could scan the spreadsheet for their name, making calendar events accordingly.
Google has good documentation on publishing add-ons as seen here, but it would be a much more tedious and time-consuming process.

Is it possible to add the same custom menu to every new google document? The goal is to make a local script function like an installed app

Is it possible to add the same custom menu to every new google document? The goal is to make a local script function like an installed app.
This seems like it should be easy but I can't seem to find a way to do it. I'm using a standard account for personal use. I've created a script attached to the original document and also made copy as a stand alone script. (I'm not sure that makes a difference.) I've searched and read about deployment and it seems to be only useful for a domain account or publicly as an app. I've played a little with scripting but this is the first attempt to get a script working "globally".
Any help will be most appreciated.
I found a way to do this in an old example at: https://sites.google.com/site/scriptsexamples/custom-methods/2d-arrays-library.
The script is in a project and that needs to be connected to the current doc. The onOpen() script is a UI that contains the line:
.addItem('Select Phrase', 'PS.showPrompt')
where PS is the name of the attached library. Once this is done, as you said, you can make a copy and use it with the script active.
It is a little slow as they describe about using a library, but is make the code easier to maintain. Thanks to all for the direction to look.
What we do is create a copy of the original document (the document with the attached google apps script). The script is copied along with it and the copied document has the exact same functionality (menus, custom functions, etc).
You could create a Google Docs Add-On using an independent Apps Script script and then install your Add-On globally. For more information regarding its installation check this documentation. However you would need to run this Add-On on the new documents to get your desired menu.

Is there a way to run scripts when someone is answering to a Google Form?

So, here at the company I work for we usually manage the workforce weekends schedules in a very manual way using Excel, and then upload a pdf version of the sheet in our intranet so everyone can see their own schedule for the rest of the month and look up for swaps if they need to.
The problem is that the workforce recently has grown up a lot and we're spending a lot of time validating and reuploading the schedule everytime we need to do the shift swaps. So I decided to start a project using Google Sheets and Google Forms, so we could just show iFrame of the sheet in the view mode, and their supervisors could make their swaps via a google form, which would update the sheet and they could see it happening on-the-fly, I guess.
Now that I gave you some context, what I really need is to discover if there's actually a way to accomplish what I'm trying to, because I already stuck once realizing that the script running from a Google Sheet only works with edit permissions (I need to no one have permission to change manually the sheet and preferably not even needing to link account to Google), but now I realized even when scripting inside of the form it doesn't run when people are answering to the form, but only in the edit mode, which I think it's useless for me since questions should be dynamic (the form would look for the available weekends and employees in the sheet using some validations).
For example, the code below runs when I open the form in Edit mode, but not when I open the shared link:
function onOpen() {
var ui = FormApp.getUi();
ui.alert('test');
}
So, my question is if this project is possible using Google Scripts and the suite, if there's some kind of option or permission I'm missing to make it work, or if it's worth I try to do it using an not inbound countainer script, or maybe HTML Service, or if I should forget Google and look for another tools for this job.
Thanks.

Is it possible to have one script for multiple spreadsheets?

I have one master spreadsheet and a number of copies. This master spreadsheet uses some scripting.
Is it possible to link all the copies of this master spreadsheet to the same script as in the master spreadsheet?
Objective:
changes in the scripting in the master spreadsheet are automatically used by the copies
aka: low maintenance
amleczko is right: you should use the new library feature in Google Apps script.
However, as of today, you won't be able to do exactly what you want (using the same script for several spreadsheets). What you can do instead is save a version of your script (Files > Manage Versions...), in order to create a library. Then, import this library in the other spreadsheets (Resources > Manage Libraries...). Switch on the "development mode" so every change made do the library will immediately take affect in the spreadsheets using this library. Otherwise, you will have to save a new version of the library for every change, and manually update the version number of the library in every spreadsheets using it.
The problem is, you need to write a script in every spreadsheets using your library, with skeleton functions like this:
function doSomething(){
myLibrary.doSomething();
}
best way is to publish as add-on, then install the add-on, it will appears in every spreadsheet you open. and you can publish as private, which only seen by yourself.
I think this has changed. According to Issue 40 starting from 22 May 2012 there is such a possibility. Please check:
https://developers.google.com/apps-script/guide_libraries
https://developers.google.com/apps-script/guide_versions
http://googleappsdeveloper.blogspot.it/2012/05/introducing-versions-and-libraries-in.html
It's not possible in this way that you're thinking. At least, not yet (see issue 40).
But, depending on your script usage, you may connect them "the hard way" or even better, use only one script. The script on the master spreadsheet can open the other spreadsheet files and do its job "remotely". It's not required that script to be hosted on a spreadsheet to interact with it (read/write on it). You only need a script hosted on the spreadsheet if you're going to use spreadsheet events triggers i.e. on-open, on-edit and on-form-submit.
Maybe you can develop a nice UI for the script on the master sheet and publish it as service. Then only have a link on the copies to access the same UI on a different browser tab. Adding parameters to the link the script UI can even adapt to the particular spreadsheet that is "triggering" it.
Well, that's all I can imagine now. But, unfortunately, there's some use cases that just don't fit this nice "workarounds". For those, one can only star issue 40 (to kind of vote and keep track of updates) and hope it's developed soon.
The solution I put in place in this context was to have a Google Site, where the Master Script is embedded, and where the Spreadsheet is embedded too
Then, the script, refering to a dedicated spreadsheet, looks for the Google Site Page's name, looks in the Master spreadsheet and get the ID of the spreadsheet which is embedded in the Page.
I have solved this problem when using a script which auto generates spreadsheets.
Typically, I will add a sheet to any spreadsheet with a script called "Info." I'll use that to store information that it important to the script. In my script which auto generates more spreadsheets, I keep track of the ID of the created sheet. This way, I can then quickly call up all of the "linked" sheets, and interact with them with using the same script. It might even be worth writing the script in one sheet, and keeping it totally separate from your Master sheet or it's children.
Take a look at this function, it might give you some ideas.
SpreadsheetApp.openById(id)