SQL statements in onOpen trigger in Apps Script not working - google-apps-script

I have written a logic using Google Apps Script which will verify the logged in users email id with the one present in the database.If the email is present in database, populate the logged in user's email id in one of the cell.This should be done without manual intervention and onOpen event of spreadsheet and should also work if someone tries to make a copy of the same spreadsheet.
If method does not contain SQL statements then, onOpen() gets executed in both
in the original sheet
when someone makes copy of it,
If method contains SQL statements then, onOpen() gets executed ONLY
in the original sheet (will not be executed if someone or even I, myself make copy of
it).
Please confirm,
if my understanding is correct and SQL statements does not work in
onOpen() trigger when someone makes copy of the sheet.
What could be the alternative to implement this functionality that gets executed in original sheet and in the copy(made using option Make a Copy)

Don't name it onOpen, use something else so it runs under your userId and not each user.
Doing that will break the copy since you will need to install the onOpenTrigger manually

Related

Apps Script Making onEdit and onOpen triggers work only for the current user

Probably the answer to this is simple but I am having a tough time getting my head around it.
I have a spreadsheet used by many users. I want custom onEdit and onOpen functions to run only for the user who triggers then. For example - if person B opens the spreadsheet, then only his custom onOpen trigger should run, not the custom onOpen triggers for the other users. Likewise if the user edits a cell only his custom onEdit should run.
Currently how I have set it up everyone has his own triggers but they all run the same script every time there is a trigger by any user, meaning the trigger runs 4x for no reason and it's slowing down the spreadsheet, and it also wastes script time usage for the other users who are not opening or editing the sheet.
You can have separate functions for each user and have code dynamically check for current user and run the function of current user only.
See here for getting current user. Link
Assuming the functions are the same but you just aren't wanting it to run for some users, you could use PropertiesService to store a user property to identify if it should run or not. You have to make some UI method to set the property and then you would check for the property at the beginning of each of your functions. Technically the functions would run but you could return out of them if you don't want to finish the logic.

How to allow users to run a macro which edits a range or sheet they don't have 'edit' permissions to?

I have made a macro that takes a set of input data, transfers it to a master data table, graphs trend lines of the data, and then clears the data entry sheet. All sheets are protected to avoid employees from changing or deleting data that has already been entered and only the cells which require data entry are available for editing by the employees who I've shared the sheet with. The macro works well if I try to run it but if another employee attempt to run the macro they get an error stating they don't have permission to edit certain ranges.
When I wrote the Excel macro for the same task I was able to unlock the sheets up front, transfer the required data, and then re-lock the sheets but can't figure out how to replicate this type of behavior in Google Apps Script.
The Protection class on the Google Developers page did not prove to be helpful, since I kept getting an error saying the function protect could not be located in the Object spreadsheet. I also tried the addEditor function without much luck. This is my first Google Apps Script and I've been trying to learn as I go but this project is proving difficult.
Thank you to #Tedinoz for helping me realize you can rename the onEdit function so long as you designate 'e' as a function input. I was able to use an Installed Trigger on the sheet set to run RenamedonEdit(e) whenever the sheet is edited.

Google Sheets: installable trigger seems to disable simple trigger script

I have a Google sheet with a function that enters a timestamp in a column if the column to its left is updated. It is triggered by the simple onedit trigger, and it only processes for a small set of cells in the sheet. It has worked fine for months with many people.
Then I added a script to do some data cleanup on a separate tab of the same doc. I use an installable trigger, so that I can schedule the cleanup to run once each week.
Unfortunately, while the scheduled installable trigger cleanup routine now works, the original onedit simple trigger timestamp routine does not. To be more precise, the timestamp routine works for me, the author of the script, but it does not work for anyone else.
Any idea how to address this?
Thank you!
the timestamp routine works for me, the author of the script, but it does not work for anyone else.
Issue can be found in Stackdriver logs. Go to Script Editor>View> Executions>Clear all filters. Check the failed logs. Most possible reason is You restricted access to those people. If the "user at the keyboard" is not permitted to do something manually, He cannot do that through scripts either. Another possible reason is Anonymous edits cannot show popups/dialogs; If you added such code to your function, Your function will fail.
Possible Solution(s):
Provide the necessary permission or
Create a installable onEdit trigger, so that the function will run under your authority. In this case, if editors are able to access your editor, they may be able to execute scripts as you. Protecting all sheets with unprotected ranges may help this, but does not provide complete security. Reducing authorised access scope might also help
Another way to do this is by creating a service account and use oauth library to access the sheet.Sample here and here.

Run script depending on last editor

I have the following issue:
'I have a Google Sheet that contains copy information that is used in an app, when the app is modified by the copy person, I would like tor receive an email so I know to update the strings used inside of the app.' There are other non-automatic ways to achieve this, but the copy person knows google docs really well, and sometimes doesn't remember to notify me of changes, or 'insert other reason here'.
At any rate, I've created the function that generates a set of strings I can paste into Xcode and run the app. The only manual part is now running the function and getting the output into Xcode. The output is stored in a separate sheet of the same document. (Newest copy at the top).
The technical question / point of this post, how do I take this to the next step of emailing me so I can make the changes to the Xcode strings file?'
I am open to suggestions / other thoughts as I'm trying to iterate on this and I feel like once I get this script to run whenever the copy person updates the sheet, I will be good for some time.
Here's where I'm at:
I tried searching through Google's documentation, SO, and other sites, and found the following:
How to access revision history of a spreadsheet using GAS?
https://code.google.com/p/google-apps-script-issues/issues/detail?id=394
stackoverflow_com/questions/10584528/documentlist-api-and-gas-how-to-marry-them
sites_google_com/site/scriptsexamples/new-connectors-to-google-services/driveservice
productforums_google_com/forum/#!topic/docs/zty8X8Pkwbs
(the above are not links as my reputation is insufficient to post more than 2 links at this time).
All of these sources suggest there is a way to potentially solve this problem, but it's quite a task for something as simple as seeing 'is the last editor someone who's changes should cause this script function to run'.
My thoughts were to have my function run whenever the sheet is edited, and so long as the last modifier is the copy person, then run, otherwise, do nothing.
I may also be missing a small detail/quirk that could prevent what I want to achieve from even being possible. Hoping to find some insight / answers through this post.
A quick solution would be to have your 'copy person' create a bound script in the spreadsheet with something like:
function myFunction() {
var user = Session.getActiveUser().getEmail();
if(user =='user-email'){
MailApp.sendEmail('destination-email', 'subject', 'test trigger: ' + user);
}
}
Run the function just for testing and to grant permissions.
Then he will have to create a Trigger by going to the menu "Resources -> All your triggers".
Then click on "No triggers set up. Click here to add one now."
Select the name of the function.
Instead of time-driven, select "From Spreadsheet"
Then select the event "On edit"
and save the trigger.
Now, the 'copy person' should modify the spreadsheet just to test that the email is sent.
Keep in mind that with this approach, every single modification made by the 'copy person' will trigger the event and you will receive a new email.

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.