[英]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:在工作表中,您将看到:
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.您可以改用filter
、 concat
和join
。 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部分比较了你的和下面的代码:
formSupportingSAP = supportingSAP.flat().concat(secondarySAP.flat()).filter(Boolean).join(", ");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.