Script triggering multiple times after first execution - google-apps-script

This scenario is within my company, with google accounts.
I have a google form which of course populates a spreadsheet. My google apps script basically creates a pdf document from the form content and sends it by email to an HR email address. The script also inserts a link to the created pdf in the line on the spreadsheet that has just been populated by the last form submit.
Everything works fine, except the script is triggered automatically a second time after a while creating a second empty pdf, because the form hasn't really been submitted again. It might happen immediately, or after a few minutes, even it once happened one hour after the first execution.
This is owned by a service account. Inside the same account I have another form/spreadsheet/script/trigger which does almost the same and it works just fine, being triggered only once.
The only difference is that the form that works fine, collects the email addresses automatically, so requires a user to login to google.
The faulty one, does not collect the email addresses, it just has the fields "full name" and "username" instead for the person to identify manually.
The executions of both triggers of course, end as "completed".
I been erasing and recreating triggers for two days. Even created the faulty form from scratch, because originally I had duplicated the good one and then modified it accordingly.
You can see in the image, the execution at 6:04:49 is a "ghost" of the one one second before. (of course I cannot do all the form filling process in just 1 second)
Then another real execution at 5:33:42 and its ghost 4 seconds after that.
Then the 3 executions at 5:39:07 seem to be ghots of previous ones done at different times.
Any clues?? Thanks a lot!

Related

Google Admin Directory SDK Users.list() Returning Old, Not Current, Data

I am working on a project for my company, that well is kind of complicated. I am not sure if I will be able to explain it as well as I need to.
Basically, this is how it works:
A user fills out a Google Form that represents a user in our company, either new or existing
My Google Apps Script gets the last form response, triggered on form submission
That script then relays that last form response data to the custom schema fields I set up on Google Admin
Once the user is updated, I grab ALL the users in the domain and paste them into a google sheet
The spreadsheet represents our company roster. We originally just tried to maintain the data solely in a spreadsheet, but it became a mess.
To solve this, we introduced the google form, which can validate all data. Furthermore, our data is backed up to Google Admin (within custom schema). The spreadsheet is solely for display. It is bulletproof. Upon refresh, it pulls the data, and re-writes the sheet.
However...
I am noticing that after the form is submitted and the user is updated in Google Admin, and then all the users are pulled from Google Admin, the user that was updated isn't showing as updated in the spreadsheet. However, if I refresh the page, then the correct data shows up (I have another script that triggers on spreadsheet open, literally the SAME EXACT code that runs after the form submission). Why does that code return the proper data on refresh, but not when run directly after the form submission and consequent user update in Google Admin?
I really am dumbfounded and having an incredibly hard time explaining this because it does not make any sense. My boss and I both spent the entire day scratching our heads on this yesterday.
Do you guys have ANY ideas?
Is there a cached response, possibly accounting for the non-current data from Google Admin?
When I used the Admin Directory SDK API Explorer, the user modified shows as current. I just don't seem to be getting that same response when my script runs.
I am so lost. I spent 2 months developing this system. It worked in development.
Any and all suggestions are appreciated

Google Apps Script problems running as different user

I am having problems with script execution from within my company's Google Workspace account.
I am the developer and I have no problem executing any of the scripts with my account; all my scripts run all the way to the end.
Now, there is this one script through which I present the user with a form in a sidebar for him/her to input some data, and then I do some stuff with that data. As mentioned, no problems for me. However, when I try to run it as another person from the company (which isn't just another editor but the superadmin account for the company's Google Workspace), after the initial script permission-granting dialogues are done and dusted, the script only runs halfway through and hangs. It actually gets the sidebar up, allows the user to input data and also to submit some data (I need to check if it submits all of the data). I know for a fact that some of the data does get submitted since one of the things the script does is fill up a particular cell with the data of one of the inputted fields, but it hangs there forever... How could that happen? I thought it might be a problem with the HTMLService, but as some of the inputted data does go through and gets added to the right cell..., I'm at a loss.
Has anyone encountered anything alike?
As it turned out, after I banged my head around for hours, it was a matter of dealing with protected cells.
Even though the script is set to execute as me, as soon as the script attempted to write into a protected cell whenever a different user executed it, and where I had set myself as the only user with write access, it failed.
Thanks go to #Kessy for having a look into it and suggesting a possible way to see the problem.

How to get a notification if Google Script's "My Executions" shows that a script hasn't run in specified time

I have a Google Script running every 5 minutes. It works except randomly there are sudden crashes and the script/trigger won't survive that. So, for many different reasons, the script stops running after some weeks of continuing runtime. At this point, I would need a notification.
How can I get a notification if a function (script) hasn't run in say 2 hours? Doesn't matter what the technique/notification is, but probably email would be great for the notification. I don't think the solution can be in the script code itself, because the script randomly (every few weeks) crashes at Google's side and there's nothing I can do about that.
Google Apps Script installable triggers automatically set an email notification. You could edit when it should be sent (i.e. send immediately) and you could add more notifications. NOTE: This can only be done manually.
The above will work for "normal" failures but it there is a service outage or other platforms failures the notification might not be sent.
One option is to log the executions somewhere then set a second trigger to check that the first trigger ran every time that it should do it as expected. NOTE: The failure that prevented that the first trigger run might also prevent that his second trigger run i.e. a service outage so you might want to set other monitoring measures accordingly the the severity/priority of this failure and your project budget.

Same script working differently in 2 script editors

Premise: I have not a problem with algorythms or syntax or method and so on, for this reason I apologize not to report scripts or piece of it.
But I am in a mistery.
I have a Form for booking medical appointments, filled on line by patients, that feeds a Sheet where appointments are reported. The sheet is used only by the doctor to have the view of his engagements. The form suggests to the patients a list of days and hourly slots, by a ListItem of open time intervals (i.e.: Wed March, 3 hh 11 - 11:20 am ). The sheet (triggered on submit of the form) drives the automatic update of the List Item of the Google Form, to be proposed to the next patient requiring an appointment.
One function in one script for all, it works perfectly.
Now, I would like to give the doctor the possibility to manage the table of the appointment (the sheet), editing the cells of the table itself. But so doing the List Item of the time slots in the Form has to be updated every time the table is edited (triggering on edit).
So, to the aim:
1) I took the piece of my script in charge of updating the Form, which became a new function in the same script editor as I developed the primary function. The new function works perfectly when it runs from the script editor of the Form.
2) I copy INTEGRALLY the above script in the script editor of the sheet and...a for loop does not work as expected, in any way it does not work as it does in the Form environment.
Note: every files are opened by ID and no methods are invoked on "active" file.
Thanks
Posting for documentation purposes; OP found the answer, as per their comment:
And I finally solved. It was a problem arising from the Timezone management. In facts, Guide says: "New scripts default to the owner's time zone, but the script's time zone can be changed by clicking File > Project properties in the script editor." I did, problem evaporated and I found my peace.

Record Time taken to complete a google form

I am trying to record the total time taken to complete and submit a Google form.
My logic is simple that the following code would record and put the timestamp as a multiple choice option.
Then upon submitting the form, we get a time stamp anyway but along with that, we would get the initially recorded timestamp as an answer to that question.
This is my cute little code:
function initial() {
var form = FormApp.getActiveForm();
form.getItemById(1589920082).asMultipleChoiceItem().setChoiceValues([new Date()]);
}
I have set the trigger as OnOpen but surprisingly, it does not renew the timestamp every time I open the form. Theoretically, it should record the time when the form was open but I think I am missing something here.
On the other hand, if I change the trigger to OnSubmit, it starts recording fresh timestamps every time I submit the form. But I don't want that as we get submission timestamps anyway. What I am trying to record is the time the form was open.
I know this question was asked a year ago, but here is a workaround I just came up with.
Have 2 forms! The first form can be a disclaimer, instructions, etc. The second form is the original form you intended for the users to fill out. Turn on email collection for both, so you can know which user submitted the test.
Get the sharing link and in the settings of the first form add the link in the confirmation message:
Then link both forms into a spreadsheet and you will see the time stamp of each submissions. From there you can easily do a calculation to determine the duration.
Unfortunately you cannot have any code executed when user opens form for filling in, as onOpen trigger is not supposed to be run in this case:
https://developers.google.com/apps-script/guides/triggers/#onopen
The onOpen() trigger runs automatically when a user opens a spreadsheet, document, or form that he or she has permission to edit. (The trigger does not run when responding to a form, only when opening the form to edit it.)
There is an open feature request in Google Issue Tracker to introduce some kind of onOpen trigger when form opened for response, you can "star" it to make it more possible to appear: https://issuetracker.google.com/issues/63985842