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

如何修改Google Apps Script实现表单数据插入Sheets时跳过前两行插在顶部

解法说明

你只需要把原来grabarEnSheets函数末尾的appendRow逻辑替换为「在第3行前插入新行再赋值」的逻辑即可,具体修改后的完整代码如下:

function grabarEnSheets(e) {
  //Conectarse con sheets
  const libro=SpreadsheetApp.openById("SheetID")
  const hoja=libro.getSheetByName("Patrol-Logs")
  
  //Traer respuestas
  const respuestaEnviada=e.response;
  const respuestas=respuestaEnviada.getItemResponses();
  const Nombre = respuestas[0].getResponse();
  const Dia = respuestas[1].getResponse();
  const Tiempo = respuestas[2].getResponse();
  const Evidencia = respuestas[3].getResponse();
  const Traje = respuestas[4].getResponse();

  //Campos nuevos
  var fecha=new Date();

  // 替换原来的appendRow逻辑:在第3行(跳过前2行表头)前插入新行,再写入数据
  hoja.insertRowBefore(3);
  hoja.getRange(3, 1, 1, 6).setValues([[fecha,Nombre,Tiempo,Dia,Evidencia,Traje]]);
}

function darPermisos(){
  SpreadsheetApp.openById("sheetID")
  FormApp.getActiveForm();
}

核心修改点说明

  • hoja.insertRowBefore(3):在工作表第3行的位置插入一行空白行,原有从第3行开始的旧数据会自动向下偏移
  • hoja.getRange(3,1,1,6).setValues(...):把新提交的表单数据写入刚刚插入的第3行中,实现新数据始终在数据区域最顶部的效果

工作表结构参考

工作表结构参考图

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 05:24:04