onChange Function not triggering when sheet is changed by another script - google-apps-script

I have a app script Web App with a calendar, the calendar gets it's data from a master spreadsheet. A user can click on the item (on the calendar) to change the data: such as date, time, what they did, what happened, etc. These changes gets submitted to a "dump" spreadsheet, and then those changes are supposed to get copied over to the master spreadsheet.
The issue is, when the web app form is submitted, the dump sheet is being changed by a script. This causes the onChange trigger on the dumpSheet to NOT run, and in turn the submitted data is not being copied over to the master sheet.
How can I work around this issue? I could do a Time driven trigger to submit the changes, but then the issue is that the user would have to wait to see his changes in the web app calendar. And I would be running the function unnecessarily, wasting my app script quota.

Related

Sheet embedded function -only works for spreadsheet owner, not "anyone"

Long time learner/lurker, first time poster:
Howdy folks-
I've deployed a web-app that runs as the user (not me). It creates a form and a linked-spreadsheet in the
users drive with "anyone" access and "edit" permissions.
Bound to this spreadsheet is an installable "onOpen" trigger that creates a UI menu item to the right of the
spreadsheet's "help" menu, and on that menu item is a function that manipulates the data in the spreadsheet.
That all works properly -for the user who created the form+spreadsheet.
Here's my problem:
The use-case for my web-app is to allow someone to create the form and spreadsheet then hand it off to
someone to use the form and view responses and tally votes via the spreadsheet.
Because the spreadsheet's access is "anyone", the person to whom the spreadsheet is handed-off can EDIT
the spreadsheet, but the "onOpen" trigger never fires unless and until the opening-user signs in.
Then, the next problem:
Assuming the handed-off person signs in and sees the new UI menu item, they cannot successfully RUN the
function on the menu. It appears only the user that originally created the form+spreadsheet is the one
who can run the funtion from the UI (addMenu) menu.
Questions:
what's the best/recommended way to detect a user is not signed-in ..so I can present a modal alert
when the spreadsheet is first opened? The goal is to alert the user that if they signed-in, they would
see the menu item by activating the installable trigger.
Can a modal alert be presented when a user is not signed-in? I'm not sure how that code would be triggered
too, as it's likely the same problem as not triggering the onOpen UI-addMenu code
what's the best/recommended way to set the permissions for the spreadsheet's (embedded) menu-function -so
the user opening the spreadsheet can run it vs. only the original creator of the spreadsheet.
web-app example: https://forms.fattm.org/contest-form-creator
(several forms can be created, but the problem described above relates only to the form
named: CONTEST BALLOT (Item 1179) ..so you can un-check the others before hitting submit)
Thanks for your time/thoughts/help!
Welcome to SO!. The script you have that creates a custom menu using onOpen is a container-bound one, bound to the spreadsheet you are sharing. However the onOpen function is a trigger function which must be installed first (for each user that wants to use it) There is no way to get around this, unless you publish an add on, in which case the users would still have to install the add on the sheet you are providing.

Having trouble with Google script authorizations, need help remotely updating many (dozens of) Google Forms

I'm setting up an event to encourage people to stay physically active. Once someone registers, they are sent a copy of a Google Form/Sheet template that they will use to create their team. Each member of the team will enter their name, select the activity that they performed and record how long they performed it. The log Form should automatically update to list these names and activities so that the users can select from this list later rather than enter them again. The linked Response spreadsheet has charts comparing the team members activities to each other. I have a 'master' spreadsheet that imports the data from all of the team's response sheets and compares and displays them (on Google Slides) for the participants to view.
The problem that I'm having is that I would like to be able to programmatically authorize the Log Form auto update so that I don't need to do it manually every time a new Log Form is generated.
Things tried:
1) Form ranger - the Form ranger configuration can be setup in the form template, but is lost once the template is copied, requiring me to reconfigure (and authorize) the Form Ranger each new form as it's generated.
2) Use Google script to update the form with an onFormSubmit trigger. Process is working but I need to manually setup the onFormSubmit trigger, the first time, in each new Form/Spreadsheet in order for it to continue automatically afterward.
3) Updated the manifest file to include: "oauthScopes": ["https://www.googleapis.com/auth/spreadsheets.currentonly"] When I tried this, the script generated an error indicating 'You do not have permission to call SpreadsheetApp.openById'. It began working again after removing the '.currentonly' but still required me to manual authorize it for each new Form/SS.
4) Added
/**
* #OnlyCurrentDoc
*/
to the script. This added several seconds to the script runtime but, other than that, I didn't notice any change. Still required me to manually authorize.
5) I've tried using simple triggers (onOpen and onEdit) to detect changes in the Form's linked Response sheet, but found that these triggers are not activated with the form's automated sending of data to the spreadsheet but rather when the USER manually performs the operations on the spreadsheet. So still no automated updating.
Do you have any other ideas? I appreciate any assistance that you can provide.
Thanks,
David

Why does onFormSubmit trigger fire twice sometimes after a user submits their quiz?

I have a Google Forms Quiz linked to a Google Sheet and an Apps Script 'From spreadsheet - On form submit' trigger linked to an Apps Script. Intermittently, this trigger fires twice causing a problem for my application.
I thought multiple versions of my AppScript may have inadvertently been deployed in parallel. But I have made sure to delete all other versions and 'Deploy as web app...' using a new version number with 'Only myself' having access to the app.
I have also tried using a lock in the script to limit execution to one at a time and have tested whether double-clicking the 'Submit' button on the Google Forms Quiz will cause this behaviour.
I would expect the trigger to fire once per Google Forms Quiz submit button press.
In the end the solution to this problem was to handle the spurious double OnSubmit events by calling SpreadsheetApp.flush(); to ensure that my spreadsheet changes were committed before processing any second firing of the OnSubmit event. In this way my application was able to handle the issue.

Google Sheets: How to access responses tab after creating form via script

My Google Sheets script programmatically creates a form, and then attempts to change the name of the Form Responses tab that was added to the spreadsheet. The form is created fine, but I can't seem to access the responses tab, at least from the same function. It appears that the Sheets object is not getting updated with the new tab.
However, if I run a separate function later that tries to access the responses tab, this works fine.
Any suggestions for how to get the Spreadsheet to update and reflect the new set of Sheets? I was hoping I could use something like a setTimeout but doesn't sound like that is available in Google script. A time-based trigger wouldn't work because I want this function to be able to happen quickly (not after a minute).

google spreadsheet script - permissions to protect sheet

I have the following spreadsheet:
-Template (protected)
-Current working sheet
-older sheet (protected)
-even older sheet (protected)
This spreadsheet is shared with a few users, and they have write permission in the "current working sheet". Once it is considered completed, I created a button in the "current working sheet" to which I assigned a script. When ran as owner, the script:
renames the "current working sheet" to "something older"
protects the "something older" sheet
duplicates the "template" sheet as a new "current working sheet"
It all works fine wen ran by owner, however when the button that triggers the script is pressed by one of the users whom I shared the document with, then step (2) fails, meaning the sheet can't be protected. I suspect this is a permissions issue but I am a complete newbie in google scripting, so I am asking for some help here. Is there any way to make sure that no matter who presses the button, the assigned script is ran as owner?
thank you
You cannot have a button which the user at the keyboard clicks invoke a script which runs as the owner. See the doc about authorization here. That said, what you can do is have the button set a value or update a record which a script on a time based trigger checks. A flow like this would work well:
User updates the working sheet
User clicks the I am done! button
Script adds a row to a hidden sheet called Things for scripts to do which indicates that as of now, the working sheet should be archived
Every hour, a script checks Things for scripts to do and updates sheets and permissions as described above