简体   繁体   English

连接工作表中的一系列单元格后的额外逗号

[英]Extra commas after concatenating a range of cells in Sheet

For reference: Here is my Google Form .供参考:这是我的 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).这是与我的 Google 表单的 output 相关联的 Google PE 空白表(答案将在表单响应 2 选项卡上的 output 上)。


I've built up a Google Form capable of collecting a good bit of data.我已经建立了一个能够收集大量数据的 Google 表单。 It is meant to be filled out by a manager requesting SAP permissions for a new hire.它应由为新员工申请 SAP 权限的经理填写。

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.他们继续填写表格并输入他们的主要部门(SAP 流),这将引导他们进入下一页/部分,允许经理检查员工所需的权限是什么。

Sometimes, an employee will interface with multiple departments, so the option for a supporting/secondary SAP Stream exists on the second page.有时,员工会与多个部门进行交互,因此第二页上存在支持/辅助 SAP Stream 的选项。 This can be continued as needed until all of an employee's requisite roles/permissions are collected in a Google Sheet.这可以根据需要继续进行,直到员工的所有必要角色/权限都收集到 Google 表格中。

In the Sheet, you will see that:在工作表中,您将看到:

  • Columns AF include basic information AF 列包含基本信息
  • Columns HT are for the Job Responsibilities列 HT 用于工作职责
  • Columns U-AG are a range for the information of one's Supporting SAP Stream U-AG 列是支持 SAP Stream 的信息范围
  • Columns AH-AT are the Supporting SAP Stream's Job Responsibilities AH-AT 列是支持 SAP Stream 的工作职责
  • Columns AU-BG collect the Secondary Supporting SAP Stream AU-BG 列收集辅助支持 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.我构建了谷歌脚本语言来收集变量中的每一个信息位,然后这些变量将被声明为全局变量,以便它们可以传递到应用程序脚本中的 HTML 页面。

Finally, an email consisting of this HTML page would be sent out to the manager in charge of designating SAP permissions.最后,由该 HTML 页面组成的 email 将发送给负责指定 SAP 权限的经理。

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. email 被发送出去,变量正在收集我所针对的单元格范围内的信息。 But the output is full of commas separating the returned values.但是 output 充满了分隔返回值的逗号。

Screenshot of email result email 结果截图

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.全局变量在 HTML 文件中被拉入 Google 脚本的单独部分,该文件在 email 中发送。 The email generated looks like this:生成的 email 如下所示:

Screenshot of email result email 结果截图

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.我想还有另一个 function 或一些代码我可以添加到带有值变量的行中,这将有助于避免 NULL 结果。

var valuesA = ss.getRange(i,8,1,13).getValues(); var valuesA = ss.getRange(i,8,1,13).getValues();

You can use filter , concat and join instead.您可以改用filterconcatjoin In your case, since the data is also 2D array, you additionally need to do flat on them before using concat .在您的情况下,由于数据也是二维数组,因此您还需要在使用concat之前对它们进行flat处理。 Check the code below.检查下面的代码。 Use concat instead of adding 2 arrays directly using + .使用concat而不是直接使用+添加 2 arrays 。 I have compared yours and the code below on Logs and Output section:我在Logs 和 Output部分比较了你的和下面的代码:

Code:代码:

formSupportingSAP = supportingSAP.flat().concat(secondarySAP.flat()).filter(Boolean).join(", ");

Logs and Output:日志和 Output:

日志和输出

References/Notes:参考/注释:

  • flat() to get the 1 dimensional array equivalent flat()获得等效的一维数组
  • concat() to combine the 2 arrays concat()组合 2 个 arrays
  • filter() to filter out blank cells filter()过滤掉空白单元格
  • join() to combine into a string with a delimiter ", " join()用分隔符", "组合成一个字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将单元格范围复制到另一个工作表 - Copy range of cells to another sheet 基于不同工作表中的值的单元格颜色范围 - Color range of cells based on values in different sheet 复制谷歌表格后受保护单元格的问题 - Issues with Protected cells after duplicating the google sheet 谷歌脚本将单元格范围移动到时间敏感触发器上的存档表 - Google Script to Move Range of cells to Archive sheet on time sensitive trigger Google表格脚本,返回范围值关闭了3个单元格 - Google sheet script, return range value is 3 cells off 将一系列值复制/粘贴到单独工作表上的下一组空单元格中 - Copy/Pasting a Range of Values into the Next Empty Set of Cells on a Separate Sheet Google Sheet Script - 根据包含电子邮件的一系列单元格隐藏选项卡 - Google Sheet Script - Hiding tabs based on a range of cells containing emails 将一系列单元格从一行移动到另一个谷歌工作表并删除它 - Move a range of cells from a row to another google sheet and delete it Google工作表的脚本,用于基于另一个单元格值(在列范围内)更改单元格值 - Script for Google sheet to change a cells value based on another cells value (in a column range) Select 单元格(Ctrl+A)然后复制范围并粘贴到另一个工作表的第一个空行 - Select cells(Ctrl+A) then Copy range and paste to another sheet's first empty row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM