Button setOnAction: check in and out

200 views
Skip to first unread message

Mani S

unread,
Feb 10, 2023, 11:55:14 AM2/10/23
to Google Apps Script Community
Hi

I am writing a check-in and check-out system so people can sign out gear using Google sheet.

check out function - gets the username and email and protects the cells, disables the checkout button until check in function runs

check in - clears content and (will remove protection) and re-enable checkout button 



checkout  () - works and changes the function for checkout button

checkin () - same code DOES NOT work setOnAction and gives undefined error. 

function checkout() {

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const table = ss.getSheetByName("table")
  
  var cue = Session.getActiveUser().getEmail()
  // 1 row , 4 column
  table.getRange(5, 4).setValue(cue);

  Logger.log(cue)

  var userName = Session.getEffectiveUser().getUsername()
  Logger.log(userName)
  table.getRange(5, 3).setValue(userName);

  var range = table.getRange('C5:F5');
  var protection = range.protect().setDescription('Sample protected range');

protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}

const drawings = table.getDrawings();
const button = drawings.find(drawing => drawing.getOnAction() == "checkout");
button.setOnAction("1");


function checkin() {

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const table = ss.getSheetByName("table")
  

  var range = table.getRange('C5:F5');

  range.clearContent();

  const drawings = table.getDrawings();
  const button = drawings.find(drawing => drawing.getOnAction() == "1");
  button.setOnAction("checkout");

}

checkinout.png

Brett Grear

unread,
Feb 14, 2023, 11:38:21 AM2/14/23
to Google Apps Script Community
button.setOnAction("1") is essentially changing your button to look for a function called 1() the next time it is clicked
This obviously doesn't exist as functions can't start with numbers

Your script should say button.setOnAction("checkin") so that it then looks for the checkin function.

However, I think I'm missing some key piece of information though on how your script runs as there doesn't seem to be anyway that the script knows which line (item) the checkout or checkin process is run on

What was your reason for changing the button action to '1'?

Reply all
Reply to author
Forward
0 new messages