简体   繁体   中英

Extra commas after concatenating a range of cells in Sheet

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 AF include basic information
  • Columns HT 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 ", "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM