I have a script attached to a Google Form which sends a notification to a Discord channel on submission. I want to include a direct link to the individual response (link looks like https://docs.google.com/forms/d/<myformid>/edit#response=<responseid>). How can I retrieve that link? I get part of the link up to /edit with Form.getEditUrl() but I can't get the correct id. I checked FormResponse.getId() but that doesn't link me to any response.
Here's how to get to that link manually via edit form:
Since you know the response Id, you can use the geEditResponseUrl method to get the direct link to the form response. Do note that anyone with this URL can edit the response.
function getEditUrl(responseId) {
var form = FormApp.getActiveForm();
var response = form.getResponse(responseId);
return response.getEditResponseUrl()
}
You can get the responseId through the .getId() method.
let response = form.getResponse(responseId);
let responseId = response.getId();
Or if you are doing this onSubmit,
let form = FormApp.getActiveForm();
let allResponses = form.getResponses();
let latestResponse = allResponses[allResponses.length - 1];
let responseId = latestResponse.getId();
What you want is the url to the Editor of the Form opened to the correct tab
var responseId = e.response.getId()
var urlString = "https://docs.google.com/forms/d/" + formId + "/edit#response=" + responseId
The Problem is that the getId() function returns a different type of ID then the one that is generated on the edit screen. I tried both of these examples:
function onFormSubmit(e){
var responseId = e.response.getId()
}
and from Earlking's Response
var allResponses = thisForm.getResponses();
var latestResponse = allResponses[allResponses.length - 1];
var responseId = latestResponse.getId();
They both give the same ID, But not the required URL ID,
Here is a Snippet comparing the URLs https://docs.google.com/forms/d/FORMID/edit#response=ACYDBNh8k40Y7zxtUeYzw8wDwRx4pggu8APuxl5TmInVVieXN-SrmTW8tK0zHvQPmVsnYzY
https://docs.google.com/forms/d/FORMID/edit#response=2_ABaOnuet7P69_wc3S4QJkgkjS4abty4aDD9Zn1IQ8bhSKyGiynGGtuyg1v0A-xkLgOMelUE
After the edit#response= they are different.
The first one I copied from my Form editor opened to the last response. The second is generated by the code. It will take you to your form edit page, but redirects to open on the first response when the url has an error.
Seems like a Bug or an Undeveloped Feature.
Related
I am trying to extract data from a webpage which requires login.
The following code with
URLfetchapp.fetch(x).getcontentText()
returns the login page, however I want to get the data after login security page. Code to get data from the page is :
function getPTtrack(linksArray){
linksArray = `https://www.blogger.com/about/`;
var ptTracknames = [];
for(var sear in linksArray){
var optoutlink= linksArray[sear].toString().search("Publish your passion");
if(optoutlink!=-1){
var x = linksArray[sear].replace(">.","").replace("<","").replace(">","").toString();
var page = UrlFetchApp.fetch(x).getContentText();
var number = page.match(/<b>(.*)<\/b>/)[1];
Logger.log(number);
ptTracknames.push(number);
}
}
console.log(ptTracknames);
return ptTracknames;
}
But when I run the code to get the data after login to the page. I didn't get the data from next page instead I get the login page html code in console. Can anyone please suggest what code should I use
I'm having a really hard time sending an automated email (with Google Apps Script) that includes a URL that contains query parameter.
Expected Behavior
Google Apps Script (specifically, the Gmail service) sends an email, and part of the email body contains a URL with a query parameter. The URL will look something like this:
http://my.app/products?id=Bz9n7PJLg8hufTj11gMF
Observed Behavior
The Gmail service seems to be stripping out the = from my URL. So, the body of the email ends up looking like this:
...
http://my.app/products?idBz9n7PJLg8hufTj11gMF
...
Obviously, that link won't work.
I've checked other questions here on SO, and I've tried working with the base encoding tools from the GAS Utilities service, as well as working with the encodeURI() JavaScript method. No luck so far.
Email-sending Code
//////// GENERATING MESSAGE FROM ID ////////////
// Gets message from ID
var id = Gmail.Users.Drafts.get('me', 'r-1006091711303067868').message.id
var message = GmailApp.getMessageById(id)
var template = message.getRawContent()
// Replaces template variables with custom ones for the user using RegExes
let listingUrl = 'http://my.app/products?id=xyz'
let creatorEmail = 'hello#gmail.com'
let creatorUsername = 'Sam'
template = template.replace(/templates#my.app/g, creatorEmail)
template = template.replace(/firstName/g, creatorUsername)
//** Below is the string that gets modified and broken **//
template = template.replace(/listingUrl/g, listingUrl)
// Creates the new message
var message = Gmail.newMessage()
var encodedMsg = Utilities.base64EncodeWebSafe(template)
message.raw = encodedMsg
// Sends it
Gmail.Users.Messages.send(message, "me", Utilities.newBlob(template, "message/rfc822"))
Regex-based Solution
With the help of Tanaike and Rafa Guillermo, the solution that ended up working for me was to replace = with = by using a little .replace() like this:
listingUrl = listingUrl.replace(/=/, '=')
This is a two part question:
I have google sheet with a linked Form. When the form is submitted I want the responses from the form to be copied to another google sheet where I intend to change and reformat and then send via email. The script below is what i have currently written and it has a trigger set up onFormSubmit. However, I keep getting the follow error:
TypeError: Cannot read property "Values" from undefined. (line 7, file "Code")
Code below:
function formSubmitReply(e)
{
var t = "1g-wIs6nGxu3mJYA1vKtPCxBLCsvh1upeVGbCokOOTIw";
var tName = "AggregationOutput";
//Get information from form and set as variables
var email = e.Values[2];
var name = e.Values[3];
// Get template, copy it as a new temp, and save the Doc’s id
var tcopyId = SpreadsheetApp.openById(t).copy(tName+' for '+name).getId();
// Open the temporary document & copy form responses into template copy response sheet
var copyt = SpreadsheetApp.openById (tcopyId);
var copyts = copyt.getSheetByName('Resp');
// Transfers Data from Form Responses to Temporary file
copyts.getRange('A3').setValue(name);
//Sends copy of template in an email as an excel file
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + copyt.getId();
var subject = 'Aggregaton Output for' + name;
var body = url
MailApp.sendEmail(email, subject, body);
// Deletes temp file
DriveApp.getFileById(tcopyId).setTrashed(true);
}
Part two of my question, even if I can get the code to work what would you recommend when a question is skipped in the form - won't this change the array from e.values. The issue with using the last row as a problem is that I want people to go back and edit responses on the form and then resubmit which means the last row isn't always the row used.
Any and all help is appreciated.
For Part 1, try this:
function formSubmitReply(e)
{
var t = "1g-wIs6nGxu3mJYA1vKtPCxBLCsvh1upeVGbCokOOTIw";
var tName = "AggregationOutput";
//Get information from form and set as variables
var itemResponses = e.response.getItemResponses();
var email = itemResponses[2].getResponse();
var name = itemResponses[3].getResponse();
// Get template, copy it as a new temp, and save the Doc’s id
var tcopyId = SpreadsheetApp.openById(t).copy(tName+' for '+name).getId();
// Open the temporary document & copy form responses into template copy response sheet
var copyt = SpreadsheetApp.openById (tcopyId);
var copyts = copyt.getSheetByName('Resp');
// Transfers Data from Form Responses to Temporary file
copyts.getRange('A3').setValue(name);
//Sends copy of template in an email as an excel file
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + copyt.getId();
var subject = 'Aggregaton Output for' + name;
var body = url
MailApp.sendEmail(email, subject, body);
// Deletes temp file
DriveApp.getFileById(tcopyId).setTrashed(true);
}
Question 1:
The error you get is due to a wrong syntax, values (All small, not Values)
var email = e.values[2];
var name = e.values[3];
Question 2:
When the question is skipped the value of the response is blank. So if an email is left blank e.values[2] would still refer to the email field in your form, but will have no value in it.
If you have edit later option activated on the form, the edited responses will only be present in the e.values array. So if they update their email ID only, e.values[2] = "updated Email ID" and e.value[0-1,3-end] = Empty/blank.
To figure out if the submission is new entry or edited entry you can use e.range to figure out where the responses are going to be added in the "form Response" sheet. And you can mirror that range in your "resp" sheet to keep it updated the same way as form response sheet.
I have a multi page form.
Page One has a few fields that get passed into the second form, via GET method, and it auto fills the first four fields of the second part of the form.
Page two has a few more questions, and when you submit it, it submits into our CRM(vanillaSoft), and leads to a thank you page.
My current issue:
I want to be able to take an affiliate link, such as:
http://something.com/step-one.html?AFFILIATE_ID=#affid#&SUB_ID=#s1#
I need to dynamically populate the AFFILIATE_ID parameter with a unique transaction ID, and the SUB_ID with a unique ID as well.
I currently have two fields on my first page with hidden fields, ex:
<input type="hidden" name="SUB_ID">
<input type="hidden" name="AFFILIATE_ID">
But that isn't working. I need this date to be sent into the CRM I use.
Any advice?
Thanks!!!
Your current setup will work if you set your form submit method to GET. You probably have it set to POST.
Setting your form method to GET will put those hidden fields in the URL, like you are expecting.
On the last form, set that one to POST (to POST to the server).
You can grab the Query string with JavaScript, like this:
var getParamValue = (function() {
var params;
var resetParams = function() {
var query = window.location.search;
var regex = /[?&;](.+?)=([^&;]+)/g;
var match;
params = {};
if (query) {
while (match = regex.exec(query)) {
params[match[1]] = decodeURIComponent(match[2]);
}
}
};
window.addEventListener
&& window.addEventListener('popstate', resetParams);
resetParams();
return function(param) {
return params.hasOwnProperty(param) ? params[param] : null;
}
})();
How can I get query string values in JavaScript?
You could also send both POST and GET methods. But POST can be done only on server side, where JavaScript is Client-side scripting language.
<form method="POST" action="form.php?a=1&b=2&c=3">
PHP -> Send both POST and GET in a form
How to read the post request parameters using javascript
I am working on a feature for my site that allows the user to use the back button and not have to load more database results.
I start by loading 16 results, and then there is a load more button which loads the next 16. In the ajax success i change the href of this button so the url changes to e.g. domain.com/#1 to #2.
I wrote this last night:
// First get the page URL and split it via # signs
var parts = location.href.split('#');
// now we run a check on the URL and see how many 'parts' there are
if(parts.length > 1)
{
var params = parts[0].split('?');
var mark = '?';
if(params.length > 1)
{
mark = '&';
}
location.href = parts[0] + mark + 'page=' + parts[1];
}
Which gets the URL, and redirects the user the same page but converts the fragment number to a page number. From this i then use a PHP $_GET and set the limit claus last value from that.
This works fine. But its primitive. Let for instance say i push back and the URL becomes:
www.domain.com/?page=1
If i then click to load some more data, the page url becomes:
www.domain.com/?page=1#2
If the user then visits another page and comes back then they get directed to:
www.domain.com/?page=1&page=1
Whats the best way around this? I was thinking of running a check on the URL at the same time as looking for a fragment and if the URL has a page variable i then add that variable to the fragment variable and the page URL becomes ?page=THE SUM NUMBER
Any help on modifying the snippet i posted above to check the URL for a page value and then add the two together before the redirection?
Thanks!
You need to use location.search to get the query string on a URL:
var queryParameters = location.search.split('&');
Then you can loop through the queryParameters and check if page is set:
var pageNumber = 0;
for(var i = 0; i < queryParameters.length; i++)
{
var keyvaluePair = queryParameters[i].split('=');
if(keyvaluePair[0] == 'page')
{
pageNumber = keyvaluePair[1];
break;
}
}
Please see the documentation on the MDN:
https://developer.mozilla.org/en-US/docs/Web/API/Window.location
You might also find this example useful for returning one value:
https://developer.mozilla.org/en-US/docs/Web/API/Window.location#Example_.236.3A_Get_the_value_of_a_single_window.location.search_key.3A
If you want to get the information after the #, you need to use location.hash. The MDN documentation I linked also has information on location.hash.