I'm having a hard time to get the id of the last submitted response
var form = FormApp.openById('SAMPLE FORM LIVE ID HERE');
var responseId = form.getResponses().getLastResponse.getId();
var ssResponses = wrkBk.getSheetByName("RESPONSES");
var numRows = ssResponses.getLastRow();
form.deleteResponse(responseId);
ssResponses.deleteRow(numRows);
I would like to delete the last submitted response in Google Form.
I have also sheet that is linked from my form.
So what I want to happen here is, delete both responses in Google Form and linked sheet.
Related
Sorry for the newbie question here; I'm trying to have the name of my response sheet match the name of my form - Right now its just named "form response (1), (2) etc. I have multiple forms that are landing on 1 google sheet, which is exactly what I want, just would love to be able to have the name of the tab be the Google Form name and not "form Response...."
From a previous thread i tried:
function writeFormTitle(){
//Get Form
var form = FormApp.getActiveForm();
//Get Form's Title
var formTitle = form.getTitle();
//Get the Write To Spreadsheet ID where the data will be written to
var destinationSpreadsheet = form.getDestinationId();
//Activate that Spreadsheet by the destinationSpreadsheet ID
var sheet = SpreadsheetApp.openById(destinationSpreadsheet).getActiveSheet();
//Find the last column and row
var writeToColumn = sheet.getLastColumn()+1;
var writeToRow = sheet.getLastRow();
//Write title to Spreadsheet next to submitted data.
sheet.getRange(writeToRow, writeToColumn, 1, 1).setValue(formTitle);
}
But with this its appending the google form name into the last column on EACH sheet.
Any insight would be great!
You are very close.
Replace:
var sheet = SpreadsheetApp.openById(destinationSpreadsheet).getActiveSheet();
with:
var sheet = SpreadsheetApp.openById(destinationSpreadsheet).getSheetByName("Form Responses 1").setName("new Name");
Or:
var sheet = SpreadsheetApp.openById(destinationSpreadsheet).getSheetByName("Form Responses 1").setName(formTitle);
I have looked at the Google Apps script how-tos, but it is not self-explanatory to a newbie like me.
I have also tried to adapt online video scripts to what I need and they always return errors.
I need help with how to enter the code below so it generates a link on the form response spreadsheet that shows the complete form with the answers that users submitted.
Below is the code I found on the Google Apps script, but I don't know how to enter it.
toPrefilledUrl() generates a URL for the form in which the answers are pre-filled based on the answers in this form response.
function addFormResponseUrl(e) {
// Get the Form response URL and add it to the Google Spreadsheet
var responseUrl = formResponse.getEditResponseUrl();
var row = e.range.getRow(1);
var responseColumn = 1; // Column where the response URL is recorded.
responseSheet.getRange(row, responseColumn).setValue(responseUrl);
}
In order to retrieve the EditResponseUrl and paste it into the destination spreadsheet next to the form response:
Write a function that retrieves the latest form response and pastes the respective EditResponseUrl into the destination spreadsheet
Bind the script to the destinaiton spreadsheet
Attache to the function an installable FormSubmit trigger
Sample script:
function addFormResponseUrl(e) {
//open the form by its id
var form =FormApp.openById("XXX");
var formResponses = form.getResponses();
// get the latest form response
var lastResponse = formResponses[formResponses.length-1];
//get the response URL
var responseUrl = lastResponse.getEditResponseUrl();
// get the sheet where the responses have been stored
var responseSheet = SpreadsheetApp.getActive().getActiveSheet();
//get the row where the last response has been stored
var row = e.range.getRow();
// Safe the response Url in the first free column after the form responses.
var responseColumn = e.values.length+1;
responseSheet.getRange(row, responseColumn).setValue(responseUrl);
}
Background:
I am trying to create a google form that will be filled out by people, and then send a second form to be filled by someone else (couple). The form response needs to be anonymous, but the responses will also need to be linked.
My solution so far - using a unique ID for linking (kudos -> How to assign a unique ID to a google form input?) then using the email inputted to send off a message with a pre-made link to the next form with the ID and then delete the email address from the sheet.
The Question
The next sanitation which I am stuck on - is removing the email address form the Google Form response. One way would be to use deleteAllResponses() but that will also delete the basic data analysis pre-done by Google that I intend to use (to save time on creating some of the graphs).
I tried to find a way to access the response object and delete/modify the email field, but was unable to achieve that.
Would appreciate the help.
function myFunction() {
//delete the last email that was added
function deleteEmails(Row){
sheet.getRange('B'+Row + ':B'+Row).clearContent();
}
//delete all responses - would like to change this to only delete/modify the email field.
function deleteResponses() {
var form = FormApp.openById('The form ID');
form.deleteAllResponses();
}
//get the data on the sheet
var sheet = SpreadsheetApp.openById("The sheet ID").getSheetByName('Form Responses 1');
var StartRow = 1;
var LastRow = sheet.getLastRow();
var Range = sheet.getRange(LastRow, 2, 1, 62)
var AllValues = Range.getValues();
//if the person disagrees to fill the form - dont send an email
if (AllValues[0][1] != 'disagree') {
var SendTo = AllValues[0][0];
var body = "Thank you for answering pfa a link for the sedond form";
//send the actual email
MailApp.sendEmail({
to: SendTo,
cc: "",
subject: 'Thank you for answering',
htmlBody: body,
});
}
//delete emails and responses
deleteEmails(LastRow)
deleteResponses()
}
I am trying to work out a solution to something and whilst I can get snippets of help from the weeb I cannot pin it down to what I want it to do. So, what I want to achieve:
A google form is submitted which creates a record in a google spreadsheet.
using the query function I take that information instantly to another sheet in the same spreadsheet. Whilst in there it goes through some if statements which creates a unique ID for the submission, separates the date and the time of the submission into separate columns and adds a email address that needs to be notified of this submission. For the purpose of this test, the submitter and the notification emails are the same.
the script monitors the second sheet and, on form submission (trigger), takes the information from the last row in a certain sequence to create an email:
function formsubmission(e){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetbyName('email');
var startRow = 2;
var lastRow = e.sheet.getLastRow();
var formid = e.sheet.column[1];
var notificationemail = e.sheet.column[2];
var formdate = e.sheet.column[3];
var formtime = e.sheet.column[4];
var submitter = e.sheet.column[6];
var yn = e.sheet.column[7];
var subject = formid + "submitted";
var message = "Thank for answering" + yn + "on" + formdate + "at"+ formtime;
MailApp.sendEmail (notificationgroup, subject, message,{
cc: submitter,
});
}
Needless to say that the above script does not work.
If any of you kind people want to help. The form is test form to which the above refers is here and the related spreadsheet is here.
I would prefer not to have a separate sheet but the one accepting submissions does not allow for any functions or anything else in it.
Any help to make the above script work would be greatly appreciated.
Thank you
I'm trying to create a script for new google sheets that sends a notification if a form is submitted and contains data in a specific column.
In this example, if the question that populates data in column E is answered in the form, I would like an email notification with a link to the spreadsheet.
In the below script, if I set the trigger to on edit it will work if column E is edited. I cannot get it to work with on form submit, however. Can anyone help with fixing this script to work on edit?
Thanks!
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Form Responses");
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var recipients = "email#email.com";
var subject = 'subject';
var body = 'body';
if (cell.indexOf('E')!=-1) {
MailApp.sendEmail(recipients, subject, body);
}
}
when your script runs on a form submission trigger , there is no such thing as active cell, the form data is inserted in the last row of the sheet.
You only have to look at this last row and check the right column (getLastRow() and column 5 for "E").