尝试附加行 google 工作表 api

trying to append row google sheets api

我一直在尝试让追加行示例运行几个小时,但没有成功,我没有收到任何错误,但我的电子表格根本没有更新。

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", ".50", "4", "3/1/2016"],
        ["Door", "", "2", "3/15/2016"],
        ["Engine", "0", "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' 
    } 
}

我是不是漏掉了什么?

编辑

我的电子表格完全是空的

我看到的唯一区别是该示例包括您正在更新的 sheet 的范围:

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

Node.js 客户端库需要名为 resource 的参数中的请求正文。请参阅以下示例:

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

对于 Google API v4 格式化你的请求应该有效:

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

答案不太适合我...

  // 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) {
   // ...

我就是这样做的,对我来说效果很好。

//写行到sheet

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]
         ],

    }
})