【发布时间】:2019-02-18 10:50:28
【问题描述】:
我正在使用这个 nodejs 模块google-spreadsheet 我可以阅读、编辑单元格。没问题。但是当我尝试添加一行时,我收到了这个错误。
sheet #1: Sheet22 1000x26
Error: HTTP error 400 (Bad Request) - Blank rows cannot be written; use delete instead.
我在寻找答案,他们都是关于编辑标题的吗?但我似乎无法理解这里缺少什么,因为标题已经存在。
非常感谢!
这是我的代码
saveTransaction: function(transaction){
var creds = require('./credentials.json');
var doc = new GoogleSpreadsheet('xxxxxxxx');
doc.useServiceAccountAuth(creds, function (err) {
if (err) console.log(err);
doc.getInfo(function (err, info) {
console.log('Loaded doc: ' + info.title + ' by ' + info.author.email);
sheet = info.worksheets[21];
console.log('sheet #1: ' + sheet.title + ' ' + sheet.rowCount + 'x' + sheet.colCount);
var newrow = {
title: 'banana',
type: 'fruit',
url: 'http://example.com'
};
doc.addRow(17, newrow, function( err, rows ){
if (err) console.log(err);
console.log(rows);
});
})
})
},
【问题讨论】:
-
您在此处显示的代码不会尝试添加空白行,而是尝试添加具有值的行。另请注意,您的示例工作表有一个重复的列标签,而您添加的行内容有一个键--
type--您的工作表没有。这可能是用户错误,因为您指定的键与目标工作表的标题不匹配。 -
您应该查看您正在使用的库的代码:github.com/theoephraim/node-google-spreadsheet/blob/… 实际的官方Sheets REST API 支持插入空白行。 (并且有node.js documentation
-
你的第一个参数
17到doc.addRow()似乎很奇怪。工作表的 AFAIK id 是oh6fgn3形式的字符串。 (您可以通过doc.getInfo()api 调用将它们放入info.worksheets[]数组中)。我敢打赌,错误消息是红鲱鱼,并且您的工作表 ID 无效。
标签: javascript node.js google-sheets google-sheets-api