简体   繁体   English

Google电子表格按钮上的时间戳

[英]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: 我的问题是,是否可以让按钮在凌晨4点过后检查下面的1行,所以代替检查单元格B2它将检查B3,这是我当前的代码:

    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 . 然后,您忘记了增加行数并将每次迭代后的enable重置为false。

I added the date comparison too using a simple getHours() javascript method (see doc on date object here ) 我也使用简单的getHours()javascript方法添加了日期比较(请参阅有关date对象的文档

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());    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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