如何实现查询功能以使用Google电子表格作为数据库?

时间:2013-11-02 19:55:30

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

我正在尝试将电子表格用作数据库,其中每个工作表都是一个表格,并且一个人的名字被用作主键(这似乎不是最好的解决方案,但良好的电子表格界面使得我更喜欢这个解决方案而不是尝试使用ScriptDB。) 我想要执行以下操作:当您在工作表上选择一个名称并按下我添加的菜单上的按钮时,一个函数会在另一个表和一个屏幕中执行搜索,并在另一个表中显示该查询的所有结果,显示只有该表包含的属性记录(稍后我想添加从GDocs模板生成文本文件的可能性)。 我的问题是: 1)考虑到此屏幕/面板UI具有可变长度(因为其他表中的记录编号可能不同),在Google Apps脚本中创建此面板/ UI的最佳方法是什么? (我不想使用Logger.log,因为我想添加一个按钮将查询转换为文件)

2)除了这个解决方案(在生成的2D数组中搜索):

function test(){ // to test the find function with an argument, 'any' in this case 
  var result = findItem('any');
  if(result){Logger.log(result.getA1Notation())}else{Logger.log('no luck !')};
}

function findItem(item){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data = ss.getDataRange().getValues()
  for(var n = 0;n<data.length;++n){
    if(data[n].indexOf(item)>-1){ // this is a "strict" find, ie the value must be the entire search item. If you want to do partial match you should compare differently...
      return (ss.getRange(n+1,data[n].indexOf(item)+1)); // if found return the range. note the +1 because sheets have 1 index while arrays have 0 index
    }
  }
  return false;// if we come to the end of sheet without result...
}

还有另一种方法可以执行这样的查询吗?

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

创建UI实例。然后,主面板内的可滚动面板是执行此操作的最佳方式,然后使用数组搜索数据。我通常创建标题主体和页脚面板,主体可滚动

相关问题