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

Google Sheets脚本获取Col_K(11)值返回空数组,Col_J(10)正常

问题:Google Apps Script读取第11列数据返回空数组,第10列正常

在使用Google Apps Script获取表格数据时,第11列(Col_K)的返回结果是全空数组,但第10列(Col_J)的数据能正常读取。

代码

function oiNotif() {

  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getSheetByName('OiNotif');  
  var condCol;
  var triggerPrice;
  
 
  condCol = sheet.getRange(2,10,sheet.getLastRow()).getValues();
  triggerPrice = sheet.getRange(2,11,sheet.getLastRow()).getValues();

  
  Logger.log(' condCol =  %s',condCol);
  Logger.log(' triggerPrice =  %s',triggerPrice);

}

日志输出

condCol =  [[0.0], [0.0], [0.0], [true], [0.0], [0.0], [0.0], [false], [0.0], [0.0], [0.0], [true], [0.0], [0.0], [0.0], [true], [0.0], [0.0], [0.0], [true], [0.0], [0.0], [0.0], [false], [0.0], [0.0], [0.0], [false], [0.0], [0.0], [0.0], [false], [0.0], [0.0], [0.0], [false], [0.0], [0.0], [0.0], [false], [0.0], [0.0], [0.0], [false], [0.0], [0.0], [0.0], [false], [0.0], [0.0], [0.0], [false], [0.0], [0.0], [0.0], [false], [0.0], [0.0], [0.0], [false], [false], [false], []] 




triggerPrice =  [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]

表格截图

表格截图


问题原因及解决方案

核心原因

getRange参数使用错误:第三个参数是要读取的行数,你传入了sheet.getLastRow(),这意味着从第2行开始,读取「最后有数据的行号」行,直接超出了表格实际有数据的范围,加上第11列对应行如果本身无值(或公式返回空),就会返回空数组[]。

比如表格最后一行是第63行,getRange(2,11,63)会读取第2到第64行,而第64行及之后都是空行,所以返回全空。

修复方案

1. 修正读取范围的行数计算

把读取行数改为sheet.getLastRow() - 1(因为从第2行开始,总行数是最后行号减1):

condCol = sheet.getRange(2, 10, sheet.getLastRow() - 1).getValues();
triggerPrice = sheet.getRange(2, 11, sheet.getLastRow() - 1).getValues();

2. 检查第11列的单元格状态

确认第11列的单元格:

  • 是否真的包含数值,而非公式返回的空字符串(比如=IF(...)返回空)
  • 是否被设置了隐藏或数据验证限制(这种情况少见,但可以排查)

3. 更高效的批量读取方式(推荐)

一次性读取需要的列范围,减少API调用次数,提升效率:

function oiNotif() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getSheetByName('OiNotif');
  // 读取第2行到最后一行,第10-11列的所有数据
  var dataRange = sheet.getRange(2, 10, sheet.getLastRow() - 1, 2);
  var data = dataRange.getValues();
  
  var condCol = data.map(row => [row[0]]); // 保持原格式的二维数组
  var triggerPrice = data.map(row => [row[1]]);
  
  Logger.log('condCol = %s', condCol);
  Logger.log('triggerPrice = %s', triggerPrice);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 17:40:40