简体   繁体   中英

time stamp on a button google spreadsheet

I have a button that will only be visible when a specific cell is empty. if the cell is empty and the button is pressed it will add date and time to that specific cell. my question is, is it possible to let the button check 1 row below when time has passed 4 am so in place of checking cell B2 it will check B3 this is my current code:

    function doGet() {
  var ss = SpreadsheetApp.openById('key');
  var sheet = ss.getSheetByName('klant');
  var klant = sheet.getRange(2, 1, sheet.getLastRow()-1, 1).getValues();
  Logger.log(klant)


  var app = UiApp.createApplication().setTitle('Gym Check In');
  var grid = app.createFlexTable().setId('grid');
  app.add(grid);

  var row = 0;
  var column = 0;
  var enable=false;

  for (var m in klant) {    

    grid.setWidget(row, 0, app.createLabel(klant[m]));

    if(sheet.getRange('G2').getValue()==''){enable=true}
      grid.setWidget(row, 1, app.createButton('Check In').setEnabled(enable).setId(row+2).addClickHandler(app.createServerHandler('checkIn').addCallbackElement(grid)).addClickHandler(app.createClientHandler().forEventSource().setEnabled(false)));}
  if(!enable){grid.setWidget(row, 1, app.createHTML('<b>Error:</b>'));}
  return app;
}

function checkIn(e) {
  var ss = SpreadsheetApp.openById('key');
  var sheet = ss.getSheetByName('klant');

  var button = e.parameter.source;

  sheet.getRange(button, 7).setValue(new Date());
  sheet.getRange(button, 8).setValue(Session.getActiveUser());    
}

There were a few errors and typos in your sample code, I've set up a new version that uses only one server handler (no need to create one for each button since they all call the same handler function) and one client handler (for the same reason : same effect on source).

Then you forgot to increment row and to reset enable to false after each iteration .

I added the date comparison too using a simple getHours() javascript method (see doc on date object here )

Here is the full code, I hope it's clear enough... if not, don't hesitate to come back here .

function doGet() {
  var ss = SpreadsheetApp.openById('1Zfs3LnVYx2JzaZvasdyQek6_FnqelO95VLrKH8gconA');
  var sheet = ss.getSheetByName('klant');
  var klant = sheet.getRange(1,1,sheet.getMaxRows(),sheet.getLastColumn()).getValues();
  var headers = klant.shift();
  Logger.log(headers);
  Logger.log(klant);
  var time = new Date().getHours();
  Logger.log(time);
  var app = UiApp.createApplication().setTitle('Gym Check In');
  var grid = app.createFlexTable().setId('grid').setStyleAttributes({'marginLeft':'60px'});
  app.add(grid);

  var row = 0;
  var column = 0;
  var enable=false;
  var handler = app.createServerHandler('checkIn').addCallbackElement(grid); 
  var cHandler = app.createClientHandler().forEventSource().setEnabled(false)
  for (var m=0 ; m<klant.length-1 ; m++) {  
    Logger.log('value in col 7 and row '+m+' = '+klant[m][6]);
    grid.setWidget(row, 0, app.createLabel(klant[m][0]));
    if(time<4){    
      if(klant[Number(m)+1][6]=='' ){enable=true}
    }else{
      Logger.log(Number(m)+1)
      if(klant[Number(m)+1][6]=='' ){enable=true}
    }
    grid.setWidget(row, 1, app.createButton('Check In').setEnabled(enable).setId(row+2).addClickHandler(handler).addClickHandler(cHandler));

    if(!enable){grid.setWidget(row, 1, app.createHTML('<b>Error:</b>'));}
    row++;
    enable=false;
  }
  return app;
}

function checkIn(e) {
  var ss = SpreadsheetApp.openById('1Zfs3LnVYx2JzaZvasdyQek6_FnqelO95VLrKH8gconA');
  var sheet = ss.getSheetByName('klant');

  var button = e.parameter.source;

  sheet.getRange(button, 7).setValue(new Date());
  sheet.getRange(button, 8).setValue(Session.getActiveUser());    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM