将我的代码从Variable更改为Array [英] Change my code from Variable to Array

查看:128
本文介绍了将我的代码从Variable更改为Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一套最好的社区成员提供的一组漂亮的代码。这段代码的功能是从Google电子表格中读取单元格值,然后自动检查html表单的复选框。现在我想让代码读取一系列电子表格的含义,使值存储在数组中,并以html形式执行相同的功能。

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;
}

我的新JS代码已经改变,例如
match1 = sheet.getRange (2,2,5,1).getValue();
我的HTML代码应该如何改变?

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

以下是执行自动复选框功能的Index.html: -

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!

推荐答案

要读取范围,您需要使用范围的 getValues()方法,而不是 getValue(); getValue 会返回最左上角的单元格内容。

getValues() 。每行都是该行内的单元格数组。

请注意,它返回一个数组数组,即使它只是一列宽或一行高。

因此,如果您想要一个数组你可以

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);

这篇关于将我的代码从Variable更改为Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