2015-06-22 96 views
0

我有一个电子表格,其中包含有关每行用户的信息,每行的第一列是用户的ID。我试图通过电子表格搜索某人输入的ID并使用该ID检索关于该用户的信息。我已经通过更简单的数据获得了这个概念的证明,但是现在脚本根本不会返回任何信息。下面是我从电子表格中检索信息的功能。任何想法?Google Apps脚本不从电子表格中检索信息

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

} 
+0

您是否有链接到表单?您的工作表中是第一列(ID)文本还是数字?您发送给该功能的ID号码是多少? –

+1

您的代码适用于我,使用普通数字。尝试将'rowData'数组转换为一个字符串,'return rowData.toString();'看看是否有任何区别。 Apps脚本不会从服务器返回对象。 –

+0

这是表中的一个数字。我现在已经设置好了,所以如果脚本无法匹配用户在电子表格中输入的ID,它会显示一条警告(我已经成功测试过)。当我输入一个我知道在电子表格中的ID时,没有任何反应。没有发现无法找到ID的警告。电子表格确实有超过50列,不知道这是否重要...? – Ben

回答

0
//Returns the variable data to the function changeInfo. 
    if (row != -1){ 
    for(var j=0; j<range[row].length; j++){ 
     rowData[j] = range[row][j].toString(); 
    } 
    } 
    else 
     rowData = ['', 'INVALID ID', 'INVALID ID']; 

这工作。谢谢桑迪好。

相关问题