I'm trying to extract the link inside href and store it in a variable. Please see my code snippet below. Note that the token id is expected to be different with every single run.
Thank you for your help.
const body = <html><head></head><body><p>This is SignUp Email with confirmation link</p><p></p></body></html>
const activation_link = ???
console.log(activation_link)
/*
The expected result to be printed on Console:
"http://www.company.com/ls/click?upn=tokenid-11111-22222-333333-444444-555555-xxxxxx"
*/
const body = `<html><head></head><body><p>This is SignUp Email with confirmation link</p><p></p></body></html>`
const matched_links = body.match(/(?<=")http.+(?=")/);
console.log(matched_links);
Related
I am trying to take the user input from the search bar, translate it into a string and then open up the webpage i have created using the input they have given me.
Here is what i am looking to do.
if(userData){
icon.onclick = ()=>{
webLink = 'myPages/userData.html'
linkTag.setAttribute("href", webLink);
linkTag.click();
}
When i try to add in the variable userData, which is the user input from the search bar, doing it like this:
if(userData){
icon.onclick = ()=>{
webLink = 'myPages/' + userData + '.html'
linkTag.setAttribute("href", webLink);
linkTag.click();
}
This comes off as an error and says it cannot find the file to redirect to.
I am new to javascript and need to figure out how to pass the variable in and create the correct address to open up the new html file page.
I have already taken out the spaces and formatting from the user input, as well as tried to create a new variable containing the address but both did not work and i still got the same error saying there are quotations in the address so it cannot be found.
I already have the html form made from bootstrap, however, when I am trying to use the click button to add the information to google sheet from the html form it is not working
this is my GS function
function addRow(rowData){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getSheetByName("STUDENTS");
ws.appendRow([rowData.names,rowData.phones,rowData.idnumber,rowData.fivehr]);
and this is my function on html file
function afterButtonClick(){
var name = document.getElementById("studentnames");
var phone = document.getElementById("phonenumber");
var idnumber = document.getElementById("ID-number");
var fivehr = document.getElementById("fivehr");
var rowData = {name: name.value,phone: phone.value, idnumber: idnumber.value, fivehr: fivehr.value};
google.script.run.addRow(rowData);
}
document.getElementById("ADD").addEvenListener("click",afterButtonClick);
example for the line for the variable
<input style="font-weight:bold" type="text" class="form-control" placeholder="Student Name" aria-label="Name" id="studentnames">
the html form is perfect the way I want it, but the function on html is not adding the data to sheet
I am following a youtuber but it is not working at all, I did some changes on the wording but I guess I am missing something, if anyone can look at it and tell me my mistake, I appreciate the help thank you
if fivehr is a datetime then that is a restricted parameter and cannot be passed using google.script.run. If it is a restricted parameter then the entire object will be nulled out. Read about it below:
https://developers.google.com/apps-script/guides/html/communication#parameters_and_return_values
Keys of your object should be same.
You've to use rowData.name and rowData.phone.
ws.appendRow([rowData.name,rowData.phone,rowData.idnumber,rowData.fivehr]);
In order to see what is the problem, you can firstly go to DevTools > Console and see if there is an error message in your client side functions. If not you go to Script Editor > Executions and see if your server side functions returned an exception.
I have a project where i need to bulk send an email with text specific to each user, and with the signature and image from the associated gmail account. I've got this atm:
//get template/draft email & body
const scGmailTemplate = GmailApp.getDraft("r4329894329375089160");
const scGmailTemplateMessage = scGmailTemplate.getMessage();
const scGmailTemplateBody = scGmailTemplateMessage.getBody();
//create new gmail
let scGmailContactMessageBody = scGmailTemplateBody;
//create array for gmail find & replaces [find, replace]
const gmailFindReplace = [["INV_START", scVars.INV_START],
["INV_END", scVars.INV_END],
["DEM_DATE", scVars.DEM_DATE]
];
gmailFindReplace.forEach(x=>{scGmailContactMessageBody=scGmailContactMessageBody.replace(x[0], x[1])});
const scGmailSubject = "Service Charge Invoice ("+scVars.INV_START+"-"+scVars.INV_END+")";
let bodyHtml = HtmlService.createHtmlOutput(scGmailContactMessageBody);//didnt work
GmailApp.sendEmail("me#me.com", "test", scGmailContactMessageBody);
Issue is the resultant email is just the raw html with the image displayed at the bottom
I've tried adding {htmlBody: html} as an option but that throws an error html not defined.
Any help would be much appreciated
The htmlBody expects a string.
GmailApp.sendEmail("me#me.com", "test", scGmailContactMessageBody, {htmlBody: scGmailContactMessageBody});
No need for let bodyHtml = HtmlService.createHtmlOutput(scGmailContactMessageBody); as getBody() already returns an HTML string.
From the question
I've tried adding {htmlBody: html} as an option but that throws an error html not defined.
The error message is straight forward, the code doesn't include an statement declaring html
try replacing html by bodyHtml
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(/=/, '=')
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.