bubakazouba
bubakazouba

Reputation: 1683

trying to append row google sheets api

I have been trying to get the append row example to work for a couple of hours now with no luck, I'm getting no errors but my spreadsheet isn't getting updated at all.

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

Response:

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

Am I missing something?

Edit

My Spreadsheet is completely empty

Upvotes: 2

Views: 10319

Answers (6)

Kunga Tashi
Kunga Tashi

Reputation: 1

In the updated googleapis client for Node.js, the resource property has been renamed to 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'],
    ],
  },
});

Link to the sample from googleapi's doc: Append sample

Upvotes: 0

Ankur Phani
Ankur Phani

Reputation: 56

This is how I do it and it works fine for me.

//Write rows TO 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]
         ],

    }
})

Upvotes: 1

malix
malix

Reputation: 3572

Answers didn't quite work for me...

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

Upvotes: 2

Yoshi
Yoshi

Reputation: 412

For Google API v4 formatting your request as such should work:

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

Upvotes: 1

Eric Koleda
Eric Koleda

Reputation: 12673

The Node.js client library expects the body of the request in a parameter called resource. See the following example:

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

Upvotes: 2

Grant Harding
Grant Harding

Reputation: 33

Only difference I see is the example includes a range for the sheet which you're updating:

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

Upvotes: 1

Related Questions