I have created Google Sheet file with script which should, when one checkbox is checked, clear some fields in other cells, set date in some cells and increase some counters in some cells. And this script is sometimes running and sometimes not. I have check logs and everything looks ok. I'm using onEdit function which checks if this checkbox was checked. The worst is that for me it's always working (both on computer and on phone), but for some people who are using it not, so I'm not able to recreate this problem to pinpoint the cause. Does anybody has any idea what is happening or how to fix it?
Code:
function onEdit(eventObj) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var curRow = eventObj.range.getRow();
var idCell = sheet.getRange(curRow, idColumn);
if (!idCell.getValue())
{
rangeIdColumn = sheet.getRange(3,idColumn,300,1);
var values = rangeIdColumn.getValues();
max = 0;
for (var row in values)
{
if (values[row][0] && values[row][0] > max)
max = values[row][0];
}
idCell.setValue(max + 1);
}
const checkedColumn = 3
if (eventObj.range.getColumn() == checkedColumn && eventObj.range.getValue())
{
var curRow = eventObj.range.getRow();
var curRange = sheet.getRange(curRow, 1, 1, 60);
var curValues = curRange.getValues();
for (col = startColumn; col < endColumn; col += 3)
{
//check if was requested
if(curValues[0][col])
{
curRange.getCell(1, col + 1).setValue(false);
curRange.getCell(1, col + 2).setValue(new Date());
var num = curValues[0][col + 2];
if (num)
curRange.getCell(1, col + 3).setValue(Number(num) + 1);
else
curRange.getCell(1, col + 3).setValue(1);
}
}
curRange.getCell(1, endColumn + 3).setValue("");
curRange.getCell(1, checkedColumn - 1).setValue(false);
curRange.getCell(1, checkedColumn).setValue(false);
}
}
question from:
https://stackoverflow.com/questions/65846710/google-sheet-script-with-onedit-is-not-always-working 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…