Run script depending on last editor - google-apps-script

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.

Related

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 Apps Script, calling function once when document is copied

I have been playing around with Google Script Editor and I've gotten heaps of use out of it.
The next task that I am looking at is automatic formatting and text insertion/replacement when copying a Template.
An example use case is as follows: Within my organisation I have submitted a Doc to the template gallery. When creating a copy of the template I want it to automatically insert today's date and the current time (rounded to the nearest hour).
This is a question about the Triggers. The text replacement bit is easy and done. Not to mention this is just one of the basic use cases, I'll be attempting many more similar behaviours with things like timesheets and the like.
The problem that I am running into is that I can't seem to get the triggers to work as I'd like them to.
2 of the Triggers that I thought I could try and use: onOpen(e) and onInstall(e).
onOpen(e), though it works, it works "too well". That is, it also replaces the text on the original template as well, proving a nuisance when updating info in these templates.
onInstall(e), I thought this would work as creating a copy of the Doc also "installs" the script as well. However this function doesn't seem to run at all.
Any ideas about getting a trigger to happen once and only once when a Doc is created from a template?
Cheers,
Bricktron
First of all, Trigger onInstall(e) works only for Add-ons.
Now coming to onOpen(e), in my opinion you can use Google Apps Script Property's Services to store one flag which helps your code identifying whether this file has been opened or not.
So for very first time onOpen(e) runs, assign property eg: propertyService.setProperty("opened","TRUE") and next time you can check by accessing the property whether it has been already "opened" or not.
Example:
var openedFlag=propertyService.getProperty("opened");
if(openedFlag=="TRUE"){
//Document has been modified
//Do not run the modifiable code again
}else {
//First time
//Edit the file
//Set the propertyService to "TRUE"
}

Send an e-mail if a cell value meets a given criterion

I'm working on a scoreboard automation process using Google Sheets, but I've come up with a little problem. I tried searching here but the question has been treated in a unclear way to me.
What am I trying to do is quite simple:
I want an automatic e-mail to be sent to a specific person IF cell value > X (threshold). I already know I need to use Google Apps Script for that, but I haven't found much interesting code lying around yet so I was wondering if you guys had an idea how that would work?
I tried something based on this thread with no success: How do I make a Google Sheet script send an email when a specific cell's value changes?
#Yvan1401,
You can use a script like this along with an installable trigger like onEdit or onChange to accomplish what you wish. To set up the installable trigger follow the steps below (found here):
From the script editor, choose Resources > Current project's
triggers.
Click the link that says: No triggers set up. Click here to add one
now.
Under Run, select the name of function you want to trigger. Under
Events, select From spreadsheet.
Select and configure the type of trigger you want to create (e.g. on
Change or on Edit trigger).
Optionally, click Notifications to configure how and when you will
be contacted by email if your triggered function fails.
Click Save.
Please see comments in code for portions to change.
function SendEmail() {
var ui = SpreadsheetApp.getUi();
var file = SpreadsheetApp.getActive();
var sheet = file.getSheetByName("Sheet1"); //Change as needed
if(sheet.getRange(5,1).getValue()>10){ //change row and column in get range to match what you need
MailApp.sendEmail("xxxxxcxxx#gmail.com", "subject", "message");
}
}
function sendMail() {
if (SpreadsheetApp.getActive().getSheetByName('Scoreboard').getRange('AL2:AL1000').getValue()<2.70) return;
MailApp.sendEmail("yvan#********.com", "******** new potential candidate available ! ", "Dear ****, a new candidate with strong potential is available for due dil !", {
name: "SOURCE NAME"
});
}
Here is an example of code that works the best and is the shortest. The thing is, I want it to scan only the cells that has changed (updated) within the column in question and not the old responses (data source is a form). What do you think i should do ? Because i don't want to erase the previous response within the same column.
edit: I use an "onChange" trigger.

Contacts not being collected from "Soccer Club Homepage" tutorial

I am new to google apps scripts and I am following the tutorial at https://developers.google.com/apps-script/articles/sites_tutorial and when I run the myContact function from step 1, no contact info is populated into my spreadsheet.
I have a group created called AZ_Pilot, and I have the myContact function set up as follows:
function myContact() {
var contacts = ContactsApp.findContactGroup("AZ_Pilot").getContacts();
SpreadsheetApp.getActiveRange().setValue(contacts[0].getPrimaryEmail());
}
The script runs with no errors, but nothing is updated within the spreadsheet. It's a bit embarrassing to be stumped at step 1, but here we are.
You are using getActiveRange() which is the range which is currently under selection in the spreadsheet. By default, it is the first cell in the spreadsheet. Maybe you have a different cell selected and therefore, the email might be written to a different cell. Try keeping the first cell in focus and running the function again.
Also, you haven't mentioned whether or not there are any contacts in the group you created :)
I was adding some additional logging information, and it just started working correctly with no change to the functional code. Not sure what happened there.

SQL statements in onOpen trigger in Apps Script not working

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