Extra commas after concatenating a range of cells in Sheet - google-apps-script

For reference: Here is my Google Form.
Here is a Google PE Blank Sheet which has been tied to the output of my Google Form (answers will output on tab Form Responses 2).
I've built up a Google Form capable of collecting a good bit of data. It is meant to be filled out by a manager requesting SAP permissions for a new hire.
They proceed through the Form and enter their primary department (SAP Stream) which leads them to the next page/section that allows the manager to check off what the employee's required permissions will be.
Sometimes, an employee will interface with multiple departments, so the option for a supporting/secondary SAP Stream exists on the second page. This can be continued as needed until all of an employee's requisite roles/permissions are collected in a Google Sheet.
In the Sheet, you will see that:
Columns A-F include basic information
Columns H-T are for the Job Responsibilities
Columns U-AG are a range for the information of one's Supporting SAP Stream
Columns AH-AT are the Supporting SAP Stream's Job Responsibilities
Columns AU-BG collect the Secondary Supporting SAP Stream
I had built Google Script language to collect each of these bits of information in variables which would then get declared as global variables so they could be passed along to an HTML page in the App Script.
Finally, an email consisting of this HTML page would be sent out to the manager in charge of designating SAP permissions.
Everything is working fine since my last update. The email gets sent out and the variables are collecting the information from the range of cells I have targeted. But the output is full of commas separating the returned values.
Screenshot of email result
Is it that the cells which have nothing are being included and separated with commas? Or is there something else I've done wrong with my variables?
Edit:
Here is a sample of some of the relevant portions of my code:
function sendEmail_v3() {
// get the spreadsheet information
const ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
//const responseSheet = ss.getSheetByName('Form Responses 1');
const data = ss.getDataRange().getValues();
//console.log(data);
// Loop over the rows
data.forEach((row,i) => {
// Identify whether notification has been sent
if (row[59] === '') {
// Get the Form info
var emailTo = "xxxxxx#gmail.com"
var subject = 'SAP Role Request Subject';
const Timestamp = row[0];
var emailAddress = row[1];
var name = row[2];
var department = row[3];
var userID = row[4];
var SAP_Stream = row[5];
var valuesA = ss.getRange(i,8,1,13).getValues();
var jobResponsibilities = valuesA;
var valuesB = ss.getRange(i,21,1,13).getValues();
var supportingSAP = valuesB;
var valuesC = ss.getRange(i,34,1,13).getValues();
var secondaryJob = valuesC;
var valuesD = ss.getRange(i,47,1,13).getValues();
var secondarySAP = valuesD;
formTime = Timestamp;
formEmail = emailAddress;
formName = name;
formUserID = userID;
formSAP_Stream = SAP_Stream;
formDepartment = department;
formResponsibilities = jobResponsibilities;
formSupportingSAP = supportingSAP + "," + secondarySAP;
formSecondaryJob = secondaryJob;
let body = '';
// Write the email in email.html
var html = HtmlService.createTemplateFromFile("email.html");
var htmlText = html.evaluate().getContent();
var options = {htmlBody: htmlText};
// Send the email
GmailApp.sendEmail(emailTo,subject,'',{htmlBody: htmlText});
// Mark as Notified
const g = 'Notification sent';
ss.getRange(i + 1,60).setValue(g);
}
});
}
The global variables get pulled into a separate part of the Google Script in an HTML file which gets sent out in the email. The email generated looks like this:
Screenshot of email result
I imagine there is another function or some code I can add to the lines with the values variables which will help to avoid the NULL results.
var valuesA = ss.getRange(i,8,1,13).getValues();

You can use filter, concat and join instead. In your case, since the data is also 2D array, you additionally need to do flat on them before using concat. Check the code below. Use concat instead of adding 2 arrays directly using +. I have compared yours and the code below on Logs and Output section:
Code:
formSupportingSAP = supportingSAP.flat().concat(secondarySAP.flat()).filter(Boolean).join(", ");
Logs and Output:
References/Notes:
flat() to get the 1 dimensional array equivalent
concat() to combine the 2 arrays
filter() to filter out blank cells
join() to combine into a string with a delimiter ", "

