Google Apps脚本无法从电子表格中检索信息

时间:2015-06-22 19:25:11

标签: javascript google-apps-script google-sheets

我有一个电子表格,其中包含每行中用户的信息,每行的第一列是用户的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;

}

1 个答案:

答案 0 :(得分: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'];

这很有效。谢谢Sandy Good。