简体   繁体   中英

Able checkbox from data validation in Google Sheets if a cell contains a value

I've been developing a script thanks to the community which is almost complete to meet my needs.

I've a spreadsheet with 2 types of sheet (1 sheet per employee and 1 sheet for archive): when an employee has finished a project, he can archive/unarchive a row through a checkbox; and this row is removed to Archive sheet.

My script contains a code that fills automatically Col23 with the owner's sheet when a new client is added in Col3.

function onEdit(event) {
  // assumes source data in sheet named Elisa-Miriam-Victor
  // target sheet of move to named Archive
  // getColumn with check-boxes is currently set to column 2 or B
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();


  // When a client is mentioned in Col3, fill automatically Col23 or W by the PM name    
  var lastRow = s.getLastRow();
  var col23 = s.getRange(lastRow, 23).getValue();
  if (r.getRow() == lastRow && r.getColumn() == 3 && col23 == ""){
    s.getRange(lastRow, 23).setValue(s.getSheetName());
 }

  // Archive a project with checked box from Archive Sheet
 if((s.getName() == "Miriam"||s.getName() == "Elisa"||s.getName() == "Hanane"||s.getName() == "Sarah"||s.getName() == "Apoorva"||s.getName() == "Victor")&& r.getColumn() == 2 && r.getValue() == true) { // copy past every time a new PM-sheet is added
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Archive");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  } 
  // Unarchive a project from Archive sheet to respective owner
  else if(s.getName() == "Archive" && r.getColumn() == 2 && r.getValue() == false) {
    var row = r.getRow();
    var nameColumn = 23;
    var name = s.getRange(row, nameColumn).getValue();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName(name);
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);   
}
}

I would like a code similar to this one for which the objective would be to generate a checkbox from data validation in Google Sheets when Col3 is filled . I don't want to use text with box checked or unchecked; I need the tick box from data validation because that allows employee to archive/unarchive.

Thank you for your help!

To incorporate the creation of checkboxes into your already existing code, modify your first if statement as following:

  if (r.getRow() == lastRow && r.getColumn() == 3 && col23 == ""){
    s.getRange(lastRow, 23).setValue(s.getSheetName());
    var rule = SpreadsheetApp.newDataValidation().requireCheckbox().build()
    s.getRange(lastRow,2).setDataValidation(rule);
  }

Explanation:

To set checkboxes, you need to use the method setDataValidation() , building the rule with the criteria requireCheckbox()

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