Email after Form Submit, Problem with Calculations - google-apps-script

I've currently got a trigger on form submit to run my code which pulls in the submitted data and emails a summary to the user. I need to perform calculations on the submitted data and provide those results in the summary email. I've been able to hard code this and I have a working script but I want to do the calculations on one of the sheets instead. I've got another sheet setup to pull in the form data and perform the calculations but it seems the problem is that when the form data is submitted, none of the other sheets are updated so if I try to pull calculations from the other sheets it will not include the latest entry.
I've tried getting data from other sheets but I don't think the other sheet calculations are refreshing to include the newly submitted data, or the code is executing before the sheets can refresh, possibly order of operations limitation. The desired goal would be to have a user submit the form, then have all sheets using that data pull it in and complete calculations, then send an email using the newly calculated results. Ideally pulling data from multiple sheets.
I'm thinking the "on form submit" trigger is not appropriate here, instead maybe I need to have an event occur on the sheet or sheets I'm using to calculate that triggers an email. Or does anyone know if there's a way to force all sheets to update and fields to calculate before continuing to execute the code?
Any ideas? Thanks!

I'm not sure if I got 100% what you're looking for but you need to know that if it's your function who is opening a Spreadsheet and writing data on it, you can't add any trigger.
Trigger can only run following a "human" action (onOpen, onEdit..)

Pl. try importrange in the target sheet/sheets to pull data from the form sheet.
You have to maintain "completed" in a seperate sheet.
If you see a new entry, do the calculation, send mail and update the completed field.

Related

Is there a way to set up Google Apps Script trigger to be based on the edit of one of multiple spreadsheets?

I want to send an email when one of three spreadsheets have been edited. I have set up a trigger in the UI that runs a function on edit for one spreadsheet, but how do I make it so it runs on edit for one of three spreadsheets?
One solution is I put the function in scripts for each file and set up separate triggers, but is there a way to do this without having three scripts?
When I say spreadsheet, I mean a google sheets file
If the information required isn't time sensitive, Create a time based trigger on a standalone script, which checks each of the three spreadsheet files every hour or so for modifications and sends a email, if modifications are inferred. It is possible to use PropertiesService
to store last modified date time and compare it to the current modified date time.
get last row of sheet and compare it to last row currently, if data is added in that format.
There is no way to make that a single on edit trigger, simple or installable, be triggered by the edits made on several spreadsheets.
One option is to use code to create multiple triggers to run the same function, one trigger for each spreadsheet.
Reference
https://developers.google.com/apps-script/guides/triggers
https://developers.google.com/apps-script/guides/triggers/installable

Macro to delete the old data