Related

How to group by a specific cell and then send email to respective IDs in google sheets?

Here's a sample dataset I have
In apps script I want this data to be divided by sender like for example for hub1 would be
and then in another sheet I have the following data
and then send that specific hub's table to that specific hub's email address.
I've already made the code to send the emails to all the hubs which are entered, but how to slice the data per hub for each hub and send that data only I am not able to do.
Kindly help!
I faced the same problem recently. Cannot find a helper at that time so I make personal lib. See the file to copy in a new .gs file at the link : https://github.com/SolannP/UtilsAppSsript/blob/main/UtilsGSheetTableHelper.gs
You can use the code bellow to adapt to feet your needs :
// get range of all cell near A1 with value, in your case something like A1:B5 (email and HubName)
var contactData = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SHEET_WITH_YOUR_EMAIL_DATA").getRange("A1").getDataRegion();
// get range of all cell near A1 with value, in your case A1:C7
var tableData = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SHEET_WITH_YOUR_DATA_SET").getRange("A1").getDataRegion();
// create special table of contact
var specialTableContact = new TableWithHeaderHelper(contactData);
// create special table of data set
var specialTableData = new TableWithHeaderHelper(tableData);
// for each row in the table of contact
for(var i=0 ; i < specialTableContact.length() ; i++){
var email = specialTableContact.getWithinColumn("EmailID").cellAtRow(i);
var hubName = specialTableContact.getWithinColumn("HubName").cellAtRow(i);
//create a sub table for matching hubName
var filterDataOnHubName = specialTableData.getTableWhereColumn("Sender").matchValue(hubName)
// Some data for later use
var summaryDataForEmail = "";
var cost="";
var idProduct="";
// for each row in the table product matching current HubName
for(var j=0; j < filterDataOnHubName.length() ; j++){
cost = filterDataOnHubName.getWithinColumn("Cost").cellAtRow(j).getValue();
idProduct = filterDataOnHubName.getWithinColumn("ProductID").cellAtRow(j).getValue();
summaryDataForEmail = summaryDataForEmail+ `${idProduct} at ${cost}.`
}
// ... do watever you want
// something like
// MailApp.sendEmail({
// to:email ,
// subject: "some subject",
// cc:"anyone in copy",
// htmlBody:summaryDataForEmail,
// });
}
Speed is not optimized but at least it's not so hard to grasp what the code is doing in 30~40 lines of code.

Sending the same multiple emails instead of 1 email for loop bug

