Reputation: 573
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!
Upvotes: 1
Views: 74
Reputation: 10776
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);
Upvotes: 1