I'm really new to this and would love some help where I'm currently stuck. I've created a macro to clear a spreadsheet. I've tried multiple triggers of onedit and onchange and haven't had luck with my end goal. I'm sending data (about 10 columns and 100 rows into a google sheets via zapier. Basically, I'm refreshing the data by replacing the data. So, before the data comes in, I wanted the macro to delete the old data. However, the macro actually deletes the new data coming in once it posts. Any scripts or workarounds that could help?
From what I know about Zapier it works on timed intervals. You can have a script run on time intervals just before the time Zapier inputs information and delete the old info.
Another approach would probably be to have a intermediary sheet.(Lets call this sheet, "sheet1". The final sheet can be called "sheet2". When Zapier writes data to sheet1 it can trigger the onChanged event. Take the information in sheet2 delete everything, then post the new data to sheet 2.
Finally, Zapier supports webhooks. You can publish your script as a web app and have it do a get or post to the app which runs the delete function. To give it time you can use the delay that Zapier provides.
What you are describing sounds like the perfect candidate for the IMPORTDATA worksheet function. The function is entered in a cell, as are all worksheet functions, and it takes a URL which points to a CSV or TSV file. Once entered the data referred to by the URL is expanded out into the appropriate number of rows and columns. Seeing as the function resides in a single cell you would only need to update that cell when you wanted to change the data. This means that you would no longer need to use a .gs file to remove old data and could instead complete everything from within Zapier. I have answered a question similar to this here, I describe how this is done within Zapier. The only prerequisite is that the data you are using is in either CSV or TSV format.

Is there a script/code for Google Forms to pull data from a spreadsheet upon completion?

Looking to have someone fill a simple Google form, and then automatically give them a key from a spreadsheet. Then, upon the next completion of the form, it pulls from the next cell in the sheet. So each completion receives a new data point until it runs out.
That is totally doable though gScript. Here are the steps to get to where you wanna go:
Create your form
Make sure that the output is going to a gSheet where your keys also live
Create an onSubmit function so that your function (called onSubmit) is ran when a user submits to your form
Assuming your keys are all in a single column on one page, you could use MailApp to send them an email, and use
sheetName.getRange(sheetName.getLastRow(), 1).getValue()
to get the key you want to send, and after successfully emailing it to them, just clear the value like so:
sheetName.getRange(sheetName.getLastRow(), 1).clear()
Hopefully that helps out!

Update Google Forms field based on dynamic data from a Google Sheets spreadsheet

In a Google Form we have a pull down selection list.
The list of items may change from minute to minute. The master copy of the data is in a column in a google docs spreadsheet.
When a user accesses the form, we want them to see the latest copy of the pull down list as is current in the spreadsheet.
I don't see an obvious way to do this with triggers. onOpen, for example, only triggers for administrators of the form, not users. Is there a way I can keep this form up-to-date automatically?
This is my first time in google docs, but I'm very experienced in excel scripting.
The work flow needs to be a bit different than what you've described. Since there is no ability for Google Apps Script to interact with an end user accessing a Form, there's no way to react to that. However, you can react to changes in the spreadsheet, and update the form options in preparation for the next end user.
Depending on how the options get updated in the spreadsheet, you can use an installable trigger there, to react to an edit or a change.
If the form is associated with the spreadsheet containing the choices, you can get a handle on the form using Spreadsheet.getFormUrl(), then pass the URL to FormApp.openByUrl(). Otherwise you will need to provide the ID or URL explicitly to FormApp.openById() or .openByUrl(), respectively.
That spreadsheet trigger function can modify the available selections for the relevant question by using ListItem.setChoices() or ListItem.setChoiceValues().

How to generate an EditResponseUrl() for manually entered rows in a Google Form response spreadheet?

I have an spreadsheet linked with a form and made it as, all the user to edit the response after submit. And connected it with Awesome Table. When i insert a data through the form, the spreadsheet automatically generate the edit response url. this is done by gas. This isworking fine. But, what i want now is, how can i generate the response url for the data which i paste directly to the sheet from another sheet? is it possible? please help me with some suggestions or a piece of code.
Thanks in advance.
It's not possible to do this.
The forms services and the sheets service are two separate services. This is a simplified version of what happens when you submit a response to a form:
The form is submitted.
The forms service stores the response.
The forms service writes the response to a sheet.
When you use the EditResponseURL() method, you're getting the information from step 2), and editing it. This then causes the form to complete step 3) a second time, updating the sheet.
If you write the response directly to the sheet, you're skipping step 1) & 2). If the response is not stored in the form, there's no way to use the EditResponseURL() (As this is a forms only method).
Easy solution: Submit everything via the form, and do not write directly to the sheet.
More complicated solution: Write a custom form that allows you to pull the information in a range of cells from a sheet, update the information, then writes the edited information back to the sheet.
The second Sheet would need to "know" what it was looking for. The second sheet can retrieve anything it needs from the first sheet, but it needs information about what to retrieve. You would need to explicitly record information that matches some kind of key to the information that should be retrieved. The first Sheet could have information on what response is matched to what row of information. You could have an ID that was just sequential numbers, or you could have an ID (key) that encoded the user name, the date, the time, a random number, a row number, etc. if you need/want that. Your essentially designing a database structure.
It might help to define a flow chart of the process.