NodeJS 尝试追加行谷歌表单API

mf98qq94  于 2023-11-17  发布在  Node.js
关注(0)|答案(6)|浏览(108)

我一直试图让追加行的例子工作了几个小时,现在没有运气,我没有得到任何错误,但我的电子表格是不是得到更新。
https://developers.google.com/sheets/samples/writing#append_values

function insertInSpreadSheet(sheetId, sheetService) {
var body = {
    "range": "Sheet1",
    majorDimension: "ROWS",
    "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)"]
    ]
}

var params = {
    spreadsheetId: sheetId,
    "range": "Sheet1",
    valueInputOption: "USER_ENTERED",
    media: {
        body: body  
    }
};
sheetService.spreadsheets.values.append(params, function(err, res) {
    if(err){
        console.log(err);
        return;
    }
    console.log(res);
    return;
});
}//end function

字符串
回复:

{ 
    spreadsheetId: '1NvBbTlET7G5yhuuFF6zj-7LQUQ9TI85_geviRLuzCD0',
    updates: { 
        spreadsheetId: '1NvBbTlET7G5yhuuFF6zj-7LQUQ9TI85_geviRLuzCD0',
        updatedRange: 'Sheet1!A1' 
    } 
}


我错过什么了吗?

编辑

我的简历完全是空的

vom3gejh

vom3gejh1#

Node.js客户端库要求在一个名为resource的参数中包含请求的主体。请参见以下示例:

sheets.spreadsheets.values.append({
  spreadsheetId: spreadsheetId,
  range: 'Sheet1',
  valueInputOption: 'USER_ENTERED',
  resource: body,
}, function (err, response) {
  // ...
});

字符串

qvtsj1bj

qvtsj1bj2#

答案对我不太管用...

// file comes from drive.files.list
  // sheet comes from sheets.spreadsheets.get
  const rows = [['1','2',3/* , ... */]]; 
  const request = {
    spreadsheetId: file.id,

    range: sheet.title,  // TODO: Update placeholder value.

    valueInputOption: 'USER_ENTERED',

    insertDataOption: 'INSERT_ROWS',  // TODO: Update placeholder value.

    resource: {
      values: rows
    }
  };

  sheets.spreadsheets.values.append(request, function (err, response) {
   // ...

字符串

kxxlusnw

kxxlusnw3#

  • 我就是这么做的而且效果很好 *

//将行写入工作表

const { name, email, phone, city, pin, nick } = req.body;

await googleSheets.spreadsheets.values.append({
    auth,
    spreadsheetId,
    range: "test1!A:E",
    valueInputOption: "USER_ENTERED",
    resource: {
         values: [
             [name, email, phone, city, pin, nick]
         ],

    }
})

字符串

33qvvth1

33qvvth14#

我看到的唯一区别是示例包含了要更新的工作表的范围:

"range": "Sheet1!A1:E1",

字符串

wfsdck30

wfsdck305#

对于Google API v4,您的请求格式应该如下:

requests.push({
appendCells: {
  sheetId: 2063764471,
  fields: '*',
  rows: [{
    values: [{ // First Row
      userEnteredValue: {stringValue: "first row"}
    }, {
      userEnteredValue: {numberValue: 2}
    }, {
      userEnteredValue: {numberValue: 3}
    }]
  }]
}});

字符串

uttx8gqw

uttx8gqw6#

在更新的Googleapis客户端中,resource属性已重命名为requestBody

const res = await sheets.spreadsheets.values.append({
  spreadsheetId,
  range,
  valueInputOption: 'USER_ENTERED',
  requestBody: {
    values: [
      ['Justin', '1/1/2001', 'Website'],
      ['Node.js', '2018-03-14', 'Fun'],
    ],
  },
});

字符串
链接到googleapi文档中的示例:Append sample

相关问题