GCP/Apps Script

Apps Script로 Google Sheets의 Checkbox 생성 후, Checkbox 조작하기

whistory 2023. 1. 25. 10:44
반응형

 

Apps Script를 이용해,

1. Google Sheets에 Checkbox를 생성

2. 체크가 되엇을떄 다음셀에 값을 입력

3. 체크된 checkbox 수 세기

4. 전체선택/전체선택해제 

 

를 해보겠다.

 

 

체크박스 생성

function createCheckbox() {
    const sheetName = "sheetNameHere";
    const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
    sheet.getRange("B2:B10").insertCheckboxes();
}

 

 

체크박스 선택 edit event

체크박스 선택 시, 앞 셀의 값을 뒷 셀에 뿌려주기.

/**
 * edit event
 * @param {object} e = event
 */
function onEdit(e) {
  const getValue    = e.value;
  const sheetName   = spreadSheet.getActiveSheet().getName();
  const column      = e.range.getColumn();
  const row         = e.range.getRow();
  const activeCell  = spreadSheet.getActiveCell();

  if ( sheetName == "checkbox_test" ) {
    if ( column == 2 && row >= 2 ) {
      const nextCell = activeCell.offset(0, 1);
      var   preValue = "";
      if ( getValue == "TRUE" ) {
        const preCell = activeCell.offset(0, -1);
        preValue = `나는 ${preCell.getValue()} 입니다.`;
      }
      nextCell.setValue(preValue);
    }
  }
}

 

 

체크박스 선택 edit event

체크박스 선택 시, 앞 두개 셀의 값을 뒷 셀에 뿌려주기.

/**
 * edit event
 * @param {object} e = event
 */
function onEdit(e) {
  const getValue    = e.value;
  const sheetName   = spreadSheet.getActiveSheet().getName();
  const column      = e.range.getColumn();
  const row         = e.range.getRow();
  const activeCell  = spreadSheet.getActiveCell();

  if ( sheetName == "checkbox_test" ) {
    if ( column == 3 && row >= 2 ) {
      const nextCell = activeCell.offset(0, 1);
      var   settingValue = "";
      if ( getValue == "TRUE" ) {
        const nameCell = activeCell.offset(0, -2);
        const jobCell = activeCell.offset(0, -1);
        settingValue = `나는 ${nameCell.getValue()} 입니다. 직업은 ${jobCell.getValue()} 입니다.`;
      }
      nextCell.setValue(settingValue);
    }
  }
}

 

선택된 checkbox count

/**
 * edit event
 * @param {object} e = event
 */
function onEdit(e) {
  const getValue    = e.value;
  const sheetName   = spreadSheet.getActiveSheet().getName();
  const column      = e.range.getColumn();
  const row         = e.range.getRow();
  const activeCell  = spreadSheet.getActiveCell();

  if ( sheetName == "checkbox_test" ) {
    if ( column == 3 && row >= 2 ) {
      const nextCell = activeCell.offset(0, 1);
      var   settingValue = "";
      if ( getValue == "TRUE" ) {
        const nameCell = activeCell.offset(0, -2);
        const jobCell = activeCell.offset(0, -1);
        settingValue = `나는 ${nameCell.getValue()} 입니다. 직업은 ${jobCell.getValue()} 입니다.`;
      }
      nextCell.setValue(settingValue);
    }
	const count = countCheckbox("C2:C10");
    spreadSheet.getRange("D1").setValue(`선택 : ${count} `);
  }
}

function countCheckbox(range) {
  const values = SpreadsheetApp.getActiveSpreadsheet().getRange(range).getValues();
  var checkCount = values.reduce(function (a, b) {
    return a + (b[0] === true ? 1 : 0);
  }, 0);
  return checkCount
}

 

체크박스 모두 선택, 모두 선택 해제

function checkAllCheckbox(){
  const range = "C2:C10";
  const value = true;

  setCheckbox(range, value);
}
function uncheckAllCheckbox(){
  const range = "C2:C10";
  const value = false;

  setCheckbox(range, value);
}
function setCheckbox(range, value) {
  const checkRange = SpreadsheetApp.getActiveSpreadsheet().getRange(range);
  checkRange.setValue(value);
}

반응형