Getting an error when creating a simple Apps Script trigger - google-apps-script

I have a new Forms response spreadsheet that I'm trying to trigger off. I don't make changes (yet) to the default myFunction() method and try to add a trigger. The first field tries to auto-populate forever, spinning and spinning, while I get this error message:
You cannot create a trigger without a target function, please add functions to the attached script.
I've done some online searching, and there are very few similar issues out there, and zero solutions for me.

Before creating a trigger either manually or programmatically you should save the Google Apps Script project.
Tip: Replace the default project name by something descriptive otherwise you might eventually end with a lot of "Untitled project".
One advantage of doing this programmatically from the script editor is that when clicking on the Run button if the script was not save at least one time it will prompt for the project name and save the project.
Resources
https://developers.google.com/apps-script/overview#your_first_script

ScriptApp.newTrigger("must have a function name here not a function declaration")
function imanoob(e) {
Logger.log(JSON.stringify(e));
e.source.toast('I am a noob');
}
Copy both functions into script editor and save them.
Run the second one first and then edit any cell on any tab or sheet.
function createImANoobTrigger() {
if(ScriptApp.getProjectTriggers().filter(t => t.getHandlerFunction() == 'imanoob').length == 0) {
ScriptApp.newTrigger('imanoob').forSpreadsheet(SpreadsheetApp.getActive()).onEdit().create();
}
}

Related

How to log event in Google Apps Script

I'm trying to trigger a function on a cell in a particular column being edited. I'm trying to work out what parameters are available for me to work with, but I can't seem to log them. I have created the following which I've taken from here: https://developers.google.com/apps-script/guides/logging#stackdriver_logging:
function onEdit(e) {
console.log(e);
Browser.msgBox('somethign was edited');
}
If I edit a column, I get the pop-up, but there's nothing logged in the Execution Log. How do I get the data to show in the Execution Log?
Here's how to get the parameters:
function onEdit(e) {
Logger.log(JSON.stringify(e));
}
After copying this into the script editor and saving it with no errors. Just go to any page and perform a manual edit. Then come back to script editor and view executions you can access it from the menu to the left in the new editor. Read the info section of the top entry and it should contain the JSON of the event object

How do you use Logger.log(e) with Google Forms? (Using the NEW Apps Script Editor)

In the legacy Apps Script Editor you could log e from Google Form submissions.
How is this done in the NEW Apps Script Editor?
function myFunction(e) {
Logger.log(e);
}
To make sure we are on the same page:
If you are going to run a function like this in any editor you are going to get null:
because simply e is not defined and it is only returning data upon trigger executions of this function. But this function is executed by some events depending on the type of trigger you are using. Therefore, you are not going to see anything (that is not null) in the console if you manually execute this function.
After the function is triggered by specific events:
In any editor again, you can go to the execution page to see the details of the execution. In the new editor, you go to Executions:
and you can see a list of all the executions of this particular function. For example, if your function is a simple onEdit trigger e.g. onEdit(e), you will see this upon editing a cell in the spreadsheet:
You can also see the type of the execution, whether it was executed by the script (Editor) or by a trigger (Simple Trigger).
But anyway, trigger functions are not supposed to be executed manually. As the name suggests, trigger functions are triggered upon events. It wouldn't make sense to use a trigger function and need to manually execute it. It would be a regular function then.

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"
}

Google App Script onFromSubmit Trigger doesn't trigger

I have a spreedsheet, with a sheet feed by a Google Form. unfortunetly, I need special features, like conditional formatting, and data added by google form are added in a new line without format.
So, I did an other sheet, with the same columns, and I had to copy paste from the first one to the second one. I'd like this copy to be automated. So I tried to do it using Google App Script. But it seems like I did something wrong with the trigger.
Here is the code :
function creerTrigger()
{
ScriptApp.newTrigger('deplacerCommande').forSpreadsheet("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").onFormSubmit().create();
}
function deplacerCommande(e)
{
var id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var feuille = SpreadsheetApp.openById(id).getSheetByName("Commande");
feuille.appendRow(e.namedValues);
}
When I run deplacerCommande() from Google App Script, the messageBox is displayed, but not when a form is submitted, so I don't think the trigger works.
Yes, I created my trigger. I did it by running the function creerTrigger() from Google App Script. I also tried deleting it, then changing the version, then recreating it, because I read it can solve the problem, but it didn't.
list of trigger
It's in French, but I'll translate the value for you :
deplacerCommande is the name of the function called by the trigger.
the second value means From the spreedsheet.
the third means sending Form
The solution was simple. The trigger triggered, but the appendRow function raised an Exception because namedValue is an object, not an arrow. using e.values instead of e.namedvalues solved the problem!

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.