创建Google Spreadsheet API请求时出错:字段值'无法找到

时间:2016-06-06 19:35:42

标签: node.js google-sheets google-spreadsheet-api

这是我的代码:

var sheets = google.sheets('v4');
sheets.spreadsheets.values.update(
{
  auth: auth,
  spreadsheetId: '1tsyo5XFh1CzlF6Xd_q1ciUQY9KDo_rWYGdrwlANBduc',
  range: 'Sheet1!A1:D5',
  values: [
  ["Item", "Cost", "Stocked", "Ship Date"],
  ["Wheel", "$20.50", "4", "3/1/2016"],
  ["Door", "$15", "2", "3/15/2016"],
  ["Engine", "$100", "1", "30/20/2016"],
  ["Totals", "=SUM(B2:B4)", "=SUM(C2:C4)", "=MAX(D2:D4)"]
  ],
},
function (err, response) {
  if (err) {
    console.log(err);
    return res.status(400).send({
      message: errorHandler.getErrorMessage(err)
    });
  }
  console.log(response);
});

我是通过例子来做这件事的 Google Doc

但我收到错误:

{   [错误:收到无效的JSON有效负载。未知名称"值":无法绑定查询参数。字段值'在请求消息中找不到。] 代码:400, 错误:

[ { message: 'Invalid JSON payload received. Unknown name "values": Cannot bind query parameter. Field \'values\' could not be found in request message.',
domain: 'global',
reason: 'badRequest' } ] }

其余代码来自相同的文档,我使用它来阅读电子表格,一切正常。

1 个答案:

答案 0 :(得分:5)

好的我按照http://google.github.io/google-api-nodejs-client/7.0.0/sheets.html

计算出来了

您需要编辑代码,如下所示:

var sheets = google.sheets('v4');
sheets.spreadsheets.values.update(
{
  auth: auth,
  spreadsheetId: '1tsyo5XFh1CzlF6Xd_q1ciUQY9KDo_rWYGdrwlANBduc',
  range: 'Sheet1!A1:D5',
  valueInputOption: 'USER_ENTERED',
  resource: { 
    values: [
      ["Item", "Cost", "Stocked", "Ship Date"],
      ["Wheel", "$20.50", "4", "3/1/2016"],
      ["Door", "$15", "2", "3/15/2016"],
      ["Engine", "$100", "1", "30/20/2016"],
      ["Totals", "=SUM(B2:B4)", "=SUM(C2:C4)", "=MAX(D2:D4)"]
  ]},
},
function (err, response) {
  if (err) {
    console.log(err);
    return res.status(400).send({
      message: errorHandler.getErrorMessage(err)
    });
  }
  console.log(response);
});

如果您希望将输入到电子表格中的值按照字面意思输入(而不是像他们那样格式化),您也可以为'RAW'属性输入'USER_ENTERED'而不是valueInputOption如果你要将它们输入GUI)。