创建Google Spreadsheet API请求时出错:找不到字段"值"

zon*_*lsk 6 google-sheets node.js 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);
});
Run Code Online (Sandbox Code Playgroud)

我是通过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' } ] }
Run Code Online (Sandbox Code Playgroud)

其余代码来自相同的文档,我用它来从电子表格中读取,一切正常.

thi*_*ami 6

好的,我按以下方法找出了答案: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);
});
Run Code Online (Sandbox Code Playgroud)

你也可以把'RAW'替代'USER_ENTERED'valueInputOption属性,如果你想输入到电子表格从字面上你的价值观,因为它们是(而不是被格式化为,如果他们是,如果你是他们进入GUI)。