I have bug where when I run my send email function. its sending multiple emails instead of just one email notification here is my code what am I doing wrong??!?! I got 31 of the same emails. I believe the issue the for loop is sending an email each time the if statement is true instead of just one time if its true help.
here is my code:
function sendEmail(){
var ss = SpreadsheetApp.getActiveSpreadsheet(); //get active spreadsheet only! to get the url for the filter view
var SpreadsheetID = ss.getSheetId(); // get the sheet Id
var spreadsheetURL = ss.getUrl(); // get the current active sheet url
var SpreadsheetID = spreadsheetURL.split("/")[5]; // using the last / for getting the last parts of the email
var filterViewName = 'PO_Log Precentage'; // Name of the filter view you want to get the url from & MAKE SURE Title matches view name account for "spaces" too
var filterViewID = filterId(SpreadsheetID, filterViewName); // Getting filter view id
var url = createURL(spreadsheetURL, filterViewID); // creating the url to send the filter view id
Logger.log(url);// Testing to see the correct url is created
var po_numID = ss.getSheetByName("Purchase Orders List").getRange("A2").getDisplayValue().substr(0,3);// Gets the Purchase Order List Sheet and the PO# the first 3 Characters of the PO in A2
Logger.log(po_numID);
var email_va = ss.getSheetByName("Purchase Orders List");
//gonna build statuses to look for into array
var statusesToEmail = ['On-going', '']
//"Status" is in Column T (Col 2)
//"Precent" is in Column Q (Col 3)
var data = email_va.getDataRange().getValues()
// //var headerRowNumber = 1; // When checking for emails in the sheet you want to exclude the header/title row
var emailDataSheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/17G0QohHxjuAcZzwRtQ6AUW3aMTEvLnmTPs_USGcwvDA/edit#gid=1242890521").getSheetByName("TestA"); // Get The URL from another spreadsheet based on URL
Logger.log(emailDataSheet.getSheetName());
var emailData = emailDataSheet.getRange("A2:A").getDisplayValues().flat().map(po => po.substr(0,3));
Logger.log(emailData)///Working to get the first 3 charcters in column A
var subject = po_numID + " Po Log Daily Notification "; // Unique PoTitle of the email
var options = {} // Using the html body for the email
options.htmlBody = "Hi All, " + "The following" + '<a href=\"' +url+ '" > Purchase Orders </a>' + "are over 90% spent" + "";
for(var i = 0; i < data.length; i++){
let row = data[i];
if( statusesToEmail.includes(row[1]) & (row[2] >= .80)){
emailData.every((po, index) => {
if (po == po_numID){
const email = emailDataSheet.getRange(index + 2,7).getValue();//Getting the last colmun on the same row when the Po# are the same.
console.log(email);
MailApp.sendEmail(email, subject, '', options); // Sending the email which includes the url in options and sending it to the email address after making sure the first 3 Charcters Of the PO_log are the same as
return false;
} else {
return true;
}
});
}
}
}
here is the spreadsheet
https://docs.google.com/spreadsheets/d/1QW5PIGzy_NSh4MT3j_7PggxXq4XcW4dCKr4wKqIAp0E/edit#gid=611584429
you have to use the break function if u wish to stop the loop once the loop has been fulfiled, because if im not wrong , the email is sent if the IF condition is met , thus in the block that has mailapp.sendemail , you have to add in a break otherwise the loop will keep on happening. this is the basic of javascript and you should read up more about the FOR loop here
break as in just type "break" at the end of the code so the script will not continue to loop once the condition has been met.

Scipt: Input data in new cell based on value from specific cell and form submit auto fill a google doc

I have a google form that inputs collected answers to a google sheet, with a script that auto fills a doc with the relevant information and emails to the selected recipient. That all works. Here is my problem.
I have one specific question on the Google Form with 5 options. When an option is chosen, I have a specific research/quote that is associated with it. I created a formula in the spreadsheet to pull that into the last column of the spreadsheet - which worked.
But...now when I submit the form with with this new info, the script does not work. I get TypeError: Cannot read property 'values' of undefined
at onFormSubmit(Code:8:21)
I assume that the script is trying to get a value from the column I directed it to, but the formula did not operate fast enough for there to be a value there?
I am guessing I need to make this all happen in the script, so the info is put into the column I need first, then it pulls data from the spreadsheet to create the document and send the email. I just don't know how to start...
I cannot even be considered a novice - a friend shared this code with me and I edit it when needed.
Ex: If answer choice in column F = Read, then column K should show "read quote"; If answer choice in column F = Write, then column K should show "write quote"; and so on.
My current script:
// Global variables
var docTemplate = "1EoBcz0BK4R5hm-q5pR68xnQnR8DlR56XzjxRrgsu4uE"; // *** replace with your template ID ***
var docName = "You got a High 5";
function onFormSubmit(e) { // add an onsubmit trigger
// Values come from the spreadsheet form
var observer = e.values[1]
var teacher = e.values[2]
var email = e.values[2]
var period = e.values[4]
var time = e.values[3]
var cif = e.values[5]
var image = e.values[6]
var comments = e.values[7]
var message = e.values[10]
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+' for '+teacher)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys,
copyBody.replaceText('keyobserver', observer);
copyBody.replaceText('keyteacher', teacher);
copyBody.replaceText('keyemail', email);
copyBody.replaceText('keyperiod', period);
copyBody.replaceText('keytime', time);
copyBody.replaceText('keycif', cif);
copyBody.replaceText('keyimage',image);
copyBody.replaceText('keycomments', comments);
copyBody.replaceText('keymessage', message);
var todaysDate = Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy");
copyBody.replaceText('keyTodaysDate', todaysDate);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF by using the getAs blob conversion
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "High 5 - it matters.";
var body = "You got a High 5! See attached PDF. " +
"Please do not reply to this email. You will be asked to supply a response thorugh a link within the attached PDF. " +
"'Do all the good you can. By all the means you can. In all the ways you can. In all the places you can. At all the times you can. To all the people you can. As long as you ever can. -John Wesley'";
MailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}
The form submit trigger's event object will only capture the responses from the form written to the Google Sheet, not formulas done only in the sheet. That is why you are getting undefined error.
So you need to do your logic in the script itself:
var message;
if (cif == 'Read') {
message = 'read quote'
}
else if (cif == 'Write') {
message = 'write quote'
}
This is extremely simplistic, if you have a lot of choices for column F you might need to use switch instead of if statement.

