1

I have a beautiful set of code provided from one of the best community member. What this code does is it reads a cell value from Google spreadsheet and then automatically check the checkbox of a html form. Now I want to make that code read a range of spreadsheet meaning making the values store in array and perform the same function in html form.

Here is the Javascript which reads a single cell value:-

function getValues(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var match1 = sheet.getRange("B2").getValue();
return match1;
}

My new JS code has changed say match1 = sheet.getRange(2,2,5,1).getValue(); how should my HTML code would change?

Here is the Index.html which performs the auto checkbox function:-

<script>
google.script.run.withSuccessHandler(checkDefault).getValues();

function checkDefault(val){
var checkBoxName = "name"+val;
document.getElementById(checkBoxName).checked = true;
}
</script>

Any help is appreciated. Thank You!

0

1 Answer 1

1

To read ranges you need to use the range's getValues() method, not getValue(); getValue returns the top leftmost cell's contents.
getValues() will return an array of rows. Each row being an array of cells within that row.
Be aware that it returns an array of arrays, even if it's just one column wide or one row high.
So if you want an array you can

var match1 = sheet.getRange("B2").getValues().map(function(row) {return row[0]});

In the script try

google.script.run.withSuccessHandler(checkDefault).getValues().forEach(checkDefault);
Sign up to request clarification or add additional context in comments.

6 Comments

I have a set of values in cell "B2" = "1", cell "B3" = 2, cell "B4" = 3.
cant make it to run correctly. should I post my sheet link?
It would help to have a reproducible example yes.
Here is the link Robin.
Did you try running onOpen in the script editor? When I did that and approved access to sheets it worked.
|

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.