• 카테고리

    질문 & 답변
  • 세부 분야

    업무 자동화

  • 해결 여부

    미해결

안녕하세요 onEdit 이쪽부분에서 오류가 나서 질문드립니다.

22.04.21 21:19 작성 조회수 402

0

let ws=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("명렬표");
let ref=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("이름");

let options=ref.getRange(2,1,ref.getLastRow()-1,2).getValues()



function onEdit(e){
  const activerange=e.range;
  const row=activerange.getRow()
  const col=activerange.getColumn()

  const val=activerange.getValue()

  if (col===1){
    const filteredoptions=options.filter(r=>r[0]===val)
    const listofName=filteredoptions.map(r=>r[1])

    const cell=ws.getRange(row,2)
    setValidation(listofName,cell)

  }

}


function setValidation(list,cell){


const rule=SpreadsheetApp.newDataValidation().requireValueInList(list).build()
ws.getRange(cell).setDataValidation(rule)
 
 
 
 
 
TypeError: Cannot read property 'range' of undefined



}
 
 
여기서 e.range 이부분에서 오류가 지속발생합니다. ㅠㅠ 부탁드립니다.
 
typeError

답변 1

답변을 작성해보세요.

0

안녕하세요. 

 

현재 작성하신 코드 확인 한 결과로는 아래 setValidation 함수 부문에서 아래가 누락이 되어 onEdit이 참조해야 되는데 실행 이 되지 않는 것으로 보입니다.  ^^

cell.setDataValidation(rule)

아래와 같이 원본 코드를 보내드립니다. 비교해 보시면 차이점을 알 수 있을 것입니다.

let ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('master');
let ref = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('ref')
let options = ref.getRange(2, 1, ref.getLastRow()-1, 2).getValues()

function onEdit(e) {
  const activerange = e.range;
  const row = activerange.getRow()
  const col = activerange.getColumn()
  const val = activerange.getValue()

  if(col === 1) {
    const filteredoptions = options.filter(r => r[0] === val)
    const listofName = filteredoptions.map(r => r[1])
    // console.log(listofName)
    const cell = ws.getRange(row, 2)
    setValidation(listofName, cell)
    }
  if(col === 3) {
    ws.getRange(row,4).setValue(new Date())
    }
}

function setValidation(list,cell) { 
  const ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('master');
  const rule = SpreadsheetApp.newDataValidation().requireValueInList(list).build()
  cell.setDataValidation(rule)
}