Google Docs script editor trying to autogenerate an email from an autogenerated document

Everything I've found about this topic includes having a google sheet populate a google doc which then sends an email. I personally have pasted some code I found around the web into script editor of a doc. Now, upon opening a doc, the user is prompted to answer question boxes. the answers autopopulate a new document that is created. The script then calls for an email to be sent out.
So far, I have the prompts correct, the new document is created, with the correctly filled-in information from the prompt boxes. I have also gotten it to send an email to 1 address, which is all it is supposed to do. The subject line of the email is also correct. The problem is I want the new Google document that is created in the script to be the body of the email, and I just cannot figure out how to make that happen.
This is the code I have in script editor. I have tried numerous things in the last line to make the body of the new document populate the email body, with no luck. Can someone tell me the programming language for how to make this work please?
function myFunction() {
// Display a dialog box for each field you need information for.
var ui = DocumentApp.getUi();
//var response = ui.prompt('Enter Name', 'Enter sales person's name', ui.ButtonSet.OK);
var shiftResponse = ui.prompt('Enter shift, i.e. 7-3 or 3-11');
var peersResponse = ui.prompt('Enter peers on shift');
var participantsResponse = ui.prompt('Enter names of face to face encounters');
var phonelogResponse = ui.prompt('Enter names of people we called on phone log');
var filescreatedResponse = ui.prompt('Enter names of people we created files for');
var notesResponse = ui.prompt('Enter any notes about shift');
var cleanResponse = ui.prompt('Was Crisis Center Cleaned? Enter yes or no');
var authorResponse = ui.prompt('Enter your name');
var date = Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy");
//Make a copy of the template file
var documentId = DriveApp.getFileById('1lXTJPvwlJrXkRJ807daFsFbfaiC_wl7EAQ4giixLeEc').makeCopy().getId();
//Rename the copied file
DriveApp.getFileById(documentId).setName(date + " " + shiftResponse.getResponseText() + ' Shift Report');
//Get the document body as a variable
var body = DocumentApp.openById(documentId).getBody();
//Insert the entries into the document
body.replaceText('##date##', date);
body.replaceText('##shift##', shiftResponse.getResponseText());
body.replaceText('##peers##', peersResponse.getResponseText());
body.replaceText('##participants##', participantsResponse.getResponseText());
body.replaceText('##phonelog##', phonelogResponse.getResponseText());
body.replaceText('##filescreated##', filescreatedResponse.getResponseText());
body.replaceText('##notes##', notesResponse.getResponseText());
body.replaceText('##clean##', cleanResponse.getResponseText());
body.replaceText('##author##', authorResponse.getResponseText());
MailApp.sendEmail("jason.chrystal#voicesofhopececilmd.org", "Shift Report", body);
}
As it reads in the documentation:
The replaceText methods expects a regex pattern value as the first parameter:
https://developers.google.com/apps-script/reference/document/body#replacetextsearchpattern,-replacement
Also the last parameter of the MailApp.sendMail parameter expects a string and you are giving it a body class object.
Change your first parameter to a correct matching regex pattern and your code will work just fine.
body.replaceText(/^##date##$/, date);
body.replaceText(/^##shift##$/, shiftResponse.getResponseText());
body.replaceText(/^##peers##$/, peersResponse.getResponseText());
etc...
-- regex not tested.
If you are not comfortable with regular expressions you can use body.setText() as an alternative like so:
var oldBodyText = body.getText();
body = body.setText(oldBodyText.replace('##date##', date));
oldBodyText = body.getText();
body = body.setText(oldBodyText.replace('##shift##', shiftResponse.getResponseText()));
oldBodyText = body.getText();
body = body.setText(oldBodyText.replace('##peers##', peersResponse.getResponseText()));
etc...
// And then the last lines:
var newBody = body.getText();
MailApp.sendEmail("jason.chrystal#voicesofhopececilmd.org", "Shift Report", newBody);

How to generate a Google Doc from a Form response, containing all Form items and responses?

I am currently using the following to send an email to a distribution list containing the headers/questions and corresponding answers when a Google Form is submitted:
function sendFormByEmail(e) {
// Subject field of email
var emailSubject = "New Student Case Filed";
// Comma-separated list of email addresses for distribution.
var yourEmail = "my email address";
// Spreadsheet key, found in the URL when viewing your spreadsheet.
var docKey = "my spreadsheet key";
// If you want the script to auto send to all of the spreadsheet's editors, set this value as 1.
// Otherwise set to 0 and it will send to the yourEmail values.
var useEditors = 0;
// Have you added columns that are not being used in your form? If so, set this value to
// the NUMBER of the last column that is used in your form.
// for example, Column C is the number 3
var extraColumns = 40;
if (useEditors) {
var editors = DocsList.getFileById(docKey).getEditors();
if (editors) {
var notify = editors.join(',');
} else var notify = yourEmail;
} else {
var notify = yourEmail;
}
// The variable e holds all the submission values in an array.
// Loop through the array and append values to the body.
// Need to omit headers with no response*
var s = SpreadsheetApp.getActive().getSheetByName("StudentCases");
if (extraColumns){
var headers = s.getRange(1,1,1,extraColumns).getValues()[0];
} else var headers = s.getRange(1,1,1,40).getValues()[0];
var message = "";
for(var i in headers) {
message += headers[i] + ' : '+ e.values[i].toString() + '\n\n';
}
Now I also want a Google Doc created containing the headers and responses. So far, I've been able to create the Doc, add the title, and add a paragraph, but now I need to replicate the array of headers/responses in the Google Doc as well.
// Create a new Google Doc named 'Case File' * need to figure out how to pull last name response from form.
var doc = DocumentApp.create('Case File: Last Name*');
// Access the body of the Doc, add a paragraph, *need to append array of headers/answers
var body = doc.getBody().body.appendParagraph();
// Get the URL of the Google Doc to include in Email
var url = doc.getUrl();
// Get ID of Doc to attach to email
var id = doc.getId()
One other issue I'd like to solve; I only need the headers/questions that contain a response, as many of them will not necessarily warrant an answer. So in other words, IF there is no answer, THEN do not append to email.
It seems you provided a list of general requirements without much in the way of what you tried with the results you got. StackOverflow will be helpful to you if you provide more pointed questions about what exactly you have tried..
Can you share the exact code you have tried please? What were your results?
From a high level, I would proceed with this general workflow.
Draft a gDoc template using placeholders in a variables nomenclature of your choice ~FName ~LName etc..
Use an onFormSubmit trigger to make a copy of the gDoc template when a new form is submitted.
Replace the ~FName ~LName placeholders in the copied gDoc with content captured in the form
Save the copied gDoc as a PDF
email the PDF to the email address provided in the form submission