如何解决是否有创建循环以替换数据的功能? 参考文献
我创建了此功能(使用YouTube教程),用于向我学校的家长发送成绩。 因此结果如下:
20-10-13 12:16:57:353 CDT] Entrega de Calificaciones
Buen dia:
Por medio de la presente le informamos que su hijo(a) (STUDENT NAME)
que cursa el grado de Secundaria 1° A obtuvo las siguientes calificaciones
en el 1er. parcial:
ESPAÑOL (8.3)
HISTORIA 6.2
FCyE 6.8
ARTISTICAS 10.0
MATEMÁTICAS 7,7
BIOLOGÍA 9.5
COMPUTACIÓN 8.5
INGLÉS 6.2
GEOGRAFÍA 1.4
我必须创建一个模板来做到这一点。我的意图是替换给定行中该学生的姓名,并替换该行中该学生的成绩而无需更改常数,并向每位家长发送个性化电子邮件
我不知道这是否可行,如何运作。
提前谢谢
这是代码:
function enviarMail(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("1A");
var plantilla = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("1A").getRange("B1").getValue()
var range = ss.getRange(6,1,28,13)
var email = ss.getRange("b6").getValue();
var nombre = ss.getRange("d6").getValue();
const grupo = ss.getRange("d2").getValue();
const periodo = ss.getRange("d4").getValue();
const esp = ss.getRange("e1").getValue();var espres = ss.getRange("e6").getValue();var espres2 = ss.getRange("s6").getValue();
const hist = ss.getRange("f1").getValue();var histres = ss.getRange("f6").getValue();var histres2 = ss.getRange("t6").getValue();
const fce = ss.getRange("g1").getValue();var fceres = ss.getRange("g6").getValue();var fceres2 = ss.getRange("u6").getValue();
const art = ss.getRange("h1").getValue();var artres = ss.getRange("h6").getValue();var artres2 = ss.getRange("v6").getValue();
const mat = ss.getRange("i1").getValue();var matres = ss.getRange("i6").getValue();var matres2 = ss.getRange("w6").getValue();
const bio = ss.getRange("j1").getValue();var biores = ss.getRange("j6").getValue();var biores2 = ss.getRange("x6").getValue();
const com = ss.getRange("k1").getValue();var comres = ss.getRange("k6").getValue();var comres2 = ss.getRange("y6").getValue();
const ing = ss.getRange("l1").getValue();var ingres = ss.getRange("l6").getValue();var ingres2 = ss.getRange("z6").getValue();
const geo = ss.getRange("m1").getValue();var geores = ss.getRange("m6").getValue();var geores2 = ss.getRange("aa6").getValue();
var asunto = "Entrega de Calificaciones"
var mensaje = plantilla .replace("{nombre}",nombre).replace("{grupo}",grupo).replace("{periodo}",periodo)
.replace("{esp}",esp).replace("{espres}",espres).replace("{espres2}",espres2)
.replace("{hist}",hist).replace("{histres}",histres).replace("{histres2}",histres2)
.replace("{fce}",fce).replace("{fceres}",fceres).replace("{fceres2}",fceres2)
.replace("{art}",art).replace("{artres}",artres).replace("{artres2}",artres2)
.replace("{mat}",mat).replace("{matres}",matres).replace("{matres2}",matres2)
.replace("{bio}",bio).replace("{biores}",biores).replace("{biores2}",biores2)
.replace("{com}",com).replace("{comres}",comres).replace("{comres2}",comres2)
.replace("{ing}",ing).replace("{ingres}",ingres).replace("{ingres2}",ingres2)
.replace("{geo}",geo).replace("{geores}",geores).replace("{geores2}",geores2)
}
解决方法
我对老师们情有独钟,实际上我有一些代码可以完全按照您的意图去做。我整理了一下以适应您的情况。如果稍微探索一下,您也许可以取得一些进步。
您可以看到我的example here and make your own copy.
电子表格假定您以某种表格格式(在我的情况下为Q:S
列)获得孩子的成绩。然后,所有学生都在G列中列出,并带有相应的电子邮件,问候语,主题,他们的成绩(以数组形式排列在一起)和结束语。最困难的部分可能是excel数组函数,但这都是常规的电子表格功能。
运行它的过程如下所示。它向下循环G列,直到到达空白行。请注意,如果要排除成员,请在F中取消选中它们。
const ss= SpreadsheetApp.getActiveSpreadsheet().getSheetByName("1A");
const firstCellofNames = "G2";
const columnWithMessage = "M";
function runProcess() {
var theKid = ss.getRange(firstCellofNames);
while(!theKid.isBlank()){
if(theKid.offset(0,-1).getValue()===true){
sendEmail(theKid.getA1Notation());
}
theKid = theKid.offset(1,0);
}
}
function sendEmail(theAddress){
var thePerson = ss.getRange(theAddress)
var theEmailAddress = thePerson.offset(0,1).getValue();
var theSubject = thePerson.offset(0,2).getValue();
var theMessage = ss.getRange(columnWithMessage + thePerson.getRow()).getValue()
var newEmail = GmailApp.createDraft(theEmailAddress,theSubject,theMessage)
newEmail.send();
}
,
解决方案
当前,您具有执行以下操作的以下Apps脚本功能(通过注释//
对代码进行了说明,请看一下):
// A function is a piece of code that executes some functionallity such as sending an email
function enviarMail(){
// Here you are getting the sheet that you are working in
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("1A");
// Get the value of cell B1
var plantilla = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("1A").getRange("B1").getValue();
// Get the values of the range of cells from A6 to AB14
var range = ss.getRange(6,1,28,13);
// Get the value that is in the cell B6 (and you basically get the rest of the values using the same mechanism)
var email = ss.getRange("b6").getValue();
var nombre = ss.getRange("d6").getValue();
// Then you do the same for an amount of cells...
//Set a subject to a variable
var asunto = "Entrega de Calificaciones";
// Create a message for the email using the values we obtained from the previous methods where we were getting these values from different cells of the spreadsheet
// In this message you are replacing a string (text) for the values in your cells. For example if you have "Soy nombre" and in your Spreadsheet you have Juan then you are substituying "nombre" with "Juan" -> "Soy Juan"
var mensaje = plantilla .replace("{nombre}",nombre).replace("{grupo}",grupo).replace("{periodo}",periodo)
.replace("{esp}",esp).replace("{espres}",espres).replace("{espres2}",espres2)
//...do the same operation for all the values
}
此函数仅获取单个学生的详细信息,因为它获取单个行的值。如果要向所有学生发送电子邮件,则必须获取其余的单元格。
这可以通过for loop轻松实现,您可以在其中遍历所有学生行,获取他们的值并向他们中的每一个发送电子邮件。
要获取每个学生的所有分数和详细信息,您可以代替获取单独的单元格值,而使用getValues()
获取数组中所有学生的姓名,年级等值,然后遍历for循环为每个发送电子邮件。
为了说明这一点,这里有一个示例,说明如何仅使用其名称。为了做到这一点,他们的成绩和其他人也要遵循与名字相同的原则:
//Get all the names considering they start from row 6 and that they are in column 4 (D)
// As getValues() return a 2D array I am flattening it into a 1D array so that we only
// need to go through a single for loop iteration.
// NOTE : (start at row 6,start at column 4,get the last row with content and
// substract the rows we ommitted in the beggining to get the total number of rows and
// set the number of columns to just one.
var nombres = ss.getRange(6,4,ss.getLastRow()-5,1).getValues().flat();
// Go over each name of the array
for(i=0;i<emails.length;i++){
// set the name and replace it for the name in the template
nombre = nombres[i];
var asunto = "Entrega de Calificaciones";
var mensaje = plantilla .replace("{nombre}",nombre)
}
}
我建议您浏览Apps Script guide并通读以更好地理解这两段代码在做什么。
参考文献
我希望这对您有所帮助。让我知道您是否需要其他任何东西,或者您是否不了解。 :)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。