Can App Script triggers stick when making copies of file? - google-apps-script

I have a trigger on a Google Sheet set up to run a function weekly. I am hoping to scale this trigger out to my team without having to go into each teammate's individual sheet's script editor and enable it. Do you know if triggers can stick when making a copy of a master sheet that has a trigger set up? Can triggers be saved and used via shared libraries? I haven't had much success and would love some pointers if you have them.
Thanks!

Unfortunately, the answer is no. Only the owner of the google account can create triggers for himself/herself.
However, there is a great alternative. You can add this code to the master file (script editor):
function createTrigger() {
ScriptApp.newTrigger("Name_Of_The_Function_You_Want_To_Trigger")
.timeBased().onWeekDay(ScriptApp.WeekDay.MONDAY)
.atHour(7).create();
}
and when the person of your team executes this function (in his own copy of the master file), the script will create for him a particular trigger for:
Function: "Name_Of_The_Function_You_Want_To_Trigger"
type: timeBased()
Hour: 7am
When: weekly, every Monday
That saves you a lot of time, since the user (your colleague) has to run this particular function just once from his own file.

Related

How to implement? Spreadsheet with link - Go To Link Grab Data - Update Spreadsheet Row

I am looking for technology suggestions. Or if this can be done in native google sheets (note the site I am looking to access is behind a username and password).
I have a google sheet that looks like this
birth date
link
data_element_from_website
12/31
https://something.com/3920230
1/31
https://something.com/1920238
lets say on https://something.com/3920230 there is a HTML element 123
Twice a day I want to be able to refresh the data, this could be done by going into the spreadsheet and clicking/doing something.
Can this be done?
What if https://something.com/3920230 is behind a login (authentication). Note: I could be logged in to the website in a different tab... I don't think that would make a difference though...
Assuming you have a script that you would like to run twice a day, you can use Apps Script Time-driven triggers:
A time-driven trigger (also called a clock trigger) is similar to a cron job in Unix. Time-driven triggers let scripts execute at a particular time or on a recurring interval, as frequently as every minute or as infrequently as once per month. (Note that an add-on can use a time-driven trigger once per hour at most.)
If it's just some formulae, you can change the recalculation settings to be either On change, On change and every minute or On change and every hour under the File > Spreadsheet settings menu item and clicking on the Calculation tab.
If you want to get data from public site(no login) you can use ImportXML. You can google a lot about it, for example
https://answerbun.com/personal-finance-money/get-revenue-details-in-google-sheets-using-google-finance/
If the page you want can only be accessed using login, this becomes a rather complicated task, especially using only apps script libraries (not much scraping lib support here)

How to allow to run google script on a protected sheet (in a spreadsheet) [Can't use trigger and web app]

I'm coding a script for a spreadsheet. This script creates a menu.
Then by choosing an option in this menu a function (which uses API) will run in order to filter some columns and hide others.
The problem is:
This sheet is protected (because shared with coworkers) but I want to allow people to run the script, which is impossible without the permission.
I already looked at different solutions:
Using a trigger: Doesn't work because a trigger can't correctly call a function which uses API (yes, my functions use API).
Web App: When the script is run from the spreadsheet, the script is run as the current user, not the script editor. (the web app is efficient if the user uses the HTML page.)
Remove protection -> run the function -> Re-add protection: Can't modify the protection without permission, which is logical.
Add the current user in the list editor -> run the function -> Remove the current user from the list editor: Can't modify the editor list without permission, which is logical.
How can I solve this problem?
This is a pain that I have not seen a good workaround. Google should know that in a collaborative environment that the owner would create script and want users to be able to run those scripts while at the same time not messing with formulas or cells that you desire to protect. The only way I have found to solve this on the sheet itself is to Unprotect and then Protect the cell or range you are making modifications to during the script run. Do be mindful that if the script fails in the middle (after you have unprotected it) the cell remain unprotected. Might want to run within a "try" script.
Are you the spreadsheet owner?
If no, you are not authorized to bound an apps script on it.
But you can make a copy of the spreadsheet by duplicate it and save in your drive.
If the spreadsheet is created by you yourself, maybe you created it by another username. If it does, log in by that user and change the sharing option to allow the specific user can edit it.

Protecting Spreadsheet from renaming in Google Sheets

I have Google Spreadsheet named "TeamWork" and I share it with about 50 other users, they can edit different ranges ad sheets. Problem is that when some of them want to change the name of file, they just can do it, so time to time I have funny names for this file, like "teamNotWork" etc.
Is there a way to prevent anyone except owner from rename Spreadheet? If possible not using "onEdit" script trigger, because its using quota.
Thanks for help!
onOpen wont happen often enough and onEdit will consume too much quota.
instead, use a time based trigger (every minute or every 5 minutes) to rename the file if its not what you want.
it also has the advantage that it will work regardless of how users open the sheet (mobile or desktop).
I would try using onOpen trigger:
function onOpen() {
var file = SpreadsheetApp.getActiveSpreadsheet();
file.rename('TeamWork');
}
You may add one more time Driven trigger and launch it once a day when nobody works with file.

Google spreadsheet: Intercept calls before trigger (event) open

I have a code into a spreadsheet (A) that does a copy of a sheet from another spreadsheet (B) the is activate from a trigger onOpen.
But trigger onOpen starts after 5 or 6 seconds, so a user can do some changes into the sheet.
I would like to keep my sheet for those 10 seconds too.
Have you an idea? I'm looking how to intercept calls before trigger open but I don't find documentation.
Thanks
You can install a onEditSs edit trigger on B (dont use the simple onEdit, instead install one).
From there copy to A whenever the range changes (keep the last one copied on cache to minimize writes).
This way A is always up to date even without opening it.

Change permissions on the Spreadsheet

I have a script (Script by spreadsheet) who writes a series of data in a spreadsheet, but some parts of the spreadsheet are locked.
The users that are running this script from the spreadsheet are blocked in certain areas, it is possible that Script release these areas and then lock them again or run as adm of the Spreadsheet?
Thank you.
The user would need to have permission to edit sharing on the Spreadsheet, which would not be the case if it was locked.
Is the script an onEdit trigger? If so, you can add a trigger from the script editor that will run as yourself. Just make sure to rename onEdit() to something else so it doesn't try to run as the regular user as well.
If this isn't onEdit, I would suggest making the function they call add a specific value to a specific cell. Then you can have a timed trigger every minute running as yourself that watches this cell and does its work when the cell contains the specific word. Just make sure to clear the cell quickly so it doesn't run twice.