-1

In this code I have declared a variable to get cell value from some spreadsheet owned by me.

And then I used that variable for innerHtml of

element with id "value".

But the web page is not showing any result.

It gives blank page ..

Any idea, what where went wrong ..?

<!DOCTYPE html> 
<html>
<head>
<title>Getting Values from Sheets</title>
</head>
<body>


<p id="value2">Hello World</p>

<p id="value"></p>

<script src="sheets/snippets/snippets.js">

gapi.client.sheets.spreadsheets.values.get({
  spreadsheetId: 1jH0y-PknkZXH7KqjPBWDv98kkBnndGt_GIbdUh_1nRM,
  range: Users!A2
}).then((response) => {
  var result = response.result;
  var numRows = result.values ? result.values.length : 0;
  console.log(`${numRows} rows retrieved.`);
document.getElementById("value").innerHTML = result;
});
</script>

</body>

</html>

Thank You

1 Answers1

1

Set the value at the time of receiving the response.

gapi.client.sheets.spreadsheets.values.get({
  spreadsheetId: 1jH0y-PknkZXH7KqjPBWDv98kkBnndGt_GIbdUh_1nRM,
  range: users!a2
}).then((response) => {
  var result = response.result;
  var numRows = result.values ? result.values.length : 0;
  console.log(`${numRows} rows retrieved.`);
  document.getElementById("value").innerHTML = result;
});

This is because the next task is executed after sending a request to perform an asynchronous task.

document.getElementById("value").innerHTML = result; code was outside of then().

The var result is not defined at this moment.

Hyunjune Kim
  • 465
  • 4
  • 12
  • What should be the spreadsheet sharing setting ? May I put it to Restricted , or it should be "view only to others" or be "published" ..? –  Jan 28 '22 at 04:25
  • @PRATAPKUMARKOTTI I don't know what it means. For the new problem, it would be better to post a new question. – Hyunjune Kim Jan 28 '22 at 04:32
  • I'm new to javascript, could you modify the code for me ? –  Jan 28 '22 at 04:34
  • @PRATAPKUMARKOTTI No clues have been found in this post to solve the problem about "spreadsheet sharing". – Hyunjune Kim Jan 28 '22 at 04:39
  • I mean to retrieve the data from sheet, is it to be shared to public to "view only" ? Or may I put it default "only Me" ? –  Jan 28 '22 at 04:43
  • @PRATAPKUMARKOTTI The authority to disclose will vary depending on the purpose. If you're not the only one who's going to use it, set it to view only. – Hyunjune Kim Jan 28 '22 at 04:52
  • –  Jan 28 '22 at 06:37
  • @PRATAPKUMARKOTTI What is your goal now? The question is ambiguous. I don't know what you want to get. – Hyunjune Kim Jan 28 '22 at 06:45
  • my goal is to get the cell (Users!A2) value , and want to declare that to a variable. thanks –  Jan 28 '22 at 06:49