是否有创建循环以替换数据的功能? 参考文献

如何解决是否有创建循环以替换数据的功能? 参考文献

我创建了此功能(使用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并通读以更好地理解这两段代码在做什么。

参考文献

getRange()

getValues()

replace

SpreadsheetApp

我希望这对您有所帮助。让我知道您是否需要其他任何东西,或者您是否不了解。 :)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res