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

APK数据同步至Google Sheet时转置内容的空条目问题排查

解决test2表空行问题的方案

嘿,你这个APK同步到Google Sheet的项目已经跑通核心功能了,很棒!不过那个test2表出现空行的问题确实有点烦人,咱们来快速解决它~

问题根源

你当前的代码是无条件给每个thing字段执行appendRow,哪怕字段是空值、不存在或者只有空格,这就导致了仅含ID的空行出现。另外我还发现一个小笔误:sh.appendRow([ID, region, place, staff, thing1, thing2, thing3, thing4, thing4]);里最后一个字段写成了thing4,应该是thing5,这个顺手给你修正了。

修正后的代码

把原来直接追加行的部分改成带条件判断的,只有当thing字段有实际有效内容时才添加行:

function doPost(e) {
  var ss = SpreadsheetApp.openById('XXXXXXXXXXXXXXXXXXX');
  var sh = ss.getSheetByName('Test_Sheet');
  var sh2 = ss.getSheetByName('test2')
  //custom ID
  var scriptProperties = PropertiesService.getScriptProperties();
  var counter = scriptProperties.getProperty('counter') || 0;
  var formatDate = Utilities.formatDate(new Date(), "GMT+8", "ddMMyy");
  var zero = (counter < 10) ? '0' : '';
  var ID = "T" + formatDate + zero + counter++;
  if (counter > 99) counter = 0;
  scriptProperties.setProperty('counter', counter);
  //send from apk
  var region = e.parameter.region;
  var place = e.parameter.place;
  var staff = e.parameter.staff;
  var thing1= e.parameter.thing1;
  var thing2= e.parameter.thing2;
  var thing3= e.parameter.thing3;
  var thing4= e.parameter.thing4;
  var thing5= e.parameter.thing5;
  //Test_Sheet - 修正了thing4重复的笔误
  sh.appendRow([ID, region, place, staff, thing1, thing2, thing3, thing4, thing5]);
  //test2 - 添加空值判断,避免空行
  if (thing1 && thing1.trim() !== '') {
    sh2.appendRow([ID, thing1]);
  }
  if (thing2 && thing2.trim() !== '') {
    sh2.appendRow([ID, thing2]);
  }
  if (thing3 && thing3.trim() !== '') {
    sh2.appendRow([ID, thing3]);
  }
  if (thing4 && thing4.trim() !== '') {
    sh2.appendRow([ID, thing4]);
  }
  if (thing5 && thing5.trim() !== '') {
    sh2.appendRow([ID, thing5]);
  }
}

关键修改说明

  • 给每个thing字段增加了双重判断:thingX && thingX.trim() !== '',既检查字段是否存在(避免undefined导致的错误),又排除了纯空格的无效值,确保只有实际有内容的字段才会生成行
  • 修正了Test_Sheet数据追加时的笔误,把重复的thing4改成了thing5,保证主表数据的准确性

这样修改后,test2表就只会生成有实际内容的ID+thing行,再也不会出现仅含ID的空行啦。

内容的提问来源于stack exchange,提问作者P.J

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 12:13:15