Google表脚本脚本应用程序 - 选定学生的email名册,格式化问题
我为我们的教练创建了一个电子表格。他们选择一项运动,然后电子表格填写可能的学生名单。他们单击参加活动的学生姓名旁边的复选框,输入日期,然后单击“发送”按钮。它会向列出的教师发送一封电子邮件(第二个选项卡包含所有名册和电子邮件)。我写的脚本完成这一切没有问题。我遇到的问题涉及格式化。名称水平打印,并用逗号分隔每个名称:
学生一,学生二(等等)
[这是在原始帖子中,但我弄清楚了如何跳过数组中的空白点 如果名册中的学生被跳过,打印输出如下所示:
学生一、学生三、学生四、学生六(等)]
如果未选中该复选框,我不希望打印该姓名,但我希望电子邮件的打印输出看起来更清晰一些。 我使用数组来读取名称,我意识到它只是打印出数组,并且它有一个空框。 (解决了空名称部分) 我希望电子邮件看起来像:
学生一
学生二
我不确定如何完成此操作并且已经进行了相当多的搜索。我拥有的功能很实用,但看起来不太好。另一个循环可以完成此任务,但我不知道如何在格式化电子邮件的同时做到这一点。当我尝试在其中放置一个循环时,它绝对不喜欢。
电子表格如下:体育电子邮件电子表格示例
以下是代码我输入了:
function emailRoster()
{
var teacher = SpreadsheetApp.getActive().getRange("Rosters!J2:J4").getValues();
var roster = SpreadsheetApp.getActive().getRange("Sheet1!A6:B").getValues();
var sport = SpreadsheetApp.getActive().getRangeByName("Sport").getValue();
var date = SpreadsheetApp.getActive().getRangeByName("Date").getValue();
var lenT = teacher.length;
var lenR = roster.length;
var playerTrue = [];
for(var i=0; i<lenR; i++){
if(roster[i][1])
playerTrue[i] = roster[i][0];
}
playerTrue = playerTrue.filter(String); //recently added...fixed the printout so it ignores blank parts of the array
playerTrue.forEach(function(name) {
Logger.log(name);
});
for(var p=0; p<lenT-1; p++){
var email = teacher[p];
var subject = "Students out for " + sport;
var body = "Teachers,\nThe following students will be out for " +sport+ " on " +date +": \n\n" + playerTrue +"\n";
GmailApp.sendEmail(email,subject,body);
}
};
编辑
我创建了另一个函数来尝试让它返回每个名称后面都有一个 return,但我只能让它执行第一个名称:
function createRoster(){
var roster = SpreadsheetApp.getActive().getRange("Sheet1!A6:B").getValues();
var playerTrue = [];
var lenR=roster.length;
for(var i=0; i<lenR; i++){
if(roster[i][1])
playerTrue[i] = roster[i][0]; }
playerTrue = playerTrue.filter(String);
Logger.log(playerTrue);
for(var b=0; b<lenR-1; b++){
return playerTrue[b] + "\n";
}
Logger.log(playerTrue);
};
所以现在原始函数中的主体变量如下所示:
var body = "Teachers,\nThe following students will be out for " +sport+ " on " +date +": \n\n" + createRoster() +"\n";
I have created a spreadsheet for our coaches. They choose a sport and then spreadsheet fills with a roster of possible students. They click the check box next to the name of the student(s) attending the event, type the date, and click the Send button. It sends an email to the teachers listed (2nd tab has all rosters and emails). The script I wrote does all this no problem. The issue I am having deals with formatting. The names print out horizontally with a comma separating each name:
Student One, Student Two (etc.)
[This was in the original post, but I figured out how to skip blank spots in an array
If a student in the roster is skipped the printout looks like this:
Student One,,Student Three, Student Four,,Student Six (etc.) ]
I don't want the name to print if the checkbox isn't checked but I would like for the printout to look a little cleaner on an email. I used an array to read the names and I realize it's just printing out the array and it has an empty box. (solved the empty name part) I would like the email to look like:
Student One
Student Two
I am unsure how to accomplish this and have searched around quite a bit. What I have is functional, but doesn't look great. Another loop could accomplish this but I don't know how to do that while also formatting the email. It definitely doesn't like when I try to put a loop inside of there.
Here's the spreadsheet: Sample Sports Email Spreadsheet
Here is the code I have typed:
function emailRoster()
{
var teacher = SpreadsheetApp.getActive().getRange("Rosters!J2:J4").getValues();
var roster = SpreadsheetApp.getActive().getRange("Sheet1!A6:B").getValues();
var sport = SpreadsheetApp.getActive().getRangeByName("Sport").getValue();
var date = SpreadsheetApp.getActive().getRangeByName("Date").getValue();
var lenT = teacher.length;
var lenR = roster.length;
var playerTrue = [];
for(var i=0; i<lenR; i++){
if(roster[i][1])
playerTrue[i] = roster[i][0];
}
playerTrue = playerTrue.filter(String); //recently added...fixed the printout so it ignores blank parts of the array
playerTrue.forEach(function(name) {
Logger.log(name);
});
for(var p=0; p<lenT-1; p++){
var email = teacher[p];
var subject = "Students out for " + sport;
var body = "Teachers,\nThe following students will be out for " +sport+ " on " +date +": \n\n" + playerTrue +"\n";
GmailApp.sendEmail(email,subject,body);
}
};
EDIT
I have created another function to try and get it to return each name with a return after each name, but I can only get it to do the first name:
function createRoster(){
var roster = SpreadsheetApp.getActive().getRange("Sheet1!A6:B").getValues();
var playerTrue = [];
var lenR=roster.length;
for(var i=0; i<lenR; i++){
if(roster[i][1])
playerTrue[i] = roster[i][0]; }
playerTrue = playerTrue.filter(String);
Logger.log(playerTrue);
for(var b=0; b<lenR-1; b++){
return playerTrue[b] + "\n";
}
Logger.log(playerTrue);
};
So now the body variable in the original function looks like this:
var body = "Teachers,\nThe following students will be out for " +sport+ " on " +date +": \n\n" + createRoster() +"\n";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的显示脚本来看,
playerTrue
似乎是一个数组。当直接使用数组作为文本体时,就会出现这样的情况。当想要将值垂直方向对齐时,使用join
进行如下修改怎么样?From:
To:
或者,当你想每2行放入一个值时,下面的修改如何?
参考:
From your showing script, it seems that
playerTrue
is an array. When the array is directly used as the text body, such a situation occurs. When you want to align the value to the vertical direction, how about the following modification usingjoin
?From:
To:
Or, when you want to put the value every 2 lines, how about the following modification?
Reference: