Is there any way so that I can search value upwards?

泄露秘密 提交于 2021-02-11 12:08:24

问题


I was wondering if anyone knows whether it is possible or not to search a value upwards instead of the usual up to down in a column.

The reason why I'm asking is because I'm currently preparing a visitor sheet which will be recorded into Google Sheets, that comes from Google Forms. Each visitor will be given an ID/Pass number which will be used to record their entry and exit time.

The problem now is that, if two people uses the same ID, it will change only the entry and exit time of the initial entry and not the latest one.

Therefore, I was wondering if anyone knows or is it possible so that the script can search upwards instead (so the newer entry will be found first) or if anyone knows a command so that it doesn't overwrite the previous entry (ignore an entry that already has a time recorded in the cell) that would be great.

I've already tried sorting it into a new Tab using QUERY and IMPORTRANGE but then I can't record the time onto the new tab.

var ss = SpreadsheetApp.openByUrl("sheet_url");
var sheet = ss.getSheetByName("Sheet_name");

function doGet(e){
  var action  = e.parameter.action;

  if(action == "in")
    return inTime(e);

  if(action == "out")
    return outTime(e);

}


function doPost(e){
  var action  = e.parameter.action;

  if(action == "in")
    return inTime(e);

  if(action == "out")
    return outTime(e);

}

function inTime(e){
  var id = e.parameter.id;
  var values = sheet.getRange(2,1,sheet.getLastRow(),1).getValues();

  for(var i = 0 ; i<values.length ; i++){
    if(values[i][0] == id){
      i=i+2;
      var in_time = Utilities.formatDate(new Date(), "GMT+8", "HH:mm:ss");
      sheet.getRange(i,8).setValue(in_time);
      return ContentService.createTextOutput("Check In Time "+in_time).setMimeType(ContentService.MimeType.TEXT);


  }
  return ContentService.createTextOutput("Id Not Found").setMimeType(ContentService.MimeType.TEXT);
}


function outTime(e){
  var id = e.parameter.id;
  var values = sheet.getRange(2,1,sheet.getLastRow(),1).getValues();

  for(var i = 0 ; i<values.length ; i++){
    if(values[i][0] == id){
      i=i+2;
      var out_time = Utilities.formatDate(new Date(), "GMT+8", "HH:mm:ss");
      sheet.getRange(i,9).setValue(out_time);
      return ContentService.createTextOutput("Check Out Time "+out_time).setMimeType(ContentService.MimeType.TEXT);
    }
  }
  return ContentService.createTextOutput("Id Not Found").setMimeType(ContentService.MimeType.TEXT);
}

回答1:


In looking through the "values", you could go upwards/backwards to find the newer entries first (assuming newer are at the bottom):

  for (var i = (values.length - 1); i > -1; i--) {

To your other question, on how to check if a cell in column 8 is blank, you could try this:

  if (values[i][0] == id && values[i][7] == "") {



回答2:


A good solution for your script would be to implement an auxiliary variable var k=values.length-i-1, inside the loop which allows you to loop backwards.

Your loops would then look like:

function inTime(e){
  var id = e.parameter.id;
  var values = sheet.getRange(2,1,sheet.getLastRow(),1).getValues();

  for(var i = 0 ; i<values.length ; i++){
    var k=values.length-i-1;
    if(values[k][0] == id){
      k=k+2;
      var in_time = Utilities.formatDate(new Date(), "GMT+8", "HH:mm:ss");
      sheet.getRange(k,8).setValue(in_time);
      return ContentService.createTextOutput("Check In Time "+in_time).setMimeType(ContentService.MimeType.TEXT);
      }
  }
}


来源:https://stackoverflow.com/questions/56897215/is-there-any-way-so-that-i-can-search-value-upwards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!