You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Apps Script 新增A34:B40数组处理及日期格式化实现

修改方案

一、调整数组捕获逻辑:新增第二组数据+统一格式化日期

直接替换你原来的数组获取代码,完全复用你之前的空行过滤逻辑,新增A34:B40区域的数据处理,同时在生成对象阶段直接把日期转为dd/MM/yyyy格式,从根源解决日期显示带时区的问题:

// 处理第一组数据:A24:B30区域
var values  = ws.getRange('$A$24:$B$30').getValues();
// 过滤空行
var filtered_values = values.filter(function(r){
  return r.join("").length>0;
});

var arr_obj = [];
filtered_values.forEach(val => {
  var obj = {
    // 格式化日期:自动适配脚本时区,输出日/月/年格式
    "date" : Utilities.formatDate(val[0], Session.getScriptTimeZone(), "dd/MM/yyyy"),
    "quantite" : val[1]
  }
  arr_obj.push(obj)
})
Logger.log(arr_obj);

// 新增:处理第二组数据:A34:B40区域,逻辑和第一组完全一致
var values2  = ws.getRange('$A$34:$B$40').getValues();
var filtered_values2 = values2.filter(function(r){
  return r.join("").length>0;
});

var arr_obj2 = [];
filtered_values2.forEach(val => {
  var obj = {
    "date" : Utilities.formatDate(val[0], Session.getScriptTimeZone(), "dd/MM/yyyy"),
    "quantite" : val[1]
  }
  arr_obj2.push(obj)
})
Logger.log(arr_obj2);

注:如果运行后发现日期差1天,把Session.getScriptTimeZone()替换为字符串"Europe/Paris"即可,匹配你所在的中欧夏令时时区。

二、修改邮件预处理逻辑,传入第二组数据

替换你原来的预处理代码,把第二组数组也传给邮件模板,顺便修正你原来日志写法的错误(原代码里emailTemp.arr_obj不存在,只会输出undefined):

// 给模板传入两组表格数据
emailTemp.content = arr_obj;
emailTemp.content2 = arr_obj2; // 新增第二组数据的模板变量

var i = ws.getRange("$B$31").getValue();

三、修改HTML邮件模板,新增第二个表格

删掉原来模板里没用的ctr计数变量(你定义了之后完全没用到),在第一个表格后追加第二个表格即可,替换为如下代码:

<table style="border-collapse: collapse; width: 100%; height: 60px;" border="1">
<thead>
<tr style="height: 22px;">
<td style="width: 29.2011%; background-color: #c2e0f4; height: 22px; border-color: #000000; border-style: solid;"><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">Date</span></td>
<td style="width: 26.3085%; background-color: #c2e0f4; height: 22px; border-color: #000000; border-style: solid;"><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">Quantité</span></td>
</tr>
</thead>
<tbody>
    <? content.forEach(function (arrayItem) { ?>
<tr style="height: 19px;">
<td style="width: 29.2011%; height: 19px; border-color: #000000; border-style: solid;"><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><div class="date"> <?= arrayItem.date ?></div></span></td>
<td style="width: 26.3085%; height: 19px; border-color: #000000; border-style: solid;"><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><?= arrayItem.quantite ?></span></td>
</tr>
    <? }); ?>
</tbody>
</table>

<!-- 新增第二组数据的表格,和第一组样式保持一致 -->
<table style="border-collapse: collapse; width: 100%; height: 60px; margin-top: 20px;" border="1">
<thead>
<tr style="height: 22px;">
<td style="width: 29.2011%; background-color: #c2e0f4; height: 22px; border-color: #000000; border-style: solid;"><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">Date</span></td>
<td style="width: 26.3085%; background-color: #c2e0f4; height: 22px; border-color: #000000; border-style: solid;"><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;">Quantité</span></td>
</tr>
</thead>
<tbody>
    <? content2.forEach(function (arrayItem) { ?>
<tr style="height: 19px;">
<td style="width: 29.2011%; height: 19px; border-color: #000000; border-style: solid;"><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><?= arrayItem.date ?></span></td>
<td style="width: 26.3085%; height: 19px; border-color: #000000; border-style: solid;"><span style="font-family: arial, helvetica, sans-serif; font-size: 10pt;"><?= arrayItem.quantite ?></span></td>
</tr>
    <? }); ?>
</tbody>
</table>

草稿生成的代码不需要改动,直接运行就能得到你要的效果:两个表格正常渲染,日期统一显示为日/月/年的格式。

内容的提问来源于stack exchange,提问作者Thierry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 11:33:21