onChange trigger may not fire under the following conditions. Do you know the cause and the remedy?
Check the checkbox in the spreadsheet
Select Spreadsheet list
I just created it like this.
Create an empty spreadsheet
Rewriting the default code
function myFunction (event) {
event.source.getActiveSheet().getActiveRange().setValue(false);
}
Create onChange trigger
Create a checkbox in cell A1
Check the check box (about once in 10 times, the process is not executed and it is not displayed in Executions)
Onchange not firing
Yeah creating an empty spreadsheet is kind of a problem since the new container won't have a trigger inside of it's project. There are no events that fire upon the editing of script in the script editor. Yeah creating an onChange trigger that's not covered either I would not hold my breath on that one. But if you must have it then make it a feature request. That's enough for me for one question.
Related
I've been struggling for two days now to allow the users from my organization to edit the protected pieces of a spreadsheet using only the script I made.
I've been through hell and how I made it work in the end is, I made a fake hidden sheet where the data is stored, and then onOpen, I copy-paste that data to the sheet that the user sees (that they cannot edit). I want to make the UX a bit better if possible, and I was hoping there is a way to force the onOpen trigger from the script. I know I probably didn't do this right, but I cannot spend more time researching, so I need to brute-force it now. (I've tried onChange triggers, I've tried setting permissions in my web app, using doPost, and my brain hurts, this is the first time I'm doing scripting).
TL;DR
Is there a way to refresh the whole tab from the script editor? I need to trigger the onOpen event without the user having to reload the page.
To "force" the onOpen simple trigger you have the following options
call onOpen
function respondToChange(e){
onOpen();
}
If your onOpen function requires the event object you will have to emulate it.
function respondToChange(e){
const event = {};
// add the required properties to event
onOpen(event);
}
change the spreadsheet locale by using setSpreadsheetLocale
Related
How to change Google Apps Script locale?
When a function is run by clicking a button or choosing a custom menu item, or through a simple trigger such as onOpen(e) or onEdit(e), it runs under the account of the user at the keyboard.
To trigger a function to run under another account that has rights to the protected ranges, you need an installable trigger.
I need to detect when the user changes the name of the spreadsheet (not the sheet).
Of the triggers, the timeBased and onChange are the most likely to be the solution. I would prefer not to use timeBased as the max frequency you can use is once per hour (in an addon, which is my situation) and my use case is to update the database right after the spreadsheet name is updated.
onChange is a bit confusing, there is a changeType called OTHER that may work but there is no explanation of what events this triggers on.
Is there a list of events that trigger OTHER anywhere?
Would this work for a spreadsheet name change?
Is there another solution I haven't found yet?
Since your using an add-on, your best option might be the onOpen function. It won't trigger when the name is changed, but will when the spreadsheet is open. This might be a good compromise from the time trigger.
After testing this with the onChange trigger, I've verified that it does not fire when changing the spreadsheet name.
I've created this feature request with google, hopefully they will add this event to a trigger or create a new one
Currently there is no way to capture the spreadsheet name change, you have to go about it by checking the name during onOpen, onEdit, onChange trigger events. Not ideal at all and leaves the potential for the name change to not be captured by the add on/apps script code for some time if ever.
This is a trigger and script coded directly into the form rather than on the spreadsheet that gets the form, I'm not sure if that is why this isn't working or not.
The trigger is set:
Owned by: Me
Last Run: null
Deployment: Head
Event: From Form-On form submit
Function: sendEmailFromSheet
Things I've tried: having the person who originally owned the form create a trigger yesterday, me create one today.
Filling out the form DOES result in the answers on the result spreadsheet, but it doesn't fire this onSubmitForm trigger, and I have no idea why. Again, it isn't that the function is failing but that it isn't firing at all.
Given that the function label is 'sendEmailFromSheet' then I suspect that its waiting for the sheet to receive data. But because the function is in the form it will never run because your trigger references the form and not the sheet.
If you can show us the code behind it then we might could tell you better. But without knowing that part then I think that is the mostly reason.
The "fix" (test_onformsubmit) code you gave, I have to manually run it every time there is new data in the spreadsheet. I was wanting it to automatically send the pdf to email when Form is submitted. Is there a way? Because the manual way runs the code exactly like its supposed to, but I want this as an automatic event so I don't have to do anything.
See parent thread of original problem/question
Read Understanding Triggers. This function is an Installable Trigger, so you need to set it up to run when a form is submitted. It's easy - I would have thought a Forms tutorial would have walked through it.
In the Script Editor:
Choose Edit > Current project's triggers. You see a panel with the message No triggers set up. Click here to add one now.
Click the link.
Under Run, select the function you want executed by the trigger. (That's onFormSubmit(), in this case.)
Under Events, select From Spreadsheet.
From the next drop-down list, select On form submit.
Click Save.
From this point on, the function will be triggered whenever a form is submitted to the spreadsheet.
If you plan to share your script, each recipient will need to repeat these steps.
As an aside, you should change the email setting in your script, so it will work for ANYONE.
var email_address = Session.getActiveUser().getEmail();
I'm playing with triggers.
In a spreadsheet, I have a function that inserts a date in a new row. Runs as a clock trigger every minute. Works fine if the spreadsheet is open or closed (loving that part).
In a standalone script, I setup a trigger for onEdit in the above spreadsheet. All it does is email me the e.value.
It works if I'm in the spreadsheet and just type in some characters in a cell.
What I'd "expect" is each time the date is inserted by the first trigger, it would trip the onEdit. Alas, it does not. Even if I run the insert date function outside of the trigger, my email onEdit trigger does not fire.
Any thoughts?
I ask, as my goal was to have scripts updating a sheet that kicked off an onEdit event.
Jim
UPDATE #1:
Maybe this code example helps?
I simplified this to one script inside a spreadsheet container. Each function works fine on their own and the second works if I do any interactive changes to the spreadsheet.
This is just exploration, so please ignore quota's or the utter nonsense of what I'm testing here :-)
insertClockTrigger: runs every minute, works fine
installableOnEdit: never triggers as an installable onEdit when insertClockTrigger fires
I'm curious why what looks to be an edit done by the insertClockTrigger is not firing an onEdit trigger. Do triggers not trigger triggers (could not help myself!)?
function insertClockTrigger() {
var sheet = SpreadsheetApp.openById('spreadsheet-ID').getSheets()[0];
var nDate = new Date();
sheet.getRange(sheet.getLastRow()+1,1,1,1).setValue(nDate).flush;
}
function installableOnEdit(e) {
GmailApp.sendEmail("some#email.com", "Test trig2", "FISH: "+e.value);
}
Call the onEdit function from the clock trigger function.
Also, you might hit a quota issue with running such a trigger every min.
Think I found an answer.
Henrique Abreu posted:
http://productforums.google.com/d/msg/apps-script/HmBg0p7dOZ8/xMb6wfcfvVQJ
Yes, "on edit" events are not called when the changes are made from
the code, they're are called only for manual changes. This behaviour
is intended by design, as to avoid unnecessary recursion problems.
Also, it just too easy to just call your desired on-edit function
directly from your code. i.e. when you script that inserts a new
ticket does so, it can just plainly call your "fundEmail" function,
there's no need for another trigger.
onEdit should be renamed to onUserEdit. It doesn't receive system changes, only direct user edits.
I haven't tested this, but onChange may do what you want.