【发布时间】:2015-06-22 19:25:11
【问题描述】:
我有一个电子表格,每行包含有关用户的信息,每行的第一列是用户的 ID。我正在尝试在电子表格中搜索某人输入的 ID,并检索有关具有该 ID 的用户的信息。我已经用更简单的数据实现了概念验证,但现在脚本根本不返回任何信息。我从电子表格中检索信息的功能如下。任何想法?
//Determines which request the user wants to approve/deny based on the ID they enter.
function getRequestFromSheet(id){
//Gets the info as an array from the spreadsheet.
var ss = SpreadsheetApp.openById('*spreadsheets ID*');
var sht = ss.getSheetByName('data');
var range = sht.getRange(1, 1, sht.getLastRow(), sht.getLastColumn()).getValues();
//Initially assumes the user entered an invalid request ID
var row = -1;
var rowData = [];
//Searches for the ID the user entered in the spreadsheet.
for(var i=0; i<range.length;i++){
if(id == parseInt(range[i][0])){
row = i;
}
}
//Returns the variable data to the function changeInfo.
if(row != -1){
rowData = range[row];
}
else
rowData = ['', 'INVALID ID', 'INVALID ID'];
return rowData;
}
【问题讨论】:
-
您有该工作表的链接吗?工作表中的第一列 (ID) 是文本还是数字?您发送给函数的 ID 是数字?
-
您的代码适用于我,使用纯数字。尝试将
rowData数组转换为字符串return rowData.toString();,看看是否有什么不同。 Apps 脚本不会从服务器返回对象。 -
工作表中的数字。我目前已经设置好了,如果脚本不能匹配用户在电子表格中输入的 ID,它会显示一个警告(我已经成功测试过)。当我输入一个我知道在电子表格中的 ID 时,什么也没有发生。没有找不到 ID 的警告。电子表格确实有超过 50 列,不确定这是否重要...?
标签: javascript google-apps-script google-sheets