0

I have this code

This is an example from Google Sheets API V4 https://developers.google.com/sheets/api/quickstart/nodejs

But I need to insert a value in a spreadsheet, in the doc I see the append method for this case

I have the following function to append (Use googleapis library)

function addValues(auth) {
var sheets = google.sheets('v4');

val = Object.assign({
  majorDimension: "ROWS",
  values: [
    ["Hello","Google","Sheets"]
  ]
},sheets.spreadsheets.values)

//console.log("",val)

val.append({
  auth: auth,
  range: "A1",
  spreadsheetId: '1bhXbigMkNyTgKFVePZIwP5VZE1hN0XcvTRdeFdUSUdo',
  includeValuesInResponse: true,
  insertDataOption: "INSERT_ROWS",
  responseDateTimeRenderOption: "FORMATTED_STRING",
  responseValueRenderOption: "UNFORMATTED_VALUE",
  valueInputOption: "RAW"
}, function(err, response){
  if (err) {
    console.log('The API returned an error: ' + err);
    return;
  }
  console.log(response);
  })
}

...but it won't working

Console return the following messagge

{ spreadsheetId: '1bhXbigMkNyTgKFVePZIwP5VZE1hN0XcvTRdeFdUSUdo',
tableRange: '\'Hoja 1\'!A1:C2',
updates:
{ spreadsheetId: '1bhXbigMkNyTgKFVePZIwP5VZE1hN0XcvTRdeFdUSUdo',
updatedRange: '\'Hoja 1\'!A3',
updatedData: { range: '\'Hoja 1\'!A3', majorDimension: 'ROWS' } } }

It looks like the "values" array is not being sent

My complete code is in https://github.com/aaroncadillac/google-sheets-api/tree/test

The important file is quick.js, I hope you can help me

Bye!

1 Answer 1

1

Solved!!!

If you also went through this or do not know how to use googleapis you can review the official documentation of google api as this library is very subject to it, however is not documented in how to use google resources

The solution for this case is add the parameter "resource" to the principal object "parameters"

append function looks this way

sheets.spreadsheets.values.append({
  auth: auth,
  range: "A1",
  spreadsheetId: '1bhXbigMkNyTgKFVePZIwP5VZE1hN0XcvTRdeFdUSUdo',
  includeValuesInResponse: true,
  insertDataOption: "INSERT_ROWS",
  responseDateTimeRenderOption: "FORMATTED_STRING",
  responseValueRenderOption: "UNFORMATTED_VALUE",
  valueInputOption: "RAW",
  resource: {
    values: [
      ["Hello", "Google", "Sheets"]
    ]
  }
}, function(err, response){
  if (err) {
    console.log('The API returned an error: ' + err);
    return;
  }
  console.log(response);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Have you used batchUpdate in a similar manner?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.