如果单元格值> 1,则禁用按钮0谷歌电子表格

时间:2014-03-14 23:47:57

标签: google-apps-script google-sheets

有人可以帮助我使用以下代码(感谢LennyZ71) 我试图在单元格G2的值为空时启用按钮,否则显示错误消息并隐藏按钮。

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


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

  var row = 0;
  var column = 0;

  for (var m in klant) {    

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

//----------- if value of cell G2 is empty the enable the button below else show error message  -----------\\

    grid.setWidget(row, 1, app.createButton('Check In').setId(row+2).addClickHandler(app.createServerHandler('checkIn').addCallbackElement(grid)).addClickHandler(app.createClientHandler().forEventSource().setEnabled(false)));}
    row++;
  }
  return app;
}

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

  var button = e.parameter.source;

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

1 个答案:

答案 0 :(得分:0)

你可以这样试试:

(错误信息会出现在按钮下方,但您也可以通过调整表格中的坐标将其放在右侧

注意:你说“启用”和“隐藏”,你必须选择其中一个...隐藏它使用setVisible(true/false)代替setEnabled(true/false)

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


  var app = UiApp.createApplication().setTitle('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(member[m]));

//----------- if value of cell G2 is empty then enable the button below else show error message  -----------\\
    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)));}
    row++;
    if(!enable){
      grid.setWidget(row, 1, app.createHTML('<b>error</b> message'));
      row++;
    }
  }
  return app;
}