[英]Javascript Read data from spreadsheet
I am using a HTML form to get data from google spreadsheet. 我正在使用HTML表单从Google电子表格获取数据。
I need to get the row where SerNo= 2 (or any specific number) I am looping through the sheet and trying to get the values as below - but it does nothing 我需要获得其中SerNo = 2(或任何特定数字)的行,我正在工作表中循环并尝试获取如下所示的值-但它无能为力
ex: SerNo Col2 例如:SerNo Col2
2 Option1 2选项1
3 Option2 3选项2
4 Option3 4选项3
So,if SerNo=2 ...I want to get Option1. 因此,如果SerNo = 2 ...我想获得Option1。 This has 24 columns so i have used the getLastColumn
这有24列,所以我用过getLastColumn
{function getDataRows_(ss, sheetname) {
var sh = ss.getSheetByName(sheetname);
var lr= sh.getLastRow();
for(var i=1;i<=lr;i++){
var SerNo1 = sh.getRange(i, 2).getValue();
if(SerNo1==SerNo){
return sh.getRange(i, 2, 1, sh.getLastColumn()).getValues();
}
}
}
----edit--- I have posted the whole code I use since it looks like i am filtering records at the wrong place ----编辑---我张贴了我使用的全部代码,因为它看起来好像我在错误的位置过滤记录
function read_value(request,ss){
var output = ContentService.createTextOutput(),
data = {};
var sheet="sheet1";
data.records = readData_(ss, sheet);
var callback = request.parameters.callback;
if (callback === undefined) {
output.setContent(JSON.stringify(data));
} else {
output.setContent(callback + "(" + JSON.stringify(data) + ")");
}
output.setMimeType(ContentService.MimeType.JAVASCRIPT);
return output;
}
function readData_(ss, sheetname, properties) {
if (typeof properties == "undefined") {
properties = getHeaderRow_(ss, sheetname);
properties = properties.map(function(p) { return p.replace(/\s+/g, '_'); });
}
var rows = getDataRows_(ss, sheetname),
data = [];
for (var r = 0, l = rows.length; r < l; r++) {
var row = rows[r],
record = {};
for (var p in properties) {
record[properties[p]] = row[p];
}
data.push(record);
}
return data;
}
function getDataRows_(ss, sheetname) {
var sh = ss.getSheetByName(sheetname);
return sh.getRange(2, 1, sh.getLastRow() -1,sh.getLastColumn()).getValues();
}
function getHeaderRow_(ss, sheetname) {
var sh = ss.getSheetByName(sheetname);
return sh.getRange(1, 1, 1, sh.getLastColumn()).getValues()[0];
}
One thing I would recommend is to not retrieve the data on row at a time; 我建议的一件事是不要一次检索行中的数据。 in other words retrieve all of the data that you want to search through into an array (ie row 1 through last row) and then test each row of the array, looking for your value.
换句话说,将要搜索的所有数据检索到一个数组中(即从第1行到最后一行),然后测试该数组的每一行,以查找您的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.