I have an app script that executes when the a google form response is submitted. The app script is attached to the response google sheet.
I want to send out a an email by formatting the responses but since google form does not allow formatting of answers I have to do it in the app script.
The user will enter the response for a question in multiple lines. I want to take that response and create and unordered list for that questions.
For example: the user will enter the following response this is a test this should be another list item. another one.
I want to format this as
- this is a test
- this should be another list item.
- another one.
to do this I have to get the response from the google sheet cell and split by new line (\n). For some reason I am unable to do so.
function onFormSubmit(e) {
var values = e.namedValues;
var htmlBody = '';
for (Key in values) {
Logger.log(Key ' : ' values[Key]);
try {
Logger.log(JSON.stringify(values[Key]));
} catch(error) {
Logger.log(error);
}
htmlBody = createSection(Key, values[Key]);
GmailApp.sendEmail('[email protected]', 'sending email', '', {htmlBody:htmlBody});
}
function createSection(sectionName, values) {
Logger.log(sectionName ' : ' values ' Length: ' values.length);
var linesHtml = createBulletList(values);
var sectionHtml = '<h1>' sectionName '</h1>' '<p><br></p>';
sectionHtml = '<ul>' linesHtml '</ul>' '<p><br></p>';
return sectionHtml;
}
function createBulletList(values) {
var listHtml = '';
Logger.log('createBulletList ' values ' Length: ' values.length);
//listHtml = values.forEach(listItem);
listHtml = listItem(values);
Logger.log(listHtml);
return listHtml;
}
I have tried split but for some reason it does not work on values[Key] object. I have tried JSON.stringify to flatten the object but the split('\n') still does not work on the string.
Any help will be appreciated.
CodePudding user response:
I think that this is what you are trying to do:
function createBulletList(multiLineText) {
return (multiLineText "")
.split("\n")
.map(line => "<li>" line "</li>")
.join("\n");
}
Notes:
- The "" part converts the value to text, in case it is a number or Date.
- split creates an array of texts
- map creates another array of html <li>
- join puts them back into a multi-line text (can be join("") if you don't want that)
CodePudding user response:
Sending unordered list
function onMySubmit(e) {
//Logger.log(JSON.stringify(e));
let itemA = e.values.map((v,i) => {if(i > 0) {return v}}).filter(e => e);
let hl = "<ul>" itemA.map(itm => `<li>${itm}</li>`).join("") "</ul>";
//Logger.log(hl);
GmailApp.sendEmail("recipient email","list",null,{htmlBody:hl});
}
Using values from onFormSubmit event object.
Using short answer questions
CodePudding user response:
using an explicit variable and using that instead of values[Key] fixed the issue
var cellValue = values[Key].toString();