SpreadsheetApp.Range.setValues参数不匹配错误排查求助
排查SpreadsheetApp.Range.setValues参数不匹配错误
错误信息
Exception: The parameters (number[]) don't match the method signature for SpreadsheetApp.Range.setValues
相关代码
do { //Get all the pages of comments in case the doc has more than 100 callArguments['pageToken'] = pageToken; //This is where the magic happens! docComments = Drive.Comments.list(fileId,callArguments); //I've created a "getCommentsInfo" to organize the relevant info in an array info = info.concat((docComments.items)); if (docComments.items && docComments.items.length > 0) { for (var i = 0; i < docComments.items.length; i++) { var comment = docComments.items[i]; // add comment and highlight to array's first element info.unshift([comment.content]); pageToken = docComments.nextPageToken; } } commentsheet.getRange("A1:A"+ info.length).setValues(info);
数组日志输出(截断)
[[projected weight], [projected weight], [projected weight], [projected weight], [projected weight], [projected weight], [projected weight], [pre-gym weight - 150.4 bf% - 6.7 post-gym weight -150.2 bf% - 7.8], [pre-cardio weight 149.4 bf% -7.1], [225 lbs], [225], [275], [295], [225], [185], [185], [185], [bodyweight], [bodyweight], [35], [35], [150], [170], [190], [185], [185], [185], [185], [205], [205], [215], [225], [7.5], [9], [10.5], [95], [95], [95], [bodyweight], [25], [25], [70], [70], [70], [80], [80], [80], [100], [115], [115], [3 45s], [3 45s + 1 25], [4 45s], [75], [75], [75], [95], [95], [95], [70], [90], [90], [135], [185], [205], [225], [135], [155], [175], [175], [35], [35], [35], [35], [25], [25], [25], [17.5], [22.5], [22.5], [32.5], [42.5], [42.5], [7.5], [9], [10.5], [135], [155], [155], [175], [35], [35], [35], [35], [130], [130], [130], [70], [70], [70], [70], [115], [115], [115], [115], [3 45s], [3 45s + 1 25], [3 45s + 1 25], [4 45s], [70], [70],
问题原因
SpreadsheetApp.Range.setValues()要求传入严格的二维数组:外层数组的每个元素必须是一个一维数组(对应工作表的一行,子数组元素对应列值)。你的代码存在两个核心问题:
- 数组结构污染:
info = info.concat(docComments.items)直接将Drive API返回的评论对象数组拼接到info中,这些评论是对象而非一维数组,导致info变成「混合对象与数组」的非标准结构。 - 逻辑冗余冲突:循环中用
info.unshift([comment.content])把评论内容插入数组头部,但同时又通过concat混入了原始评论对象,最终数组结构完全不符合setValues的参数要求。
解决方案
修正数组构建逻辑,确保info始终是纯二维数组,同时优化范围选择:
var info = []; // 初始化空数组,避免继承未知结构 do { callArguments['pageToken'] = pageToken; docComments = Drive.Comments.list(fileId, callArguments); if (docComments.items && docComments.items.length > 0) { for (var i = 0; i < docComments.items.length; i++) { var comment = docComments.items[i]; // 仅将评论内容以一维数组形式添加到info info.push([comment.content]); pageToken = docComments.nextPageToken; } } } while (pageToken); // 补全do-while循环的终止条件 // 使用行列索引指定范围,避免字符串拼接错误 commentsheet.getRange(1, 1, info.length, 1).setValues(info);
关键修正点:
- 移除
info.concat(docComments.items),不再混入原始评论对象; - 用
push([comment.content])统一构建符合要求的二维数组结构; - 补全do-while循环的
while(pageToken)条件,确保遍历所有评论页; - 使用
getRange(行号, 列号, 行数, 列数)的方式指定范围,比字符串拼接更可靠。
内容的提问来源于stack exchange,提问作者jacobsonblanton
相关产品推荐
相关产品推荐

