1

I am trying to pull just one cell value from a google sheet using javascript and the google sheets API, and I am using the below code, but I keep getting the below error which I can't understand because I am using the spreadsheet id and range.

throw new Error('Missing required parameters: ' + missingParams.join(', ')); Error: Missing required parameters: spreadsheetId, range

const auth = new google.auth.GoogleAuth({
            keyFile: "sheets-credentials.json",
            scopes: "https://www.googleapis.com/auth/spreadsheets",
        });
const client = await auth.getClient();
const googleSheets = google.sheets({ version: "v4", auth: client });
const spreadsheetId = "asheetid";
googleSheets.spreadsheets.values.get(spreadsheetId, `Sheet1!D1`)
TGercken
  • 79
  • 9

1 Answers1

1

In your script, please modify as follows.

From:

googleSheets.spreadsheets.values.get(spreadsheetId, `Sheet1!D1`)

To:

googleSheets.spreadsheets.values.get({spreadsheetId, range: `Sheet1!D1`});

Reference:

Added:

From your following replying,

Now there's no error but it just returns "[object Promise]"

From this situation, I could confirm that your script worked. If you want to retrieve the values from googleSheets.spreadsheets.values.get({spreadsheetId, range: Sheet1!D1});, how about the following sample script?

Sample script1:

const res = await googleSheets.spreadsheets.values.get({spreadsheetId, range: `Sheet1!D1`});
console.log(res.data);

Sample script2:

googleSheets.spreadsheets.values.get(
  {
    spreadsheetId,
    range: `Sheet1!D1`,
  },
  (err, res) => {
    if (err) {
      console.log(err);
      return;
    }
    console.log(res.data);
  }
);
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Now there's no error but it just returns "[object Promise]" – TGercken Feb 02 '22 at 01:34
  • 1
    @TGercken Thank you for replying. I apologize for the inconvenience. About `Now there's no error but it just returns "[object Promise]" `, it's yes. From this situation, I could confirm that your script worked. In order to retrieve the values from the modified script, I added 2 more scripts. Could you please confirm it? – Tanaike Feb 02 '22 at 01:39
  • the sample scripts return { range: 'Sheet1!B1', majorDimension: 'ROWS', values: [ [ '3' ] ] } so I see that it's pulling the number but how do I isolate the value so I can do other things with it? – TGercken Feb 02 '22 at 01:47
  • 1
    @TGercken Thank you for replying. I apologize for the inconvenience, again. About `the sample scripts return { range: 'Sheet1!B1', majorDimension: 'ROWS', values: [ [ '3' ] ] } so I see that it's pulling the number but how do I isolate the value so I can do other things with it?`, in that case, how about `console.log(res.data.values[0][0]);`? – Tanaike Feb 02 '22 at 01:48
  • that works perfectly, thank you! – TGercken Feb 02 '22 at 01:50
  • 1
    @TGercken Thank you for replying. I'm glad your issue was resolved. Thank you, too. – Tanaike Feb 02 '22 at 01:51